I want the user to be able to search for a number in a column in a DataGridView (dgv). Dgv can store many records. Each entry has a project number. Therefore, I want the user to be able to find the project number in the Project Number column. I have columns: ProjectID (not visible); Image (no title); Project number; Project name; Company; Contact.
Here is my code:
private void btnSearch_Click(object sender, EventArgs e) { string searchValue = textBox1.Text; int rowIndex = -1; dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect; try { foreach (DataGridViewRow row in dgvProjects.Rows) { if (row.Cells[row.Index].Value.ToString().Equals(searchValue)) { rowIndex = row.Index; dgvProjects.Rows[row.Index].Selected = true; break; } } } catch (Exception exc) { MessageBox.Show(exc.Message); } }
Problem number 1:. What she is doing so far: the user enters the project number in TextBox1. When he / she presses the button, the code searches for this line in the lines, and when the project number is found, this line is selected. It works great, but only once. When I want to find another project number, nothing happens.
Problem # 2: I think this can be done better by looking up values ββonly for the Project Name column. But how do I do this properly?
The code I used to search is derived from this answer
source share