Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add combo box filled row in data table

Posted on 2016-07-19
4
Medium Priority
?
52 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

636 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