Solved

Add combo box filled row in data table

Posted on 2016-07-19
4
41 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 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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