I developed some custom datagridview cells which contain specialized user controls, e.g. for synchronizing data between a client machine and a server containing up- and download buttons within a user control.
I have them fundamentally working .....
but I have several problems, which I think come from unsharing of rows when starting to edit a row, which seems to remove a shared cell and replace it by 2 or more new cells. As the removed cell still contains my control, this control is still visible and is "floating" around the window. I didn't yet find a reliable way to detect, when a cell is no longer needed.
The cell containing my user control is not editable itself and the column is marked as readonly. This column hosting the user control cell is using an expression defined in a typed dataset column to set it's value from the value of another cell. A typed dataset is bound to the datagridview via a binding source.
Everything works quite well as long as the datarow isn't unshared.
What is the recommended way to host user controls in a datagridview cell?
I read, that someone will prepare a white paper about custom datagridview columns. Maybe he can also give advice about this topic.
Moreover I noticed, that when overriding the SetValue method in the DataGridViewCell, only NULL values are passed in when the datagridview is populated from data of the binding source. But later the base cell contains the correct data.
There is no recommended way to do this. The DataGridView only supports hosting a user control as an editing control when a cell is in edit mode. Regarding shared rows/cells. When a row becomes unshared the grid raises the RowUnshared event. To unshare a row, the grid calls the row's clone method which in turn clones all its cells.
For a databound grid, the cells to not get their values set since the cells do not store any data -- the underlying datasource stores the data. In the SetValue and GetValue code for a cell it checks to see if the cell/column/grid is databound and if so just retrieves or sets the value to the datasource. This is why you do not get a SetValue call with values as the grid is being displayed. The cell's GetValue method will be called as the grid is being displayed, so maybe you can use that?
-mark DataGridView Program Manager Microsoft This post is provided "as-is"