Solved

Add combo box filled row in data table

Posted on 2016-07-19
4
33 Views
Last Modified: 2016-07-21
Hi,

I am doing a project in VB.Net. I have attached datatable in a datagridview with couple of datacolumns.

Now I want to insert a row just below datagridview header row in which each cell needs to be a combobox. Those combobox need to be filled up by column names of a SQL server table.

The user will select different values from those comboboxes.

Finally I want the entire datagridview table value with selected combobox values, in a datatable as datagridview.datasource.

How to do that? Please advise.

Regards,
Soumen
0
Comment
Question by:Soumen Roy
  • 3
4 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 41721673
To add comboboxes into the datagridview use the following

Private Sub AddSomeComboBoxes()
			dataGridView1.ColumnCount = 3
			dataGridView1.Columns(0).Name = "ID"
			dataGridView1.Columns(1).Name = "Name"
			dataGridView1.Columns(2).Name = "Price"

			Dim row As String() = New String() {"1", "Product A", "1000"}
			dataGridView1.Rows.Add(row)
			row = New String() {"2", "Product B", "2000"}
			dataGridView1.Rows.Add(row)
			row = New String() {"3", "Product C", "3000"}
			dataGridView1.Rows.Add(row)
			row = New String() {"4", "Product D", "4000"}
			dataGridView1.Rows.Add(row)

			Dim cmb As New DataGridViewComboBoxColumn()
			cmb.HeaderText = "Select Data"
			cmb.Name = "cmb"
			cmb.MaxDropDownItems = 4
			cmb.Items.Add("True")
			cmb.Items.Add("False")
			dataGridView1.Columns.Add(cmb)

		End Sub

Open in new window


You chould be able to access the data via    dataGridView1.datasource

hope that helps
0
 

Author Comment

by:Soumen Roy
ID: 41722061
Hi,

Thank you for your advice.

As I told, my datagridview already has filled up columns from attached datatable . So no need to add columns. There lies the criticality is.

I want to add an extra row in that datagridview at 0th index with comboboxes. Which essentially need to be filled up by other data sources.

Regards,
Roy
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 41723210
What you would want to do is use the code above to add a column (either on form load or initializegrid)

Then set the displayIndex to 0:  
 dataGridView1.Columns["YOUCOLUMNNAME"].DisplayIndex = 0

Open in new window



Datasources:
instead of

cmb.Items.Add("True")
cmb.Items.Add("False")

Open in new window


you would run a loop through your 'other data sources' and add the items that way




example (in pseudocode):
Dim dt as dataTable
'...  fill the dt with results from other datasources

 For Each Row In DT.Rows
                    cmb.Items.Add(Row("YOURCOLUMNINDATATABLE"))
Next

Open in new window

0
 
LVL 15

Accepted Solution

by:
Ess Kay earned 500 total points
ID: 41723228
'### CREATES A COMBOBOX HERE
Dim cmb As New DataGridViewComboBoxColumn()
cmb.HeaderText = "Select Data"
cmb.Name = "cmb"
cmb.MaxDropDownItems = 4

'### FILLS COMBOBOX DATA
Dim dt as dataTable
'...  fill the dt with results from other datasources
For Each Row In DT.Rows
cmb.Items.Add(Row("YOURCOLUMNINDATATABLE"))
Next

'### ADDS COMBOBOX TO GRID
dataGridView1.Columns.Add(cmb)

'### MOVES COMBOBOX TO POSITION 0 
dataGridView1.Columns["cmb"].DisplayIndex = 0

Open in new window



If you want to make a whole row of comboboxes, it would only be possible in three ways:
1. You make all columns, combobox columns
2. You make your own datagridview usercontrol which allows this
3. You use someone else's #2   (EX: Infragistics ultragrid)
Otherwise, it is not supported by regular DGV

I think however, Microsoft has already done a sample solution for #2 here:  https://msdn.microsoft.com/en-us/library/aa480727.aspx

Hope that helps
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now