如何在datagrid列中添加下拉列表

解决方案 »

  1.   

    using System; 
    using System.Drawing; 
    using System.Collections; 
    using System.ComponentModel; 
    using System.Windows.Forms; 
    using System.Data; 
    namespace DataGridTest 

    public class Form1 : System.Windows.Forms.Form 

    private System.Windows.Forms.DataGrid dataGrid; 
    private DataTable dataTable; 
    private System.Windows.Forms.Button buttonFocus;
    private System.Windows.Forms.Button button1; 
    private System.ComponentModel.Container components = null;  public Form1() 

    InitializeComponent(); 
    PopulateGrid(); 
    } protected override void Dispose( bool disposing ) 

    if( disposing ) 

    if (components != null) 

    components.Dispose(); 


    base.Dispose( disposing ); 
    } #region Windows 窗体设计器生成的代码 
    private void InitializeComponent() 

    this.dataGrid = new System.Windows.Forms.DataGrid();
    this.buttonFocus = new System.Windows.Forms.Button();
    this.button1 = new System.Windows.Forms.Button();
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGrid
    // 
    this.dataGrid.DataMember = "";
    this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.dataGrid.Location = new System.Drawing.Point(4, 8);
    this.dataGrid.Name = "dataGrid";
    this.dataGrid.Size = new System.Drawing.Size(316, 168);
    this.dataGrid.TabIndex = 0;
    // 
    // buttonFocus
    // 
    this.buttonFocus.Location = new System.Drawing.Point(232, 184);
    this.buttonFocus.Name = "buttonFocus";
    this.buttonFocus.Size = new System.Drawing.Size(84, 23);
    this.buttonFocus.TabIndex = 1;
    this.buttonFocus.Text = "获取焦点";
    this.buttonFocus.Click += new System.EventHandler(this.buttonFocus_Click);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(48, 184);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(84, 23);
    this.button1.TabIndex = 2;
    this.button1.Text = "Form2";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(332, 217);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.buttonFocus);
    this.Controls.Add(this.dataGrid);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
    this.ResumeLayout(false); }
    #endregion 
    /// <summary> 
    /// 应用程序的主入口点。 
    /// </summary> 
    [STAThread] 
    static void Main() 

    Application.Run(new Form1()); 
    }  //初始化DataGrid 
    private void PopulateGrid() 

    //创建一个DataTable对象,包括四列,前三列为String,最后一列为Boolean。 
    dataTable = new DataTable ("FunctionArea"); 
    string[] arrstrFunctionalArea = new string [3]{"Functional Area","Min","Max"}; 
    DataColumn dtCol = null; 
    //创建String列 
    for(int i=0; i< 3;i++) 

    dtCol = new DataColumn(arrstrFunctionalArea[i]); 
    dtCol.DataType = Type.GetType("System.String"); 
    dtCol.DefaultValue = ""; 
    dataTable.Columns.Add(dtCol); 

    //创建Boolean列,用CheckedBox来显示。 
    DataColumn dtcCheck = new DataColumn("IsMandatory"); 
    dtcCheck.DataType = System.Type.GetType("System.Boolean"); 
    dtcCheck.DefaultValue = false; 
    dataTable.Columns.Add(dtcCheck); 
    //把表绑定到DataGrid 
    dataGrid.DataSource = dataTable; 
    //为DataGrid加载DataGridTableStyle样式 
    if(!dataGrid.TableStyles.Contains("FunctionArea")) 
    {
    DataGridTableStyle dgdtblStyle = new DataGridTableStyle(); 
    dgdtblStyle.MappingName = dataTable.TableName; 
    dataGrid.TableStyles.Add(dgdtblStyle); 
    dgdtblStyle.RowHeadersVisible = false; 
    dgdtblStyle.HeaderBackColor = Color.LightSteelBlue; 
    dgdtblStyle.AllowSorting = false; 
    dgdtblStyle.HeaderBackColor = Color.FromArgb(8,36,107); 
    dgdtblStyle.RowHeadersVisible = false; 
    dgdtblStyle.HeaderForeColor = Color.White; 
    dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, 
    System.Drawing.FontStyle.Bold, 
    System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
    dgdtblStyle.GridLineColor = Color.DarkGray;
    dgdtblStyle.PreferredRowHeight = 22;
    dataGrid.BackgroundColor = Color.White;
    //设置列的宽度
    GridColumnStylesCollection colStyle = dataGrid.TableStyles[0].GridColumnStyles;
    colStyle[0].Width = 100;
    colStyle[1].Width = 50;
    colStyle[2].Width = 50;
    colStyle[3].Width = 80;

    DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dataGrid.TableStyles[0].GridColumnStyles[0]; 
    ComboBox cmbFunctionArea = new ComboBox(); 
    cmbFunctionArea.Items.AddRange(new object[]{"选项一","选项二","选项三"}); 
    cmbFunctionArea.Cursor = Cursors.Arrow; 
    cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList; 
    cmbFunctionArea.Dock = DockStyle.Fill; 
    //在选定项发生更改并且提交了该更改后发生 
    cmbFunctionArea.SelectionChangeCommitted += new EventHandler(cmbFunctionArea_SelectionChangeCommitted); 
    //把ComboBox添加到DataGridTableStyle的第一列 
    dgtb.TextBox.Controls.Add(cmbFunctionArea); 
    }  //设置焦点模拟 
    private void GetFocus(int row,int col) 

    //先把焦点移动到DataGrid 
    this.dataGrid.Focus(); 
    //把焦点移动到DataGridCell 
    DataGridCell dgc = new DataGridCell(row,col); 
    this.dataGrid.CurrentCell = dgc; 
    DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dataGrid.TableStyles[0].GridColumnStyles[col]; 
    //设置焦点 
    dgtb.TextBox.Focus(); 
    } //把Combobox上修改的数据提交到当前的网格 
    private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e) 

    this.dataGrid[this.dataGrid.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString(); 
    }  //设置新的焦点 
    private void buttonFocus_Click(object sender, System.EventArgs e) 

    //焦点模拟,这里设置第三行第一列 
    GetFocus(2,0); 
    }

    }