0

I want to make an object that represents a cell in DataGridView, containing a label, combo box, and a text box, and fill the cells in my table with these with different values.

I understand how to make a custom cell, but every article or tutorial I've seen is about only one control type per cell, and also very confusing for me.

I read this - DataGridView Control (Windows Forms)

But just cant figure it out..

So is there any way to make this kind of object?

Afsdw2
  • 3
  • 5
  • Do you want to have all these controls in a single cell? Do they show/edit the same value? – P. Kouvarakis May 24 '16 at 07:30
  • for example, the label should show a name, the combobox should show a list of name to pick from, and the text box will contain a phone number. whenever the user picks a name from the combobox, the label and textbox will change according to the name he picked (from a database of course). and yes all in one cell – Afsdw2 May 24 '16 at 07:37
  • It's unclear what is your requirement exactly. But if you need to show `Label`, `TextBox` and `ComboBox` in `DataGridView`, you don't need to create a custom column type. You can use a readonly `DataGridViewTextBoxColumn` as label, use `DataGridViewTextBoxColumn` as textbox and `DataGridViewComboBoxColumn` as combobox. – Reza Aghaei May 24 '16 at 07:54
  • Take a look at this question and its answer: [Displaying a collection of controls in Windows Forms](http://stackoverflow.com/questions/32759540/displaying-a-collection-of-controls-in-windows-forms) – Reza Aghaei May 24 '16 at 07:55
  • well I need a way to get all of these into 1 cell.. – Afsdw2 May 24 '16 at 07:58
  • You can take a look at [How to add a UserControl to a DataGridView in VB.net, and have the control always showing?](http://stackoverflow.com/questions/32280673/how-to-add-a-usercontrol-to-a-datagridview-in-vb-net-and-have-the-control-alway/32281605#32281605). – Reza Aghaei May 24 '16 at 08:02
  • creating a custom control seems like a good idea. but thank you for your comment as these answers could help me understand better the proccess of creating a custom cell – Afsdw2 May 24 '16 at 08:06

2 Answers2

0

You could try making user control with required controls and placing it inside DGV cell

solujic
  • 924
  • 1
  • 18
  • 43
0

If I understand correctly you want your cells to show a Name and Phone. Since every cell can have a single value you have to wrap the Name and Phone into a single object (e.g. Contact) and the bound datasource property should be of that type.

You don't actually need all three controls to display in a cell. when not in edit mode you need two labels (one for Name and one for Phone). In edit mode you need a combobox and a label (or textbox if you want phone to be editable - but that could be tricky).

Then you want to create a custom DataGridViewCell, custom DataGridViewColumn and a custom editing control.

Although you could add two labels as children of your custom DataGridViewCell, it is much better to simply override Paint and draw the Name and Phone directly on the cell surface.

The custom editing control can be any control that implements the IDataGridViewEditingControl interface. As such it can be a composite control containing a combobox and a label (or textbox). The composite control should edit the Contact object as a single unit.

Also the custom DataGridViewColumn is responsible for providing the datasource for the dropdown part of the editing control.

https://msdn.microsoft.com/en-us/library/7fb61s43(v=vs.110).aspx describes how to create a custom cell and column.

https://msdn.microsoft.com/en-us/library/7tas5c80(v=vs.110).aspx describes how to create a custom editing control and how to instantiate it from the custom cell.

P. Kouvarakis
  • 1,893
  • 12
  • 21