473,654 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 datagridviewcom bobox. 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 17228
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.Sql Client

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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(s trConn)

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

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

DataGridView1.D ataSource = ds.Tables("Orde rs")

DataGridView1.C olumns.Remove(" ProductID")

Dim dc As New DataColumn("Mul tiCols")
With dc
.Expression = "ProductNam e + ' ' + UnitPrice"
End With

ds.Tables("Prod ucts").Columns. Add(dc)

Dim dgvCombo As New DataGridViewCom boBoxColumn
With dgvCombo
.Width = 150
.DataSource = ds.Tables("Prod ucts")
.DisplayMember = "MultiCols"
.ValueMember = "ProductID"
.DataPropertyNa me = "ProductID"
.HeaderText = "Product"
End With

DataGridView1.C olumns.Add(dgvC ombo)
End Sub
End Class
Ken
--------------------------
"Bob" <bd*****@sgiims .com> wrote in message
news:Os******** ******@TK2MSFTN GP11.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 datagridviewcom bobox. 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***@bellsout h.net> wrote in message
news:OY******** *****@TK2MSFTNG P10.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.Sql Client

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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(s trConn)

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

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

DataGridView1.D ataSource = ds.Tables("Orde rs")

DataGridView1.C olumns.Remove(" ProductID")

Dim dc As New DataColumn("Mul tiCols")
With dc
.Expression = "ProductNam e + ' ' + UnitPrice"
End With

ds.Tables("Prod ucts").Columns. Add(dc)

Dim dgvCombo As New DataGridViewCom boBoxColumn
With dgvCombo
.Width = 150
.DataSource = ds.Tables("Prod ucts")
.DisplayMember = "MultiCols"
.ValueMember = "ProductID"
.DataPropertyNa me = "ProductID"
.HeaderText = "Product"
End With

DataGridView1.C olumns.Add(dgvC ombo)
End Sub
End Class
Ken
--------------------------
"Bob" <bd*****@sgiims .com> wrote in message
news:Os******** ******@TK2MSFTN GP11.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 datagridviewcom bobox. 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.Sql Client

Public Class Form1
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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(s trConn)

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

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

DataGridView1.D ataSource = ds.Tables("Orde rs")

DataGridView1.C olumns.Remove(" ProductID")

Dim dc As New DataColumn("Mul tiCols")
With dc
.Expression = "ProductNam e + ' ' + UnitPrice"
End With

ds.Tables("Prod ucts").Columns. Add(dc)

Dim dgvCombo As New DataGridViewCom boBoxColumn
With dgvCombo
.Width = 150
.DataSource = ds.Tables("Prod ucts")
.DisplayMember = "MultiCols"
.ValueMember = "ProductID"
.DataPropertyNa me = "ProductID"
.HeaderText = "Product"
End With

DataGridView1.C olumns.Add(dgvC ombo)
End Sub
End Class
Ken
--------------------------
"Bob" <bd*****@sgiims .com> wrote in message
news:Os******** ******@TK2MSFTN GP11.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 datagridviewcom bobox. 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
5233
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 (both rows and columns) are not of fixed size. I am using Visual Studio 2002/2003 IDE for development. Are there any extra feature for this availble in VS 2005? Thanking you.
0
1932
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 or a value list, etc. Can anyone point me in the right direction? Thanks!
0
2510
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 can input other keys (e.g. "Q", "@", "W") Anybody found this question? It looks like the textbox eat the input 'q' and does not show up in the text. Public Class DataGridViewEditorButtonEditingControl Inherits TextBox
0
1277
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, however I want to update the designer UI of my custom control when a person modifies the collection property via the Collection Editor in the designer (similar to when you add/remove columns from a DataGridView). I've looked and looked, however,...
0
2367
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 is a ComboBox which is similar to the built-in Combo column but will hopefully be more flexible. First: I am using this control in multiple columns which _should_ hold unique items. However, the items added to column A are visible in
1
2496
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? Please let me know. Any help will be gratefully appreciated. Thanks. my code:
1
2142
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 calender. When I changed the value of the picker, the text box is affected and vice-versa, it handled date errors like we want it to. The advantage is that it is easier to use for our user (they wanted it so I can't really have anything else). On its own it...
2
294
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 datagridview below the textbox
0
1876
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 properly by placing it on a panel control. I have added label as button on panel control to display data from datasource on to datagrid and create a binding source. Another Label which act as a button is used to update data from grid to databse.
0
8296
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8598
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.