Sorry in advance. I'm not clear on what you are trying to do. So I made a genaric search for the datagrid, and may not be what you are looking for.
Sample (this will search (loop) all cells in the datagrid and return the first match):
private void TextBox_KeyDown(object sender, KeyEventArgs e) {
if (sender == textBox1) {
if (e.KeyCode == Keys.Enter) {
if (textBox1.Text != string.Empty || textBox1.Text != "") {
DataGridCell cell = SearchRecords(textBox1.Text);
if(cell.ColumnNumber !=-1 && cell.RowNumber!= -1)
FocusOnEntry(cell);
}
}
}
}
private DataGridCell SearchRecords(object data) {
int c = dataGridView1.Columns.Count;
int r = dataGridView1.Rows.Count;
if (c != 0 && r != 0) {
for (int col = 0; col < c; col++) {
for (int row = 0; row < r; row++) {
DataGridViewCell cell = dataGridView1[col, row];
if (cell.Value != null && cell.Value.ToString() == data.ToString())
return new DataGridCell(row, col);
}
}
}
return new DataGridCell(-1, -1);
}
private void FocusOnEntry(DataGridCell index) {
dataGridView1.Rows[index.RowNumber].Selected = true;
dataGridView1.Focus();
}
Let me know if this is what you are looking for.