0
votes

I want to make a search list using datagridview in c#. I have a text box where one can enter the customer name. When the "search" button is clicked, i will hide all the rows where the CustomerName is not equal to the searchValue. The problem is that when the user want to search for the second time, how can I search through the hidden rows of the dgv. Do i need to make visible the rows hidden. Thanks.

Here is the Code.

   private void search_Click(object sender, EventArgs e)
    {
         arrayList= new ArrayList();
        String searchValue = searchBox.Text;
        for (int i = 0; i < dataGridView1.RowCount-1; i++)
        {
            if ( (dataGridView1.Rows[i].Cells[1].Value.ToString().Trim() ).Equals (searchValue.Trim()) )
            {
                //dataGridView1.Visible = true;
                arrayList.Add(i);
            }
            else
            {
                dataGridView1.Visible = false;
            }
        }
        dataGridView1.Refresh();

    }
1
Why do you need to consider about Front end, when you are filtering from a List? - Sajeetharan
Actually the user wants to select a customer's name and Id when an item is to be hired by the customer. So i want to get all the customers from the DB into the datagridview. - Zuhran
Wait, iam going to add the code here. - Zuhran
Sorry, i don't Know how to add code here. New user of this site. Can you help me please. - Zuhran
Edit your post and paste the code inside. - Sajeetharan

1 Answers

0
votes

Are you using a datatable to bind the datagridview? If you are, when you are searching you could iterate the datatable and delete the rows that do not match your search criteria. When you search you can rejectchanges on your datatable to have the original data again. Afterwards you can use the same process that was used in the first place (mark the datarows that do not match the search criteria as deleted).

Another alternative can be using a DataRowView and filter with your search criteria and use the that datarowview to bind the datagridview. I would prefer this last one.