Question

I have datagridview on my winform and you can add and delete items from it. Although I am able to do that successfully, it acts weird.

It requires that its property RowCount be set to 1 or greater. Thus, there is always a single row of grid displayed at all time even when there is no item added to it.

How do you overcome this shortcoming? or Is there a way around it?

Thanks,

Was it helpful?

Solution

This is by design if you have AllowUserToAddRows set to true.

From MSDN, DataGridView.RowCount Property:

If AllowUserToAddRows is true, you cannot set RowCount to 0. In this case, call the DataGridViewRowCollection.Clear method to remove all rows except the row for new records. Calling Clear has the same result as setting RowCount to 1 in this case, but is much faster.

When you want to set your RowCount to 0 you can set AllowUserToAddRows to false and then back to true when you want to allow your users to add rows.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top