Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# NET
my datagridview is present in one windows form can i search datagrideview rows using another form ,like name wise,email wise etc
Posted 22-Jan-13 0:50am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this :
on search textbox onchange event :
DataTable tbl = (DataTable)grd.DataSource;
DataView dv = tbl.DefaultView;
dv.RowFilter = "Name Like '" + txtSearch.Text + "%'";
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

This is how I search my DGV per row:
int rowIndex = -1;
bool found = false; 
dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    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; 
    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.

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

  Print Answers RSS
0 OriginalGriff 315
1 Sergey Alexandrovich Kryukov 201
2 ChauhanAjay 156
3 PhilLenoir 154
4 ClimerChinna 140
0 Sergey Alexandrovich Kryukov 6,140
1 OriginalGriff 5,195
2 CPallini 2,473
3 Richard MacCutchan 1,607
4 Abhinav S 1,505

Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100