Combobox in Datagridview in Edit mode

K

k_babaria

Hello,

I would like to have combobox come up in datagrid view control in
Windows forms project.
For example one of the columns is US States. When users enter into edit
mode I would like this cell to show up a combobox with the states
value. When this cell loses focus then the selected value should show
up in the cell and combobox should disappear.

I know about datagridviewcomboboxcolumn class, but this puts the
combobox in all the rows instead of having it show up in one cell while
editing.


Any help/pointers is appreciated.
Thanks
-Ketan
 
K

Ketan

Thanks Durstin,

this FAQ is for datagrid. I need to edit a cell in
DataGridView control that is introduced in the Whidbey version. Let me
know if you have any insights into this problem.

-Ketan
 
D

durstin

So you are, sorry, I wasn't paying close enough attention. Visual Studio
2005's DataGridView gives us the ComboBox column type for free, so we don't
have to do this for ourselves anymore. Here are some code snippets to
demonstrate:

//
// Column: Gender, combo box
//
// For this column, combo box contents are specified
programmatically
DataGridViewComboBoxColumn colGender = new
DataGridViewComboBoxColumn();
// Size the column width so it is wide enough to display the
header
colGender.AutoSizeMode =
DataGridViewAutoSizeColumnMode.ColumnHeader;
colGender.DataPropertyName = "Gender";
// Specifiy the list of choices in the combo box
colGender.Items.AddRange(new string[] { "M", "F" });
// Sort the combo box contents alphabetically
colGender.Sorted = true;
// Disable sorting for the column
colGender.SortMode = DataGridViewColumnSortMode.NotSortable;
colGender.HeaderText = "Gender";
colGender.Name = "Gender";
colGender.ReadOnly = false;
dataGridView2.Columns.Add(colGender);

//
// Column: Marital status, combo box
//
// For this column, combo box contents are retrieved from the
database
DataGridViewComboBoxColumn colMaritalStatus = new
DataGridViewComboBoxColumn();
// Size the column width so it is wide enough to display the
header
colMaritalStatus.AutoSizeMode =
DataGridViewAutoSizeColumnMode.ColumnHeader;
colMaritalStatus.DataPropertyName = "MaritalStatus";
// Retrieve the list of choices from the database
colMaritalStatus.DataSource =
dataSetAdventureWorks.Tables["MaritalStatusChoices"];
// Identify the column in the Employee table that is used to
select the combo box item
colMaritalStatus.ValueMember = "MaritalStatus";
// If the column value is not human friendly, e.g., a foreign
key identity off to a related table,
// the DisplayMember property is used to identify the column
used for display purposes
colMaritalStatus.DisplayMember = "MaritalStatus";
colMaritalStatus.HeaderText = "Marital Status";
colMaritalStatus.Name = "MaritalStatus";
colMaritalStatus.ReadOnly = false;
dataGridView2.Columns.Add(colMaritalStatus);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top