I made a usercontrol that is basically a panel with a bunch of labels, buttons, and Event Handlers on it. I want to be able to populate a DataGridView with these controls.
I have followed the instructions in this MSDN article, and have created a DataGridViewColumn and a DataGridViewCell class to associate with my Control.
The issue I am having is that the control is not rendering. When I run the code at the link above, It shows the date in the cell, and the user control only shows when the cell is being edited. What I'd like to have happen is that the control is always visible in the cell, similar to the behavior in a DataGridViewImageColumn.
How can I force the DataGridView to always show the control?
The main idea:
My sample control with name of SomeControl, contains a label and a text box and a property SomeProperty and an event SomePropertyChanges. I show cell value in TextBox of my custom control.
You can do anything with your custom control containing any control and any type of properties and events.
Here is vb-like pseudo code.
'This is not VB, this is vb-like pseudo code Private Sub Form_Load(sender as object , e as EventArgs) 'Get data 'Show data in grid 'For each row For Each row as DataGridViewRow in this.categoryDataGridView.Rows 'Create an instance of control Dim myControl as New SomeControl() 'Set properties and register event handlers myControl.SomeProperty = row.Cells.Value myControl.SomePropertyChanged += myControl_SomePropertyChanged 'Make it invisible myControl.Visible = False 'Set tag of your wanted cell to control row.Cells.Tag = myControl 'Add control to Controls collection of grid this.categoryDataGridView.Controls.Add(myControl) Next End Sub Private Sub myControl_SomePropertyChanged(sender as object, e as EventArgs) 'event handler of SomePropertyChanged for custom control 'do stuff here End Sub Private Sub dataGridView_CellPainting(sender as object, e as DataGridViewCellPaintingEventArgs ) 'for each row For i as Integer=0 To dataGridView.RowCount- 1 'Extract control from tag of your wanted cell var myControl= this.dataGridView.Rows[i].Cells.Tag as SomeControl 'Get cell rectangle Dim cellRectangle As Rectangle= dataGridView.GetCellDisplayRectangle(2, i, True) 'Set location myControl.Location = New Point(cellRectangle.X, cellRectangle.Y ) 'Set size myControl.Size = New Size(cellRectangle.Width - 1, cellRectangle.Height - 1) 'Make visible myControl.Visible = True Next }