Click here to Skip to main content
15,946,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want the user to be able to search for a number in a column in the DataGridView (dgv). The dgv can hold many records. Each record has a Project Number. So I want the user to be able to search for a project number in column Project Number. The columns I have are: ProjectID(not visible); Image(no headertext); Project Number; Project Name; Company; Contact.

Here is my code:
C#
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);
    }
}

What it does so far: The user types the project number in TextBox1. When he/she clicks the button, the code searches for this string in the rows, and when found the project number, that row gets selected. It works fine, but only once. When I want to search for an other project number, nothing happens. That's problem #1.

Problem #2: I think this can be done in a better way, by searching the values for column Project Name only. But how should I do this properly?

The code I used to search comes from this answer[^].
Posted
Updated 18-Aug-22 3:28am

try this for the filter but not sure for the search.


private void txtsearch_TextChanged(object sender, EventArgs e)
{



DataView dt = ds.Tables[0].DefaultView;
string tbcolumn;
try
{

tbcolumn = dataGridView1.Columns[dataGridView1 .CurrentCell.ColumnIndex ].HeaderText.ToString();
dt.RowFilter = string.Format("" + tbcolumn + " ='{0}'", txtsearch.Text);
dataGridView1.DataSource = dt;

}
 
Share this answer
 
I needed to change row.Cells[row.Index] to row.Cells[2] where 2 is index of the column:

C#
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[2].Value.ToString().Equals(searchValue))
            {
                rowIndex = row.Index;
                dgvProjects.Rows[row.Index].Selected = true;
                break;
            }
        }
    }
    catch (Exception exc)
    {
        MessageBox.Show(exc.Message);
    }
}


I needed to specify the index of the column I need to search, but, back then I didn't knew exactly how to do that.

This little change fixed both problems.
 
Share this answer
 
I think you can make it a lot easier. I would also suppress the need for a button and just use the Key Down event handler

private void txtLocateItem_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        foreach(DataGridViewRow row in dgvItemSaleTotals.Rows)
        {
            if (row.Cells["Item"].Value.ToString().StartsWith(txtLocateItem.Text.Trim()))
            {
                dgvItemSaleTotals.FirstDisplayedScrollingRowIndex = row.Index;
                break;
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900