By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jan 12 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <bd*****@sgiims.com> wrote in message
news:Os**************@TK2MSFTNGP11.phx.gbl...
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

Jan 12 '06 #2

P: n/a
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 [MVP]" <vb***@bellsouth.net> wrote in message
news:OY*************@TK2MSFTNGP10.phx.gbl...
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" <bd*****@sgiims.com> wrote in message
news:Os**************@TK2MSFTNGP11.phx.gbl...
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


Jan 13 '06 #3

P: n/a
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 [MVP]" wrote:
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" <bd*****@sgiims.com> wrote in message
news:Os**************@TK2MSFTNGP11.phx.gbl...
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


Mar 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.