none
How can I hide all rows in a DataGridView, then display just the search results? RRS feed

  • Question

  • I might be going about this the wrong way, so I'm open to other methods.

    I have a DataGridView (Windows forms app) that shows three columns from all the records in a table.  I want to add a search button at the top of my form that filters the DataGridView to only show records containing the search terms, and then add another button to clear the results and "restore" the entire table.

     

    My current approach is to deselect all the rows, then set each row visible.  From there I hoped to navigate through the DataGridView and set search result rows back to visible. 

    Unfortunately, when I try to set each row invisible, I get the error: "Row associated with the currency manager's position cannot be made invisible"

    I have a feeling I'm doing this the hard way.  I've dropped my code snippet in here.  Can someone tell me the right way to do this?

     

    private void toolStripSearchButton_Click(object sender, EventArgs e)

            {

                string Searchval = toolStripSearchTextbox.Text;

              

                DataRow[] foundRows;

                foundRows = MyDataSet.Tables["tbl_TSR"].Select("TSR Like '%" + Searchval + "%' or Resolution Like '%" + Searchval + "%'");

     

                if (foundRows.Length == 0)

                    MessageBox.Show("No match found.");

                else

                {

                    foreach (DataGridViewRow dr in this.TSRdataGridView.SelectedRows)

                    {

                         this.TSRdataGridView.Rows[this.TSRdataGridView.Rows.IndexOf(dr)].Selected = false;                   

                    }

             foreach (DataGridViewRow dr in this.TSRdataGridView.Rows)

                    {

                         dr.Visible = false;                   

                    }

                    foreach (DataRow row in foundRows)

                    {

                        this.MyDataSet.tbl_TSR.DefaultView.Sort = "TSR_ID";

                        int rowIndex = this.MyDataSet.tbl_TSR.DefaultView.Find(row["TSR_ID"]);

    MessageBox.Show(row["TSR_ID"].ToString());                    

                        MessageBox.Show(rowIndex.ToString());

     

    //*** Right here I would put the code to set the row back to visible.  I haven't worked on it since

    //*** I can't seem to make the row invisible!

                    }

                }

     

            }

     

     

    • Moved by VMazurModerator Thursday, December 23, 2010 11:17 AM (From:ADO.NET Managed Providers)
    Wednesday, December 22, 2010 10:16 PM

Answers

  • DataView dataView = new DataView(MyDataSet.Tables["tbl_TSR"]);

    dataView.RowFilter = "TSR Like '%" + Searchval + "%' or Resolution Like '%" + Searchval + "%'";

    dataView.Sort="TSR_ID";

    this.TSRdataGridView.DataSource=dataView;



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Narnian Thursday, December 23, 2010 6:16 PM
    Wednesday, December 22, 2010 11:20 PM

All replies

  • DataView dataView = new DataView(MyDataSet.Tables["tbl_TSR"]);

    dataView.RowFilter = "TSR Like '%" + Searchval + "%' or Resolution Like '%" + Searchval + "%'";

    dataView.Sort="TSR_ID";

    this.TSRdataGridView.DataSource=dataView;



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by Narnian Thursday, December 23, 2010 6:16 PM
    Wednesday, December 22, 2010 11:20 PM
  • DataView dataView = new DataView(MyDataSet.Tables["tbl_TSR"]);

    dataView.RowFilter = "TSR Like '%" + Searchval + "%' or Resolution Like '%" + Searchval + "%'";

    dataView.Sort="TSR_ID";

    this.TSRdataGridView.DataSource=dataView;



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP


    I KNEW there had to be a better way.  That's much easier and much more elegant than the approach I was taking. It will also make writing the reset button a snap.   To reset the DataGridView all I need to do is set the DataSource back to the original DataSet.  Thanks for your help.  I put a variation of what you have into my code and it works perfectly. 

    Thursday, December 23, 2010 6:27 PM