Avatar of Soumen Roy
Soumen RoyFlag for India

asked on 

Add combo box filled row in data table

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
.NET Programming

Avatar of undefined
Last Comment
Ess Kay
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

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
Avatar of Soumen Roy
Soumen Roy
Flag of India image

ASKER

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
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of Ess Kay
Ess Kay
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo