How can you add a custom user control to a datagridview?

How can you add a custom user control to a datagridview?

Post by Bob » Sat, 14 Jan 2006 03:23:18

Thinking two things,
1- Creating a userControl -yeah you guessed it, a multi column drop down
combobox - I've looked at several articles and did not find what I need, one
that's bindable and that I can use in a datagrid view.
2- Extending the datagridview so that it can be added to the list in the
column types when editing columns in the datagridview.
I'm really getting PO'd at Microsoft for not including a multi column
combobox both a the standard combox and as a datagridviewcombobox. Its
something so essential and they've been doing that in Access for years. We
had the capability in VB6, what the hell are we paying these overinflated
prices for development tools for?
Maybe I'm trying to reinvent the wheel?

Anyways, any ideas would be appreciated.


How can you add a custom user control to a datagridview?

Post by Ken Tucker » Sat, 14 Jan 2006 07:11:24


Here is a real simple example. It loads the order details and
product tables from the northwind database. I display the order details in
the datagrid view. In the ProductID column I show a combobox with the
product name and price. I added a new column to my dataset which is an
expression of ProductName and Price columns added together. Hope this

Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim da, daProducts As SqlDataAdapter
Dim conn As SqlConnection
Dim ds As New DataSet

strConn = "Server = .;Database = NorthWind; Integrated Security =
conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * from [Order Details]", conn)
daProducts = New SqlDataAdapter("Select * from Products", conn)

da.Fill(ds, "Orders")
daProducts.Fill(ds, "Products")

DataGridView1.DataSource = ds.Tables("Orders")


Dim dc As New DataColumn("MultiCols")
With dc
.Expression = "ProductName + ' ' + UnitPrice"
End With


Dim dgvCombo As New DataGridViewComboBoxColumn
With dgvCombo
.Width = 150
.DataSource = ds.Tables("Products")
.DisplayMember = "MultiCols"
.ValueMember = "ProductID"
.DataPropertyName = "ProductID"
.HeaderText = "Product"
End With

End Sub
End Class



How can you add a custom user control to a datagridview?

Post by Bob » Sun, 15 Jan 2006 02:27:35

en, thanks for your suggestion,
I had done something similar before by adding a composite column in the SQL
statement that creates the dataset that is used to populate the
dgviewcombobox and setting the displayvalue to that composite column.
However what happens is that the composite column code and name gets
displayed in the textbox of the datagridview. That is not what's required.
In the example that you give I would only want the product code to display
in the datagridview textboxcell AND one or more columns with headers in the
dropdown list. I know the C1 controls do this I've used them for VS 2003.
But I had some issues with them and am trying to roll my own.

Thanks for your help, its really appreciated.

"Ken Tucker [MVP]" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...


How can you add a custom user control to a datagridview?

Post by UmVpbmllck » Wed, 08 Mar 2006 04:22:48

i Ken,

I liked the simplicity of your code example, and I have a small question
that is plaging me for about 4 days in a row now... : Can you make this
example work if you create your own tabeldef and the fill that table with the
right info?
I do so, and keep getting display problems (with the databound combobox).
whereas if I use a dataset from the SQL DB, it works fine (but I can't use
that here).
Can you give me a pointer in the right direction ?


"Ken Tucker [MVP]" wrote: