This is how I search my DGV per row:
int rowIndex = -1;
bool found = false;
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
try
{
int searchValue = int.Parse(tbSearchValue.Text);
foreach (DataGridViewRow row in dgvProjects.Rows)
{
int compareValue = int.Parse(row.Cells[2].Value.ToString());
if (compareValue.Equals(searchValue))
{
found = true;
rowIndex = row.Index;
dgvProjects.Rows[row.Index].Selected = true;
dgvProjects.FirstDisplayedScrollingRowIndex = rowIndex;
break;
}
}
if (!found)
MessageBox.Show("Project number not found.\n\nBe sure you're searching for the right project number.", "Project not found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (FormatException)
{
MessageBox.Show("Your input is not a number.", "Input Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
In this line:
int compareValue = int.Parse(row.Cells[2].Value.ToString());
row.Cells[2].Value
stands for the second column of my DGV. I wanted my program to match the text in the textbox with the text in the second column.