第一步:public class DataGridComboBoxColumn : DataGridTextBoxColumn
{
public NoKeyUpCombo ColumnComboBox = null;
private System.Windows.Forms.CurrencyManager _source = null;
private int _rowNum;
private bool _isEditing = false;
ComboValueChanged _valueChanging;
public DataGridComboBoxColumn(ComboValueChanged valueChanging) : base()
{
_valueChanging = valueChanging;
ColumnComboBox = new NoKeyUpCombo();
ColumnComboBox.Leave += new EventHandler(LeaveComboBox);
// ColumnComboBox.Enter += new EventHandler(ComboMadeCurrent);
ColumnComboBox.SelectedIndexChanged += new System.EventHandler(ComboIndexChanged);
ColumnComboBox.SelectionChangeCommitted += new System.EventHandler(ComboStartEditing);
// Step 2 - Use the combo column style
// Add 1 col with combo style
DataGridComboBoxColumn ComboTextCol = new DataGridComboBoxColumn(new ComboValueChanged(MyComboValueChanged));
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;
//add the custom table style to the datagrid table styles
dataGrid1.TableStyles.Add(ts1);
public int SelectComBo(string tableName,object obj) //返回某单元Cell在DataTable中的行号
{
int i=0;
for(i=0;i<this.dataSet11.Tables[tableName].Rows.Count;i++)
{
if(this.dataSet11.Tables[tableName].Rows[i][0].ToString()==obj.ToString())
{
break;
}