Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Initializing a ComboBoxColumn in DataGridView
 

Initializing a ComboBoxColumn in DataGridView

I have a datagridview on my form. I am trying to make one of the columns into a ComboBox. I want to initialize the combobox when the form loads (or in designer mode....however, when I add items to the columns "Data" "Items" - the items are gone when I open it back up again).

Here is my code from the designer for the form (relating to datagrid)
//
this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewComboBoxColumn1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.dataGridViewTextBoxColumn4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn6 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn7 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn8 = new System.Windows.Forms.DataGridViewTextBoxColumn();

//

// cardFileDataGridView

//

this.cardFileDataGridView.AutoGenerateColumns = false;

this.cardFileDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;

this.cardFileDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[]
{
this.dataGridViewTextBoxColumn1,
this.dataGridViewComboBoxColumn1,
this.dataGridViewTextBoxColumn4,
this.dataGridViewTextBoxColumn6,
this.dataGridViewTextBoxColumn7,
this.dataGridViewTextBoxColumn8});
this.cardFileDataGridView.DataSource = this.dataTable1BindingSource;
this.cardFileDataGridView.Location = new System.Drawing.Point(24, 82);
this.cardFileDataGridView.Name = "cardFileDataGridView";
this.cardFileDataGridView.Size = new System.Drawing.Size(853, 220);
this.cardFileDataGridView.TabIndex = 15;

in the Page Load event

private

void SearchCardFile_Load(object sender, EventArgs e)

{

// TODO: This line of code loads data into the 'shelterValleyCombined.DataTable1' table. You can move, or remove it, as needed.

this.dataTable1TableAdapter.FillCombined(this.shelterValleyCombined.DataTable1);

//

var comboColumn = this.cardFileDataGridView.Columns["Status"] as DataGridViewComboBoxColumn;

comboColumn.Items.Add(

"Shelter Valley Inventory");

}

The datagridview is databound to a SQLExpress Database Table using dataset.
How do you set the values of the combobox grid so that the user can only select from the list when there are editing the data?

Thanks, John

JohnFeeney  Wednesday, September 30, 2009 6:31 PM

Hi JohnFeeney,

 

You need to set the DataSource, DisplayMember and ValueMember properties of the DataGridViewComboBoxColumn to fill the items. The document below includes a sample:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspx

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, October 02, 2009 9:12 AM
I should have listed the error that I am getting

System.NullReferenceException was unhandle Message="Object reference not set to an instance of an object."

This is one the line
comboColumn.items.Add("Shelter Valley Inventory");
JohnFeeney  Wednesday, September 30, 2009 6:35 PM

Hi JohnFeeney,

 

You need to set the DataSource, DisplayMember and ValueMember properties of the DataGridViewComboBoxColumn to fill the items. The document below includes a sample:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspx

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, October 02, 2009 9:12 AM
You also need to set the DisplayPropertyName.
JohnFeeney  Friday, October 02, 2009 2:52 PM

You can use google to search for other answers

Custom Search

More Threads

• Find number from string
• Print Datagrid
• pop-up window appear for every intraval of time
• datagrid currentcell highlighting
• Same schema, different data stores
• How to bind an arraylist to combobox without a nullvaule
• column alignment problem
• How to set DataGridViewComboBoxColumn to display nothing ?
• COMBOBOX many-to-many
• Is there a Repeat Region for VB?