Home All Groups Group Topic Archive Search About

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

Author
12 Jan 2006 6:23 PM
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

Author
12 Jan 2006 10:11 PM
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
--------------------------
Show quoteHide quote
"Bob" <bduf***@sgiims.com> wrote in message
news:OsGgEV6FGHA.2708@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
>
Author
13 Jan 2006 5:27 PM
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

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OYF8jU8FGHA.916@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" <bduf***@sgiims.com> wrote in message
> news:OsGgEV6FGHA.2708@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
>>
>
>
Author
6 Mar 2006 7:22 PM
ReinierG
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.

Show quoteHide quote
"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" <bduf***@sgiims.com> wrote in message
> news:OsGgEV6FGHA.2708@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
> >
>
>
>