0
votes

Ok so I'm trying to make a simple SQL CE Viewer application just to view my local databases for another application, I have it setup so that I can select what database I want to open and then it automatically populates a combo box with all of the tables in the database. When I select a table it populates a DataGridView with the records in the table.

My problem is switching between tables. I can't seem to get the DataGridView to remove everything from the previous table and re-populate the DataGridView with the new table information. Of course each table has different columns and rows and such.

I've googled and searched on here and every suggestion I find doesn't seem to work. It populates the DataGridView with the first table just fine, but when I select another it basically adds the columns and rows into whatever was there....

How can I get the DataGridView to completely clear for new data?

And please don't tell me to use dataGridView1.DataSource = null; tried that, doesn't work.

3

3 Answers

1
votes

Well, I checked it in one of my programs and just changing DataSource property is working fine. I didn't have to use datagridview.Refresh(). Maybe it depends on the kind of DataSource, which you are using to set datagridview data?

1
votes

Write this line after you have done populating the DataSource with the new data

dataGridView1.Refresh();

You don't need to clear the DataGridView directly. Its always handled by modifying the DataSource of the DataGridView.

0
votes

If there is nothing else in the form, you can simply call InitializeComponent() again. But I think @Josh is right.