Ayman Ayman - 15 days ago 6
C# Question

DataGridView selected row to display in text boxes based on first row of filtered data

How can I display the first selected row values to text boxes after I filter the data in the datagridview?

private void btnsearch_Click(object sender, EventArgs e)
{
dgpay.DataSource = p.SearchInPaymentVouchers("PaymentVouchers.VendorID", comven.SelectedValue.ToString());
}

Answer
private void btnsearch_Click(object sender, EventArgs e)
{
    DataTable dt = p.SearchInPaymentVouchers("PaymentVouchers.VendorID", comven.SelectedValue.ToString());
    dgpay.DataSource = dt;

    //Code to bind first row in textbox.
    //check if the datatable has rows
    if(dt.Rows.Count > 0)
    {
        textbox.text = Convert.ToInt32(dt.Rows[0]["id"]);
        //Row[line index you want to get]["Header of datatable column ex VendorID"]
    }
}

Hope this is what you actually want. Please feel free to ask if you face any problem.

Comments