c# - DataGridView, Get the value of the first column of the selected row -


i'm wondering efficient way value of first column in datagridview row.

currently, i'm using code:

list<string> selectedrows = new list<string>(); foreach (datagridviewrow r in dgv.selectedrows) {     selectedrows.add(r.cells[0].value.tostring()); }  int index = convert.toint32(selectedrows[0]); 

this works fine; however, there more efficient option this?

"most efficient" subjective. don't know following code faster or shorter you've got, it's method prefer use.

if you're trying list of values particular column, try this:

var results = datagridview1.selectedrows                            .cast<datagridviewrow>()                            .select(x => convert.tostring(x.cells[0].value)); 

if allow 1 selected row @ time, , want convert particular cell, try this:

var index = convert.toint32(datagridview1.currentrow.cells[0].value); 

Comments

Popular posts from this blog

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -

c++ - Visual Leak Detector detects leak on new blank MFC project -

c# - Connection String Trouble MetaDataException -