Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clear all rows from a datagridview

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello,

I have a datagridview and each time it is filled I would like to click a button and clear all the rows from it, so that it will display nothing.

what is the best method to do this?

Many thanks in advance,

Steve
 
Hi,

This works but i would like to keep the headers, this makes the whole grid blank.

Code:
Me.dgvComponets_equipment.DataSource = Nothing
Me.dgvComponets_equipment.Rows.Clear()

Many thanks,

Steve
 
If you want to keep the headers, you need to fill the datagridview with an empty datasource.

Dim dt As Datatable
Dim da as SqlDataAdapter
Dim Conn As SqlConnection

'code here for connection

'note: the where clause (in red) ensures that no data are returned, but you will get the tables columns.
da = New SqlDataAdapter("Select * from MyTable [red]where 1=0[/red]", Conn)

da.fill(dt)

Me.dgvComponets_equipment.DataSource = dt




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Thanks,

What i have decided to do is clear the source data.

Thanks for your input,

Steve
 
To add my $0.02, I use the following:

Code:
        For i As Int32 = 0 To Me.DataGridView1.Rows.Count - 2
            Me.DataGridView1.Rows.RemoveAt(0)
        Next

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Wouldn't this work...

DataGridView1.Rows.Clear()

Works for me :)
 
Hello,

To add another $0.02 this is a better way to do the for loop.

Code:
for each dr as datagridviewrow in me.dgvMyDatagrid
my.dgvMyDataGrid.rows.remove(rd)
next dr

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top