rai nalasa rai nalasa - 6 months ago 16
SQL Question

Load Selectedrow of datagrid to label

I am trying to load mysql data to label when I select a row in gridview.

enter image description here

here is my class that gets the selected row value and bind it to label.

private void LoadOrders()
{
con.Open();
cmd = new SqlCommand(@"Select quo_product,quo_address
FROM JobQuotations
WHERE quotationId = @id
AND quo_product = @product",con);

//1st row 2nd column
cmd.Parameters.AddWithValue("@id",GridView1.SelectedIndex.ToString());

//1st row 3rd column
cmd.Parameters.AddWithValue("@product",GridView1.SelectedIndex.ToString());

if(rdr.HasRows)
{
while(rdr.Read())
{
lblProductName.Text = rdr["quo_product"].ToString();
lblAddress.Text = rdr["quo_Address"].ToString();
}
}
con.Close();
}


I know I lack some codes inside of my if statement because I don't know how to get the selected rows value on it.What will I input inside my cmd.Parameter in order to get the selectedRow values?

Answer

Try this if it doesn't work let me know the issues and ill see what i can change to make it work, i haven't done it with 2 different cell names before. Can you update the image at the top with the datagridview to have the column names of the datagridview you are using please.

private void dataGrid1_SelectionChanged(object sender, EventArgs e)
        {
           foreach (DataGridViewRow row in dataGrid1.SelectedRows)
            {
                string quo_product = row.Cells[0].Value.ToString();
                //string quo_Address = row.Cells[0].Value.ToString();
            }
            lblProductName.Text = quo_product;
            //lblAddress.Text = quo_Address;
        }

UPDATE

This question is fixed due to a question asked later on by rai nalasa (Question)

Comments