Hi Guys, I hope you can help me out...

I have a slight issue in where the 'Search Box' Code I currently have seems to sift through the Populated Data Grid View fine. However, when selecting an Account that has been filtered by the use off the 'Search Box' it correctly populates the TextBoxes but when clearing the 'Search Box' it then removes the data in the Grid view below which I don't want.

My question is, How do I stop this from happening? I want to keep the DGV Populated to re-use again. I have attached screenshots of the program.

Capture1.jpg - Shows the Populated DGV with a Customer Search Box above.
Capture2.jpg - Filtering the list to show the perferred account.
Capture3.jpg - Clicking on the Filtered Account which in turn populates the Text Boxes on the left.
Capture4.jpg - When 'Backspacing the data in the search box' the DGV below is empty?

Below is the Code Block Currently Being used (Beginner here writing code...)

    private void textBox2_TextChanged(object sender, EventArgs e)
        {

            //Below Searches on the Cash Accounts DataSet for Matching Accounts.
            if (radioButton1.Checked == true)
            {
                DataView DV = new DataView();

                DV.Table = ds.Tables["cashCustomers"];
                DV.RowFilter = "CashAccRef LIKE '%" + textBox2.Text + "%' OR CashName LIKE '%" + textBox2.Text + "%' ";
                dataGridView1.DataSource = DV;
            }
        }

Hope you guys can help and this is driving me nuts!

Thanks in advance! I've learnt a lot from DaniWeb.

Hi Guys...

Slight update on this... The Above code acutally seems to work?! Oddly..

However, the code below does not. (I have 2 differnet connection types.. 1 Links to Sage Line 50 and the other connects to a MDB Database). These are selected depending on the radio button selected and the DataSet Populated.

  private void textBox2_TextChanged(object sender, EventArgs e)
        {

            //Below Searches on the Cash Accounts DataSet for Matching Accounts.
            if (radioButton1.Checked == true)
            {
                DataView DV = new DataView();

                DV.Table = ds.Tables["cashCustomers"];
                DV.RowFilter = "CashAccRef LIKE '%" + textBox2.Text + "%' OR CashName LIKE '%" + textBox2.Text + "%' ";
                dataGridView1.DataSource = DV;
            }

            //Below Searches on the Line50 Accounts DataSet for Matching Accounts.
            else if (radioButton2.Checked == true)
            {
                DataView DV = new DataView();

                DV.Table = ds4.Tables["ACCOUNTS"];
                DV.RowFilter = "ACCOUNT LIKE '%" + textBox2.Text + "%' OR NAME  LIKE '%" + textBox2.Text + "%' ";
                dataGridView1.DataSource = DV;
            }
            //Below Searches on the Line50 Accounts DataSet for Matching Accounts.
            else if (radioButton3.Checked == true)
            {
                DataView DV = new DataView();

                DV.Table = ds4.Tables["ACCOUNTS"];
                DV.RowFilter = "ACCOUNT LIKE '%" + textBox2.Text + "%' OR NAME  LIKE '%" + textBox2.Text + "%' ";
                dataGridView1.DataSource = DV;
            }
        }

The 'Line 50' Accounts DataSet is the one that is causing the Issue. But I cannot seem to see any difference between the code working and the code that does not work?

Thanks guys..

Try adding a breakpoint on row 10 and checking the value of your RowFilter by hovering on it a small magnifying glass should appear. Click it and paste the text into SQL server and add the SELECT * FROM TableName WHERE and check the results.

Alternatively you can try the following line of code which reads much simpler.

(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("CashAccRef LIKE '%{0}%' OR CashName LIKE '%{1}%'", textBox2.Text,textbox2.Text);

When stepping your code make sure that none of the datasets you're using still have a previous row filter applied when running the application for all IF scenarios.

Hi Fenrir() Thanks for your reply.

I'm currently stepping through it and I think the issue lies when selecting the filtered result from the DGV and it goes off a collects data from the table and populates the text fields.. It also then does a look up for any related Order Lines and populates a secondary grid view below (IF) any are found.

I THink Its to do with a DataSet I have called DS4 (Which hold account detailed pulled in from Line 50).

Looking at the Search Result that works.. it uses a DataSet called DS and that does not seem to be affected in the same way..

It's a tricky one... :S

Thought of another way around this issue.

Is there a way that I can re-populate the DGV with a DataSet when Text box is empty? and dependant on what RadioButton is selected?

You mean If (Textbox2.Text == "") ? you should be able to add that in the text changed event

Hi Fenrir()

Thank you for your help on this but I've managed to figure out what was going on.. Talk about Newbie mistakes...

I had my DataSets all other the place.. and when 1 was being filled another was being wiped on a differnet event. then it would not find the customer details..

Managed to clean them up and add a few more new ones to get around the issue.. Little bit messy but have noted all the code.

Thanks for your help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.