Clear datagridview

How can I clear a datagridview, delete all rows and columns ?

Thanks
tora111Asked:
Who is Participating?
 
bruintjeConnect With a Mentor Commented:
this will delete the rows and columns

        Dim rw As DataGridViewRow, cl As DataGridViewColumn
        For Each rw In DataGridView1.Rows
            DataGridView1.Rows.Remove(rw)
        Next
        For Each cl In DataGridView1.Columns
            DataGridView1.Columns.Remove(cl)
        Next
0
 
bruintjeCommented:
Hi tora111,

assuming you mean to erase the data not the structure you can try
DataGridView1.Rows.Clear()

hope this helps a bit
bruintje
0
 
tora111Author Commented:
bruintje, no both data and structure.

Because I created the structure at run-time by user-input, so I need to re-create the grid if user change input.

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
maidinhtaiCommented:
Me.DataGridView1.Rows.Clear() 'Delete all rows in the datagridview1
'or
Me.DataGridView1.Columns.Clear() 'Delelte all rows and columns in the datagridview1
0
 
tora111Author Commented:
Thanks bruintje .
0
 
txghia58Commented:
I know this is probably old but wouldn't just clearing the datasource be better?
DataGridView1.DataSource = null;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.