i have read many forums relating to getting the value of the selected row from the gridview and setting it to a textbox, however i cannot copy the code to my project. the code i have frequently seen is:

string selectedItem = MyGridView.SelectedRow.Cells[1].Text;

textboxItem.Text = selectedItem;

However in my Vis studio, the .SelectedRow is unavailable but instead SelectedRows is available, this completely messes the code up as it cannot be compiled. Is there a reason why i cannot use Row instead of rows

any help would be much appreciated

thanks

The GridView is a System.Web.UI control used in ASP.net designs and has a SelectedRow property. If you have the SelectedRows property then it sounds like you are using a DataGridView which is part of the System.Windows.Form namespace.
The DataGridView allows multiple rows to be selected and returns them as a collection of rows. You can prevent this by setting the DataGridView's MultiSelect property to false if you only want the user to be able to select one row.
With that done you can use dataGridView1.SelectedRows[0].Cells[1].Value, jsut be sure to include a check to see if SelectedRows isnt empty before you try and access it :)

EDIT: You can use dataGridView1.SelectedRows[0].Cells[1].Value without disabling MultiSelect, but the result may confusae the user as they will be selecting more than one row but only the first will be handled. You could concatenate the values from each selected row into the textbox, or reconsider your design to handle multiple rows...your shout :p

Edited 6 Years Ago by Geekitygeek: n/a

Comments
This comment answered the problem I was searching for exactly.

that was a lot of help thanks it works perfectly now, much appreciated :)

Edited 6 Years Ago by rjthomas8: n/a

This article has been dead for over six months. Start a new discussion instead.