views:

923

answers:

1

I have a DataGridView in virtual mode. I only implemented the CellValueNeeded eventhandler as described in http://msdn.microsoft.com/en-us/library/15a31akc.aspx.

Implementing the rest of the events only seems needed when you want to be able to edit the cells manually.

I would like to programatically edit a DataGridView cell value.

I tried this using the following code:

DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
DataGridView1.BeginEdit(false);
DataGridView1.Rows[0].Cells[0].Value = "testing new value";
//just using a random parameter here, not sure it is needed when editing programmatically
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.LeaveControl);
DataGridView1.Refresh();

but no success :(

any help would be appreciated

+4  A: 

When you use virtual mode, you provide your own logic for linking the DataGridView to the underlying data source. So, to edit a cell value, you should change the value in the underlying data source, and call Refresh to refresh the displayed value (this will cause the CellValueNeeded event to be called for all displayed cells)

Thomas Levesque
Thank you.Do you by any chance also know if there is a way to know which rows are currently displayed in virtual mode. This would give me a chance to determine if a refresh is necessary.
sjors miltenburg
You can use the FirstDisplayedScrollingColumnIndex and FirstDisplayedScrollingRowIndex properties, and the DisplayedColumnCount and DisplayedRowCount methods.
Thomas Levesque
super! this control has so many properties that sometimes its hard to find what you are looking for. Thx a lot!
sjors miltenburg