473,441 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

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

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
3 17204
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mandar B | last post by:
Hi, I would like to display a Checkbox and a string value in each cell of DataGrid. I am trying to bind it with Custom user control consist of a Textbox and a check box. The size of DataGrid...
0
by: Steve | last post by:
I am looking for an example of a custom user control ( 2 labels and 2 datetime pickers ) in a datagridview cell. Every example I have come across handles only once control such as a datetimepicker...
0
by: stevenhaochen | last post by:
I add a user control (a simple textbox control) to customize datagridview column to test how to add user control to datagridview. I found a weired behavior. I can not input "q", "!", "#". But I...
0
by: cranley | last post by:
Hi. I'm currently building a custom control. On my custom control is a colleciton property which I wish to edit with the VS.NET collection editor. So far I have everything working as planned,...
0
by: Mike | last post by:
Hey everyone... I've got three problems with a custom DataGridView column I've built following the "How To: Host Controls in Windows Forms DataGridView Cells" article. The base editing control...
1
by: lbelkova | last post by:
Hello, I've created a custom DataGridViewColumn. Everything work well, except for some reason the column doesn't accept some of the chars: "q", "." and "'". Did anybody have a similar problem?...
1
by: dbrunellemdil | last post by:
Hi, I have a control that is basically an 'enhanced' version of the date time picker. It consist of a masked text box with a date time picker, where we can only see the button to open the...
2
by: DaveL | last post by:
Hello, I have a Custome Control which contains TextBox Button DataGridView design is Textbox , button When a user clicks the button i resize the custom control canvasarea to expose the...
0
by: jchaturv | last post by:
All, I have a customized DataGridView where I have implemented extra functionality and bounded datasource in the same custom DataGridView (Using C# and .NET). Now, I could able to use it...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.