0

I load some data into a datagridview, I use a loop to do that.

When I need to load new data I get rid of old data like so...

foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    dataGridView1.Rows.Remove(row); ;
                }

Now I try to add new rows, because new data might different number of rows and columns, but the control does not show as if it's just gone.
Does anyone know what I might be doing wrong?

2
Contributors
3
Replies
20
Views
2 Years
Discussion Span
Last Post by Suzie999
0

I had dataGridView1.Dispose()
I removed that, and now the new data is being input but the old columns are still present too.

(edit)
I tried adding

foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    dataGridView1.Columns.Remove(column);
                }

But get the following error
Collection was modified; enumeration operation may not execute.

Edited by Suzie999

1

Rows and Columns are collections in datagridview control. Both collections have Clear method:

    dataGridView1.Rows.Clear();
    dataGridView1.Columns.Clear();

Now you should have an "empty" datagridview control.

HTH

0

Thanks Teme64, I did see the Clear method but used it wrong much like I did with the above Remove method.

Your explanation did indeed help, it is exactly what I needed.
Much appreciated, and solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.