Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my datagridview is present in one windows form can i search datagrideview rows using another form ,like name wise,email wise etc
Posted

This is how I search my DGV per row:
C#
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:
C#
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.
 
Share this answer
 
v2
Try this :

on search textbox onchange event :

C#
DataTable tbl = (DataTable)grd.DataSource;
DataView dv = tbl.DefaultView;

dv.RowFilter = "Name Like '" + txtSearch.Text + "%'";
 
Share this answer
 
You can do that using delegate and then use following ways:
Way 1.
if your DataGridView is using DataTable or a DataView then
Create a BindingSource and make BindingSource.DataSource set your DataGridView.DataSource to the BindingSource.
now you can use BindingSource.Filter to filter as per query.

Way 2.
first collect DataTable.DefaultView in DAtaView object then Use DataView.RowFilter. apply filter as per query.
 
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