CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2010
    Posts
    11

    DatagridView search

    I have a datagridview that is not connected to a data set.

    the assembly I am working with is the iControl for F5 systems, with it i am able to capture server IPs, names, and statuses into a struct that i created.

    now, what I am trying to do is write a method to search the datagridview's text row by column and hide (x.visible = false) the rows where the data is not found.

    Or would it be easier to clear the grid, search my stuct and insert the results? Unfortunately I am trying to incorporate speed of the search into it as we are in an enterprise where there could literally be a thousand nodes. a global refresh could take a while.

    I have been searching online and am only able to find where people use the DGV for connections to a SQL Database(in which case searching is easy).

    any help would be fantastic.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: DatagridView search

    You can't hide a column. Just set the value to NULL.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2010
    Posts
    11

    Re: DatagridView search

    figured it out. this is for a DataGridView with 2 columns, granted you will have to refresh the entire view to get the entire list back but it is a start. It searches the entire grid cell by cell looking for the text string you put in.

    for more columns just add more string arrays or tell it what fields you want via index.

    private void button2_Click(object sender, EventArgs e)
    {
    string[] results1 = new string[dataGridView1.Rows.Count];
    string[] results2 = new string[dataGridView1.Rows.Count];
    for (int j = 0; j < dataGridView1.Rows.Count; j++)
    {
    for (int k = 0; k < dataGridView1.ColumnCount; k++)
    {
    if (dataGridView1.Rows[j].Cells[k].Value.ToString().Contains(textBox3.Text.ToString()))
    {
    MessageBox.Show(j.ToString());
    results1[l] = dataGridView1.Rows[j].Cells[0].Value.ToString();
    results2[l] = dataGridView1.Rows[j].Cells[1].Value.ToString();
    l++;
    }
    }
    }
    if (l > 0)
    {
    dataGridView1.Rows.Clear();
    for (int p = 0; p < l; p++)
    {
    MessageBox.Show(p.ToString());
    dataGridView1.Rows.Insert(p, results1[p], results2[p]);
    }
    }
    else
    {
    MessageBox.Show("No Results!");
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured