Cannot add rows when the DataGridView control is in virtual mode?
When I try to add a new row to a DataGridView in virtual mode like this:
dataGridViewX1开发者_运维知识库.Rows.Add(new string[] { "Parker", "Seattle" });
The following exception occurs:
Operation is not valid when the DataGridView control is in virtual mode?
When a DataGridView
is in Virtual mode (DataGridView.VirtualMode = true
), that means that the DataGridView
's content is supplied from a source other than itself through events exposed on the DataGridView
. If you intend for your DataGridView to be in virtual mode, you need to add the new information to the underlying source and then tell the DataGridView
that there is another row, like this:
// add information to the underlying source
this.dataGridView.RowCount++;
If you did not intend to use DataGridView
in virtual mode, you can turn off virtual mode by setting DataGridView.VirtualMode
to false
.
You need to use data source to add new row and it will show in DataGridView automatically.
You might have done all what the other questions recommend, but set YourDataGridView.DataSource to something somewhere.
It must not be set in virtual, otherwise you will get a similar message when you try to set RowCount, and get exactly that message when you try to add .
If you "mis-used" DataSource until now as a "mailbox" instance variable that your event handlers can use to address the datasource, consider stuffing it into the Tag property, and using the reference you need from that property (using an appropriate type cast).
精彩评论