c# - datagridview only searching first row -
i trying search every cell in datagridview value "test". searching first row... (i believe searching columns) ideas on how can fix this?
datagridview1.selectionmode = datagridviewselectionmode.cellselect; string searchvalue = "test"; int searching = -1; while (searching < 7) { searching++; try { foreach (datagridviewrow row in datagridview1.rows) { if (row.cells[searching].value.tostring().equals(searchvalue)) { row.cells[searching].selected = true; break; } } } catch (exception exc) { // messagebox.show(exc.message); } }
use snippet.. iterate through every row/column , set value selected if find match.
datagridview1.selectionmode = datagridviewselectionmode.cellselect; string searchvalue = "test"; (int row = 0; row < datagridview1.rows.count; ++row) { (int col = 0; col < datagridview1.columns.count; ++col) { var cellvalue = datagridview1.rows[row].cells[col].value; if (cellvalue != null && cellvalue.tostring().equals(searchvalue)) { datagridview1.rows[row].cells[col].selected = true; // if want search every cell searchvalue shouldn't break. // break; } } }
you can above follows, using concise linq code:
datagridview1.selectionmode = datagridviewselectionmode.cellselect; string searchvalue = "test"; datagridview1.rows.tolist().foreach(row => row.cells.tolist().foreach(cell => { cell.selected = (cell.value != null && cell.value.tostring().equals(searchvalue)); }));
Comments
Post a Comment