I need to select an entire row if a cell in column 0 contains specified value. I have a TextBox and DaraGridView.when I put a value exist in gridview row selected without problem But when a put doesn't exist in gridview the program throws an exception thank you in advance!!
private void Searchvalue_TextChanged(object sender, EventArgs e)
{
dataGridView1.ClearSelection();
var enteredText = (sender as TextBox).Text;
int rowIndex = -1;
bool tempAllowUserToAddRows = dataGridView1.AllowUserToAddRows;
// Turn off or .Value below will throw null exception
dataGridView1.AllowUserToAddRows = false;
if (enteredText != String.Empty)
{
DataGridViewRow row = dataGridView1.Rows
.Cast<DataGridViewRow>()
.Where(r => r.Cells["Column1"].Value.ToString().Contains(enteredText)).First();
rowIndex = row.Index;
dataGridView1.AllowUserToAddRows = tempAllowUserToAddRows;
dataGridView1.Rows[rowIndex].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows[rowIndex].Index;
}
}
It is unclear what you are trying to accomplish here. It appears to be some sort of “filter” or “selection” mechanism. Whatever you are trying to accomplish here… it appears you are going about this in the most difficult way.
As others have pointed out this error… Sequence contains no elements?
Comments and the error “clearly” states, you are getting an “InvalidOperationException”. I am not a LINQ expert therefore, I am guessing that when you use the following command…
Where(r => r.Cells["Column1"].Value.ToString().Contains(enteredText)).First();
If the enteredText
is NOT found… what is .First()
going to return? This may be the cause of your error. Replacing this with a well-commented solution below may fix “this” problem.
Where(r => r.Cells["Column1"].Value.ToString().Contains(enteredText)).FirstOrDefault();
FirstOrDefault()
will return null if the text is not found. This means it won’t “fail” with the “InvalidOperationException” when the text is not found… however, the variable row
is going to be null and the code will obliging “fail” on the very next line…this time with a null pointer exception since row
will be null if the text is not found.
Without knowing what you are trying to do in an overall perspective I can only highly recommend that you use some sort of “collection” to store the data in the grid, most of these DataSources
have built in mechanisms to help when doing things like searching/sorting etc.… It will obviously require more work from you to manage the grid data yourself.
Without using a data source, the code below should accomplish what you describe.
private void Searchvalue_TextChanged(object sender, EventArgs e) {
dataGridView1.ClearSelection();
var targetText = Searchvalue.Text;
if (targetText != String.Empty) {
foreach (DataGridViewRow row in dataGridView1.Rows) {
if (!row.IsNewRow && row.Cells["Column1"].Value != null && row.Cells["Column1"].Value.ToString().Contains(targetText)) {
row.Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
break; // remove this if you want to select all the rows that contain the text
}
}
}
}