Some background information.
In my situation, My datagridview is bound to the datamember View in a
dataset. As you remember, I am responding to a cell change in another
control on the form. The cell change is referring to a different tables from
which I will rebuild the View and then present it in the data grid view.
The view may have a totally different number of columns and column headings.
So I rebuild my View using the new table name. And then I want to
re-populate the data grid view with the data in the newly built view (which
was bounded to a data adapter).
So I tried your suggestion which sounded hopeful..
When I set the datasource to null the data clears datagridview. hen I re-set
the datasource to the View which has not been populated with data from a
different table. It shows data appended to it. I was hoping that data souce
null would also remove the data from the view which it didn't. I combined
your sugesstion with using a datagridview.columns.clear. I not sure that
this really is what I want. It remove the datagridview from the form panel.
However, I think the data is still in the local data set memory. At this
moment I went to SQL Server to verify that the new view as built and resides
in SQL Server Database. And it Does..
So I'm thinking that the data is cached in the client.
Conclusion:
So setting the dataSource to Null isn't clearing the data from the data set
cached on the client. Its just breaking the connection. When I reestablish
the dataSource to the new view. It shows me data from the previous
connection along with data from the new connection.
What IF: I haven't tried to change the data view structure until I'm sure
the dataset is clear. Changing the structure with existing data in the data
member might lead to data type conflicts. I want to make sure the data
member has actually disposed of the previous data.
Statement of the Goal:
Clear the data grid view.
Generate a new View in the DataSet.
Reconnect the data grid view to the data member (view)
Make the data grid view display the new data and structure.
Example:
1. Display the Customer Data in DGV the first time.
2. Display the Employee Data the second time.
3. Display the Sales Data the third time
etc. etc.
Thanks for your help
Hope to hear from you soon.
Setting the DataSource to null will not change data. It will simply remove
the association (binding) between the DataGridView and the DataTable. If by
"View" you're referring to a DataView then what you really want to do is
rebuild the underlying DataTable. A DataView is simply a customizable view
into a DataTable. So creating a new view of the same table will not remove
data from the DataTable either. It sounds like you're handing the same
DataTable back to the DataAdapter and it is simply adding more records from
the database.
So to clear a bound DataGridView set the DataSource property to null. This
will cause the DataGridView to lose its connection to the data source and
update to show no rows. At that point it sounds like you'll want to create a
new DataTable (not a new DataView) and populate it with data from the
database. Then bind the DataTable, or a DataView of this DataTable, to the
DataSource of the DataGridView. The DataGridView will then update with the
new structure and data.

Signature
Tim Wilson
.NET Compact Framework MVP
> Some background information.
> In my situation, My datagridview is bound to the datamember View in a
[quoted text clipped - 43 lines]
> Thanks for your help
> Hope to hear from you soon.
zack - 31 Mar 2006 18:14 GMT
Thanks,
Your were right. I created a data table instead of view.
I was able to move on and understand better how the technology works.
Keng Chew - 07 Oct 2006 09:04 GMT
Hi Tim,
I have a problem just like Zack does. Supposed, I would like to clear
the structure and data on a datagridview and to display a table of
another source on it. Could you please advise what is the proper
statement to work it out? Thanks.
Keng
Truong Hanh Phuc - 30 Nov 2006 14:04 GMT
Could you please give me some example code about what you said
above. I also have some problem with clear the data of Datagridview in
C#. When i fill new dataset into datagriview, it show even my previous
data i filled before.
Nixon Morales - 25 Jul 2007 22:07 GMT
datagridview.datasource = ""
?Esta no seria la forma mas facil, rapida y sin gasto de recursos?.
Pregunto nada mas pues con 2005 estoy recien iniciando.