Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why i cannot clear the rows in the datagridview control?

I was doing this test, I need to re-load the datagridview with data every 4 seconds and the data was coming from a database.

so i'v created a timer control by code and added an event handler to the tick event. Then in the tick event

void t1_Tick(object sender, EventArgs e)
{

    dataGridView1.DataSource = null;
    dataGridView1.Rows.Clear();
    dt = Product.GetAllProductsBasicInfo();
    dataGridView1.DataSource = dt;

}

above code works but when I move

dataGridView1.Rows.Clear();

before

dataGridView1.DataSource = null;

it will throw a run time error saying the rows cannot be cleared, I want to know why it throws this error, typically Clear() clears the datagridview?

thanks

like image 744
sniff_bits Avatar asked Jan 31 '14 21:01

sniff_bits


People also ask

How delete all Datagridview?

I required it to clear all rows of datagridview on button click event. dataGridView1. Rows. Clear();

How check Datagridview row is empty C#?

if datagridview row is empty then error must be show. User can't leave datagridview until insert all the data in it.

How many rows can Datagridview handle?

Max value, i.e. 2,147,483,647. The DataGridView's RowCount cannot hold a number more than this because it's an integer. So, you can possibly say that limit for datagridview rows is 2 Billion.

What is the use of Datagridview control?

The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data.


1 Answers

typically Clear() clears the datagridview?

Yes, unless it has a DataSource, which in your case, it does.

So try clearing the source of the data instead:

dt.Rows.Clear();
like image 193
LarsTech Avatar answered Oct 13 '22 04:10

LarsTech