Solved

Add combo box filled row in data table

Posted on 2016-07-19
4
39 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

838 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