How can I get the index of a selected row in a Grid control?
Hi,everyone!
I got a problem when using the Grid control,could you give some advice?
Here is one example,if I have selected the second row,but how to get the index of it in the program?
Thank you!

Roei BarRank #1,271 · 1.4K reputationPosted Sep 9, 2009, 4:08 AM EDT
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e) { // Get the currently selected row using the SelectedRow property. GridViewRow row = CustomersGridView.SelectedRow; // Display the company name from the selected row. // In this example, the third column (index 2) contains // the company name. Message.Text = "You selected " + row.Cells[2].Text + "."; } void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e) { // Get the currently selected row. Because the SelectedIndexChanging event // occurs before the select operation in the GridView control, the // SelectedRow property cannot be used. Instead, use the Rows collection // and the NewSelectedIndex property of the e argument passed to this // event handler. GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex]; // You can cancel the select operation by using the Cancel // property. For this example, if the user selects a customer with // the ID "ANATR", the select operation is canceled and an error message // is displayed. if (row.Cells[1].Text == "ANATR") { e.Cancel = true; Message.Text = "You cannot select " + row.Cells[2].Text + "."; } }xu3 reputationPosted Sep 9, 2009, 7:36 PM EDT