// Step 1. Derive a custom column style from DataGridTextBoxColumn
// a) add a ComboBox member
// b) track when the combobox has focus in Enter and Leave events
// c) override Edit to allow the ComboBox to replace the TextBox
// d) override Commit to save the changed data
// Step 2 - Use the combo column style
// Add 1 col with combo style
DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn();
ComboTextCol.MappingName = "custCity";
ComboTextCol.HeaderText = "Customer Address";
ComboTextCol.Width = 100;
ts1.GridColumnStyles.Add(ComboTextCol);
// Step 3 - Additional setup for Combo style
// a) make the row height a little larger to handle minimum combo height
ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3;
// b) Populate the combobox somehow. It is a normal combobox, so whatever...
ComboTextCol.ColumnComboBox.Items.Clear();
ComboTextCol.ColumnComboBox.Items.Add("Chicago");
ComboTextCol.ColumnComboBox.Items.Add("Corvallis");
ComboTextCol.ColumnComboBox.Items.Add("Denver");
ComboTextCol.ColumnComboBox.Items.Add("Great Falls");
ComboTextCol.ColumnComboBox.Items.Add("Kansas City");
ComboTextCol.ColumnComboBox.Items.Add("Los Angeles");
ComboTextCol.ColumnComboBox.Items.Add("Raleigh");
ComboTextCol.ColumnComboBox.Items.Add("Washington");
// c) set the dropdown style of the combo...
ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
// a) add a ComboBox member
// b) track when the combobox has focus in Enter and Leave events
// c) override Edit to allow the ComboBox to replace the TextBox
// d) override Commit to save the changed data
// Step 2 - Use the combo column style
// Add 1 col with combo style
DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn();
ComboTextCol.MappingName = "custCity";
ComboTextCol.HeaderText = "Customer Address";
ComboTextCol.Width = 100;
ts1.GridColumnStyles.Add(ComboTextCol);
// Step 3 - Additional setup for Combo style
// a) make the row height a little larger to handle minimum combo height
ts1.PreferredRowHeight = ComboTextCol.ColumnComboBox.Height + 3;
// b) Populate the combobox somehow. It is a normal combobox, so whatever...
ComboTextCol.ColumnComboBox.Items.Clear();
ComboTextCol.ColumnComboBox.Items.Add("Chicago");
ComboTextCol.ColumnComboBox.Items.Add("Corvallis");
ComboTextCol.ColumnComboBox.Items.Add("Denver");
ComboTextCol.ColumnComboBox.Items.Add("Great Falls");
ComboTextCol.ColumnComboBox.Items.Add("Kansas City");
ComboTextCol.ColumnComboBox.Items.Add("Los Angeles");
ComboTextCol.ColumnComboBox.Items.Add("Raleigh");
ComboTextCol.ColumnComboBox.Items.Add("Washington");
// c) set the dropdown style of the combo...
ComboTextCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
...
comboBox1.DataSource=...;
comboBox1.DisplayMember=...;
comboBox1.ValueMember=...;
((DataGridTextBoxColumn)dataGrid1.TableStyles[0] //如果对应第一个TableStyle,为0
.GridColumnStyles[colNo]) //要嵌入的列号colNo
.TextBox.Controls.Add(comboBox1);然后在dataGrid1_CurrentCellChanged处理程序中增加:
comboBox1.SelectedValue=dataGrid1.Item(dgCont.CurrentCell);
这样当移动到相应的格时,comboBox1会显示。然后可以增加语句实现ComboBox显示相应的内容,当选择ComboBox时将选择的内容写回cell内。
{
//#1,这是一个绑定日期获取器的例子,共三步,请看#1,#2,#3
style.GridColumnStyles.AddRange(new DataGridColumnStyle[]{id,room_id,real_name,sex,card,tel,degree,post,address,birth});
dgMain.TableStyles.Add(style);
//订阅单元格获得焦点时发生的事件
dgBox=(DataGridTextBoxColumn)dgMain.TableStyles[0].GridColumnStyles[9];//哪一列出现
dgBox.TextBox.GotFocus+= new EventHandler(this.dgBox_GotFocus);
// }
//#2
private void dgBox_GotFocus(object o, EventArgs e)
{
DateTimePicker dtp = new DateTimePicker();
dtp.Dock = DockStyle.Fill;
dtp.Cursor = Cursors.Arrow;
dtp.ValueChanged+=new EventHandler(this.dtg_ValueChanged);//日期获取器变化
dgBox.TextBox.Controls.Add(dtp);
dtp.BringToFront();
}
//#3
private void dtg_ValueChanged(object sender, System.EventArgs e)
{
int lineNum=dgMain.CurrentCell.RowNumber;//或dgMain.CurrentRowIndex
dgMain[lineNum,9]=((DateTimePicker)(sender)).Value.ToShortDateString();
}
http://www.codeproject.com/useritems/datagrid_combobox.asp