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

C# random value from dictionary and tuple -

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

.htaccess - htaccess convert request to clean url and add slash at the end of the url -