Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / July 2007

Tip: Looking for answers? Try searching our database.

Clear DataGridView (VB.NET 2.0)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fulvio Giaccari (www.freeaspx.it) - 14 Feb 2006 18:16 GMT
Hi to all,
I'd like to know how I can clear the DataGridView Control.
Thanks for your support

Fulvio

Signature

==============================
Fulvio Giaccari
FreeAspx.it ASP & .NET Community
http://www.freeaspx.it
==============================

Tim Wilson - 14 Feb 2006 19:50 GMT
Try calling the Clear method on the Rows property.
Me.DataGridView1.Rows.Clear()

Signature

Tim Wilson
.NET Compact Framework MVP

> Hi to all,
> I'd like to know how I can clear the DataGridView Control.
> Thanks for your support
>
> Fulvio
Fulvio Giaccari (www.freeaspx.it) - 15 Feb 2006 08:30 GMT
Dear Tim,
thanks for your response, but doesn't function  this method.
Can you help me? Thanks for all

Fulvio
Signature

==============================
Fulvio Giaccari
FreeAspx.it ASP & .NET Community
http://www.freeaspx.it
==============================

> Try calling the Clear method on the Rows property.
> Me.DataGridView1.Rows.Clear()
[quoted text clipped - 4 lines]
> >
> > Fulvio
Tim Wilson - 15 Feb 2006 14:46 GMT
Does calling this method not clear the visual contents of the DataGridView
as you'd expect?

Signature

Tim Wilson
.NET Compact Framework MVP

> Dear Tim,
> thanks for your response, but doesn't function  this method.
[quoted text clipped - 10 lines]
> > >
> > > Fulvio
Zack - 27 Mar 2006 19:53 GMT
tim,
I would like to know how to clear a datagridview of its previous data and to
populate with data from a new query.

So I'm interested in the solution to this thread.
Tim Wilson - 28 Mar 2006 00:39 GMT
I guess it depends on how you're populating the DataGridView. If it's bound
to a data source then I would remove the binding; otherwise, if the rows
were added manually then I would just clear the rows collection.

if (this.dataGridView1.DataSource != null)
{
 this.dataGridView1.DataSource = null;
}
else
{
 this.dataGridView1.Rows.Clear();
}

Does that work in your situation?

Signature

Tim Wilson
.NET Compact Framework MVP

> tim,
> I would like to know how to clear a datagridview of its previous data and to
> populate with data from a new query.
>
> So I'm interested in the solution to this thread.
zack - 28 Mar 2006 22:05 GMT
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.
Tim Wilson - 28 Mar 2006 23:18 GMT
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.
Zack - 27 Mar 2006 19:50 GMT
I am interested in the answer to this problem.

The data in the datagridview is not clearing.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.