I think this MSDN example will help you forward
DataGridViewComboBoxColumn.DataSource Property[
^]
Basically it works like this.
1. You have a
DataGridView
where you set the DataSource to a
DataTable
or
BindingSource
.
2. You specify that one of the columns should be a
DataGridViewComboBoxColumn
This column has a property called
DataPropertyName
which is set to a column in the DataTable.
Code from the designer module
this.dataSet1 = new System.Data.DataSet();
this.dataTable1 = new System.Data.DataTable();
this.dataColumn1 = new System.Data.DataColumn();
this.dataColumn2 = new System.Data.DataColumn();
this.bindingSource3 = new System.Windows.Forms.BindingSource(this.components);
this.columnSeasons = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.dataGridView1.DataSource = this.bindingSource3;
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Tables.AddRange(new System.Data.DataTable[] {
this.dataTable1});
this.dataTable1.Columns.AddRange(new System.Data.DataColumn[] {
this.dataColumn1,
this.dataColumn2});
this.dataTable1.TableName = "Table1";
this.dataColumn1.ColumnName = "Column1";
this.dataColumn2.ColumnName = "Column2";
this.bindingSource3.DataMember = "Table1";
this.bindingSource3.DataSource = this.dataSet1;
this.columnSeasons.DataPropertyName = "Column1";
this.columnSeasons.HeaderText = "Seasons";
this.columnSeasons.Name = "columnSeasons";
this.columnSeasons.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.columnSeasons.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
3. Now you need to set the data source for the dropdown list of the combo box.
This can be a list of strings or a data table.
DataTable dtSeasons = new DataTable("Seasons");
dtSeasons.Columns.Add("Id", typeof(int));
dtSeasons.Columns.Add("Value", typeof(string));
dtSeasons.Rows.Add(1, "Winter");
dtSeasons.Rows.Add(2, "Spring");
dtSeasons.Rows.Add(3, "Summer");
dtSeasons.Rows.Add(4, "Autumn");
columnSeasons.DataSource = dtSeasons;
columnSeasons.ValueMember = "Id";
columnSeasons.DisplayMember = "Value";
The code sample is not complete but I hope you get the picture.