Why can't I clear the rows in a datagridview control?

I did this test, I need to reload the datagridview with data every 4 seconds, and the data came from the database.

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

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 runtime error saying that the rows cannot be cleared, I want to know why it throws this error, usually Clear () clears the datagridview?

thanks

+4
source share
1 answer

Does Clear () usually clear a datagridview?

, DataSource, .

, :

dt.Rows.Clear();
+9

Source: https://habr.com/ru/post/1524776/


All Articles