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

B

Bob

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.

Bob
 
K

Ken Tucker [MVP]

Hi,

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
helps.


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 =
SSPI;"
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")

DataGridView1.Columns.Remove("ProductID")

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

ds.Tables("Products").Columns.Add(dc)

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

DataGridView1.Columns.Add(dgvCombo)
End Sub
End Class


Ken
 
B

Bob

Ken, 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.
Bob

Ken Tucker said:
Hi,

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
helps.


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 =
SSPI;"
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")

DataGridView1.Columns.Remove("ProductID")

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

ds.Tables("Products").Columns.Add(dc)

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

DataGridView1.Columns.Add(dgvCombo)
End Sub
End Class


Ken
--------------------------
Bob said:
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.

Bob
 
G

Guest

Hi 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 ?

Thanks.

Ken Tucker said:
Hi,

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
helps.


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 =
SSPI;"
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")

DataGridView1.Columns.Remove("ProductID")

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

ds.Tables("Products").Columns.Add(dc)

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

DataGridView1.Columns.Add(dgvCombo)
End Sub
End Class


Ken
--------------------------
Bob said:
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.

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top