data binding Datagridcombobox column to datatable

I have a datagridview bind to datatable.
datatable structiure:
col1 nameID
col2 year

in the datagridview, I would like to add combobox from another datasource linked to the value of col1 from datatable.  Below will be the datagridview structure:
name year

how can i do that?
peterdevadossAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
Try adding this

NameComboAs.DataBindings.Add("SelectedValue", DataTable1, "NameID")
0
 
CodeCruiserCommented:
Can you explain in another way please. Do you mean you have a combobox fillby by using datatable and you want a value to be selected based on grid data?
0
 
peterdevadossAuthor Commented:
Data table:
            Dim col1 As New DataColumn()
            Dim col2 As New DataColumn()
            Col1.DataType = System.Type.GetType("System.Int32")
            Col1.ColumnName = "nameId"
            table1.Columns.Add(Col1)
            Col2.DataType = System.Type.GetType("System.String")
            Col2.ColumnName = "Locaiton"
            table1.Columns.Add(Col2)

Datagridview:
'comboboxcolumn in datagridview
            Dim typeDS As New DataSet
            sqlDa.SelectCommand = New SqlCommand("select Name,nameId from name", sqlCon)
            sqlDa.Fill(typeDS)
            Dim NameComboAs New DataGridViewComboBoxColumn
            NameComboAs .HeaderText = "Advert Type"
            NameComboAs .DataSource = typeDS.Tables(0)
            NameComboAs .DisplayMember = "AdvertType"
            NameComboAs .ValueMember = "AdvertTypeId"
            NameComboAs .ReadOnly = False

datagridview1.datasource=datatable1


- datagridview display like this: nameId,Name,location
name from combobox (bind to dataset)
nameId and location (bind to datatable)

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CodeCruiserCommented:
By the way, your query is


select Name,nameId from name
but you have

NameComboAs .DisplayMember = "AdvertType"
NameComboAs .ValueMember = "AdvertTypeId"

which does not look right.
0
 
peterdevadossAuthor Commented:
sorry that was wrong. actually it was like this:
            NameComboAs .DisplayMember = "Name"
            NameComboAs .ValueMember = "NameId"

There is no databinding property for NameComboAs.DataBindings.

I tried like the one below, which does not work either.
NameComboAs .DataPropertyName = "AdvtType"
0
 
peterdevadossAuthor Commented:
Correction:
NameComboAs .DataPropertyName = "NameId"
0
 
peterdevadossAuthor Commented:
Sorry for wrong update:
DataPropertyName is working.
Here is the correct syntax:
NameComboAs .DataPropertyName = "NameId"
0
 
CodeCruiserCommented:
Good. So the problem is sorted?
0
All Courses

From novice to tech pro — start learning today.