DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)datagrid1.TableStyles[0].GridColumnStyles[0];       
      ComboBox cmb= new ComboBox();
   cmb.Items.AddRange(new object[]{"选项一","选项二","选项三"});
   cmb.Cursor = Cursors.Arrow;
   cmb.DropDownStyle= ComboBoxStyle.DropDownList;
   cmb.Dock = DockStyle.Fill;
  
   dgtb.TextBox.Controls.Add(cmb); 动态的添加数据应该简单,修改第二句就行了

解决方案 »

  1.   

    说错了
    是修改cmb.Items.AddRange(new object[]{"选项一","选项二","选项三"});
    内容
      

  2.   

    谢谢秦哥,小弟之前已经这样做了,就是不知如何
    去修改修改cmb.Items.AddRange(new object[]{"选项一","选项二","选项三"});
    请指点
      

  3.   

    http://www.codeproject.com/cs/miscctrl/里有老外写好从ColumnTextBox继承过来的.cs控件,直接调用就行了,类似的代码有三个,不过都有点小错误,自己改改就行了。
      

  4.   

    using System;
    using System.Data;
    using System.Drawing;
    using System.Collections;
    using System.Windows.Forms;namespace ss_manage
    {
     public class DataGridComboBoxColumn : System.Windows.Forms.DataGridTextBoxColumn 
     {
      private ComboBox _cboColumn;
      private object _objSource;
      private string _strMember;
      private string _strValue;
      private bool _bIsComboBound = false;
      private Brush _backBrush = null;
      private Brush _foreBrush = null;  private int _iRowNum;
      private CurrencyManager _cmSource;
      private bool _editFlag;
      private DataTable _objSourceTable;  public DataGridComboBoxColumn(object objSource, string strMember, string strValue, bool bUseDropDownList, bool editFlag, DataGrid myDataGrid)
      {
       _objSourceTable = (DataTable)objSource;
       _objSource = objSource;
       _strMember = strMember;
       _strValue = strValue;
       _editFlag = editFlag;   _cboColumn = new ComboBox();
       _cboColumn.DisplayMember = _strMember;
       _cboColumn.ValueMember = _strValue;
       _cboColumn.DataSource = _objSource;
       _cboColumn.MaxDropDownItems = 20;
       if (bUseDropDownList == true) 
       {
        _cboColumn.DropDownStyle = ComboBoxStyle.DropDownList;
        this.ReadOnly = true;
       } 
       else 
       {
        _cboColumn.DropDownStyle = ComboBoxStyle.DropDown;
        this.ReadOnly = false;
       }   DataTable myDataTable = (DataTable)objSource;
       Graphics _graphicsContext=myDataGrid.CreateGraphics();
       float _widest=0;
       SizeF _stringSize=new SizeF(0,0);
       foreach (DataRow dr in myDataTable.Rows) 
       {
        _stringSize=_graphicsContext.MeasureString(dr[strMember].ToString(), myDataGrid.Font);
        if (_stringSize.Width>_widest) 
        {
         _widest=_stringSize.Width;
        }
       }
       _cboColumn.DropDownWidth=(int)Math.Ceiling(_widest);
       if(_editFlag)
       {
        this.Width=_cboColumn.DropDownWidth+20; // Add the space for the dropdown arrow
       }
       else
       {
        this.Width=_cboColumn.DropDownWidth+5; // Add the space for the dropdown arrow
       }   _cboColumn.Leave += new EventHandler(cboColumn_Leave);
       _cboColumn.Visible = false;
      }  private void _cboColumn_KeyPress(object sender, KeyPressEventArgs e)
      {
      }  protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
      {
       if (_bIsComboBound == false) 
       {
        _bIsComboBound = true;
        this.DataGridTableStyle.DataGrid.Controls.Add(_cboColumn);
       }
       _iRowNum = rowNum;
       _cmSource = source;
       _cboColumn.Font = this.TextBox.Font;
       object anObj = this.GetColumnValueAtRow(source, rowNum);
       _cboColumn.Bounds = bounds;
       _cboColumn.BeginUpdate();
       _cboColumn.Visible = _editFlag;
       if (anObj.GetType() != typeof(System.DBNull)) 
       {
        _cboColumn.SelectedValue = anObj;
       } 
       else 
       {
        _cboColumn.SelectedValue = null;
       }
       _cboColumn.EndUpdate();
       _cboColumn.Focus();
      }  public void cboColumn_Leave(object sender, EventArgs e) 
      {
       object objValue = _cboColumn.SelectedValue;
       if (objValue == null) 
       { 
        objValue = DBNull.Value; 
       }
       if(_cmSource.Position == _iRowNum)
        this.SetColumnValueAtRow(_cmSource, _iRowNum, objValue);
       _cboColumn.Visible = false;
      }  protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
      {
       string strCountry = "China";
       DataRow[] aRowA;   object anObj = this.GetColumnValueAtRow(source, rowNum);
       Type aType = anObj.GetType();
       if (aType != typeof(System.DBNull)) 
       {
        aRowA = ((DataTable)_objSource).Select(_strValue + " = '" + anObj.ToString() + "'");
       } 
       else 
       {
        aRowA = ((DataTable)_objSource).Select();
       }
       if (aRowA.Length > 0) 
       {
        strCountry = aRowA[0][_strMember].ToString();
       }
       Rectangle rect = bounds;
       if (this._backBrush == null) 
        g.FillRectangle(backBrush, rect); 
       else 
        g.FillRectangle(_backBrush, rect);
       rect.Y += 2;
       if (this._foreBrush == null) 
        g.DrawString(strCountry, this.TextBox.Font, foreBrush, rect); 
       else
        g.DrawString(strCountry, this.TextBox.Font, _foreBrush, rect);
      }  public System.Drawing.Color backgroundColour 
      {
       set { if (value == System.Drawing.Color.Transparent) this._backBrush = null; else this._backBrush = new SolidBrush(value);  }
      }  public System.Drawing.Color foregroundColour 
      {
       set { if (value == System.Drawing.Color.Transparent) this._foreBrush = null; else this._foreBrush = new SolidBrush(value);  }
      }
     }
    }
      

  5.   

    调用方法:
    #region 条件表Datagrid样式重载
    private void ss_gxdy21_ColumnStyle_Reload(DataGrid myDataGrid,bool editFlag,int typeFlag)
    {
    DataTable myDataTable = new DataTable();
    //声明DataGridTableStyle
    DataGridTableStyle myTableStyle = new DataGridTableStyle( );
    myTableStyle.MappingName = "ss_gxdy21"; //声明DataGridColumnStyle1
    DataGridTextBoxColumn ColumnStyle1 = new DataGridTextBoxColumn();
    ColumnStyle1.MappingName = "F_VARIABLE";
    ColumnStyle1.HeaderText = "变量";
    ColumnStyle1.Width = 0;
    myTableStyle.GridColumnStyles.Add(ColumnStyle1); DataGridTextBoxColumn ColumnStyle2 = new DataGridTextBoxColumn();
    ColumnStyle2.MappingName = "C_CNAME";
    ColumnStyle2.HeaderText = "列名";
    myTableStyle.GridColumnStyles.Add(ColumnStyle2); myDataTable = new myClass().getStrTable("=,>=,<=","C_OP_STR");
    DataGridComboBoxColumn ColumnStyle3=new DataGridComboBoxColumn(myDataTable, myDataTable.Columns[0].ColumnName, myDataTable.Columns[0].ColumnName, true, false, myDataGrid);
    ColumnStyle3.MappingName = "C_OP";
    ColumnStyle3.HeaderText = "关系";
    myTableStyle.GridColumnStyles.Add(ColumnStyle3); sql = "SELECT C_TYPE,C_TYPENAME FROM SS_TYPE";
    myDataTable = myWs.GetDs(sql,"ColumnName").Tables[0];
    DataGridComboBoxColumn ColumnStyle7=new DataGridComboBoxColumn(myDataTable, myDataTable.Columns[1].ColumnName, myDataTable.Columns[0].ColumnName, true, false, myDataGrid);
    ColumnStyle7.MappingName = "C_TYPE";
    ColumnStyle7.HeaderText = "类型";
    myTableStyle.GridColumnStyles.Add(ColumnStyle7); DataGridTextBoxColumn ColumnStyle4 = new DataGridTextBoxColumn();
    ColumnStyle4.MappingName = "C_DATA";
    ColumnStyle4.HeaderText = "数值";
    ColumnStyle4.Width = 100;
    myTableStyle.GridColumnStyles.Add(ColumnStyle4); if(typeFlag==0)
    {
    DataGridTextBoxColumn ColumnStyle6 = new DataGridTextBoxColumn();
    ColumnStyle6.MappingName = "C_VALUE";
    ColumnStyle6.HeaderText = "显示";
    ColumnStyle6.Width = 60;
    myTableStyle.GridColumnStyles.Add(ColumnStyle6);
    }
    else if (typeFlag==1)
    {
    string formatStr = "yyyy-MM-dd HH:mm:ss";
    DataGridDateTimePickerColumn ColumnStyle6=new DataGridDateTimePickerColumn(DateTime.Now,formatStr,myDataGrid,"ss_gxdy21_DateUpdate");
    ColumnStyle6.MappingName = "C_VALUE";
    ColumnStyle6.HeaderText = "显示";
    ColumnStyle6.Format = formatStr;
    ColumnStyle6.NullText="";
    ColumnStyle6.TextBox.LostFocus += new EventHandler(TextBox_LostFocus);
    myTableStyle.GridColumnStyles.Add(ColumnStyle6);
    } myDataTable = new myClass().getStrTable(",AND,OR","C_LOGIC_STR");
    DataGridComboBoxColumn ColumnStyle5=new DataGridComboBoxColumn(myDataTable, myDataTable.Columns[0].ColumnName, myDataTable.Columns[0].ColumnName, true, false, myDataGrid);
    ColumnStyle5.MappingName = "C_LOGIC";
    ColumnStyle5.HeaderText = "逻辑";
    myTableStyle.GridColumnStyles.Add(ColumnStyle5); myDataGrid.TableStyles.Clear();
    myDataGrid.TableStyles.Add( myTableStyle );
    }
    #endregion
      

  6.   

    还有内签DateTimePicker,我从上面ComboBox改的,原理差不多using System;
    using System.Data;
    using System.Drawing;
    using System.Collections;
    using System.Windows.Forms;namespace ss_manage
    {
    /// <summary>
    /// This DataGrid Column class implements a ComboBox Column. The combo box is fed from a 
    /// table containing an collection of value objects and a string descriptor (the displayed 
    /// element). On losing focus, the combo box the current cell in the associated DataGrid
    /// is updated with the current selected value object
    /// </summary>
    public class DataGridDateTimePickerColumn : System.Windows.Forms.DataGridTextBoxColumn 
    {
    private DateTimePicker _dtpColumn;
    private object _objSource;
    private string _formatStr;
    private bool _bIsComboBound = false;
    private Brush _backBrush = null;
    private Brush _foreBrush = null; private int _iRowNum;
    private CurrencyManager _cmSource;
    private string DataGridFlag; public DataGridDateTimePickerColumn(object objSource,string formatStr,DataGrid _myDataGrid)
    {
    _objSource = objSource;
    _formatStr = formatStr; _dtpColumn = new DateTimePicker();
    _dtpColumn.Format = DateTimePickerFormat.Custom;
                _dtpColumn.CustomFormat = formatStr; Graphics _graphicsContext=_myDataGrid.CreateGraphics();
    float _widest=0;
    SizeF _stringSize=new SizeF(0,0);
    _stringSize=_graphicsContext.MeasureString(formatStr, _myDataGrid.Font);
    if (_stringSize.Width>_widest) 
    {
    _widest=_stringSize.Width;
    }
    this.Width=(int)Math.Ceiling(_widest)+20; _dtpColumn.Leave += new EventHandler(dtpColumn_Leave);
    _dtpColumn.Visible = false;
    } public DataGridDateTimePickerColumn(object objSource,string formatStr,DataGrid _myDataGrid,string _DataGridFlag)
    {
    _objSource = objSource;
    _formatStr = formatStr;
    DataGridFlag = _DataGridFlag; _dtpColumn = new DateTimePicker();
    _dtpColumn.Format = DateTimePickerFormat.Custom;
    _dtpColumn.CustomFormat = formatStr; Graphics _graphicsContext=_myDataGrid.CreateGraphics();
    float _widest=0;
    SizeF _stringSize=new SizeF(0,0);
    _stringSize=_graphicsContext.MeasureString(formatStr, _myDataGrid.Font);
    if (_stringSize.Width>_widest) 
    {
    _widest=_stringSize.Width;
    }
    this.Width=(int)Math.Ceiling(_widest)+20; _dtpColumn.Leave += new EventHandler(dtpColumn_Leave);
    _dtpColumn.Visible = false;
    } protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
    {
    if (_bIsComboBound == false) 
    {
    _bIsComboBound = true;
    this.DataGridTableStyle.DataGrid.Controls.Add(_dtpColumn);
    }
    _iRowNum = rowNum;
    _cmSource = source;
    _dtpColumn.Font = this.TextBox.Font;
    object anObj = this.GetColumnValueAtRow(source, rowNum); _dtpColumn.Bounds = bounds;
    _dtpColumn.Visible = true;
    if (anObj.GetType() != typeof(System.DBNull)) 
    {
    _dtpColumn.Value = System.Convert.ToDateTime(anObj);

    else 
    {
    _dtpColumn.Value = System.Convert.ToDateTime(_objSource);
    }
    _dtpColumn.Focus();
    } public void dtpColumn_Leave(object sender, EventArgs e) 
    {
    object objValue = _dtpColumn.Value;
    if (objValue == null) 

    objValue = DBNull.Value; 
    }
    if(_cmSource.Position == _iRowNum)
    this.SetColumnValueAtRow(_cmSource, _iRowNum, objValue);
    _dtpColumn.Visible = false; if(DataGridFlag != null && DataGridFlag == "ss_gxdy21_DateUpdate")
    {
    DataView myDataViewTime = ss_Cust_Formula.ss_gxdy21.DefaultView;
    myDataViewTime.RowFilter = "F_VARIABLE = '"+ss_Cust_Formula.listBox1.SelectedValue+"' and C_TYPE = 'DATE' and (C_OP = '>=' or C_OP = '<=')"; foreach (DataRowView myDataRowView in myDataViewTime)
    {
    //绝对时间转换相对时间
    myDataRowView.BeginEdit();
    myDataRowView[4] = new myClass().AbsToRel(System.Convert.ToDateTime(MainForm.dgDataSet.Tables[0].Rows[0]["RQ"]),System.Convert.ToDateTime(myDataRowView[5]),myDataRowView["C_OP"].ToString());
    myDataRowView.EndEdit();
    }
    }
    } protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
    {

    string strCountry = "China";
    string aRowA; object anObj = this.GetColumnValueAtRow(source, rowNum);
    Type aType = anObj.GetType(); if (aType != typeof(System.DBNull)) 
    {
    aRowA = System.Convert.ToDateTime(anObj).ToString(_formatStr);

    else 
    {
    aRowA = System.Convert.ToDateTime(_objSource).ToString(_formatStr);
    }
    if (aRowA != null) 
    {
    strCountry = aRowA;
    } Rectangle rect = bounds;
    if (this._backBrush == null) 
    g.FillRectangle(backBrush, rect); 
    else 
    g.FillRectangle(_backBrush, rect);
    rect.Y += 2;
    if (this._foreBrush == null) 
    g.DrawString(strCountry, this.TextBox.Font, foreBrush, rect); 
    else
    g.DrawString(strCountry, this.TextBox.Font, _foreBrush, rect);
    } public System.Drawing.Color backgroundColour 
    {
    set { if (value == System.Drawing.Color.Transparent) this._backBrush = null; else this._backBrush = new SolidBrush(value);  }
    } public System.Drawing.Color foregroundColour 
    {
    set { if (value == System.Drawing.Color.Transparent) this._foreBrush = null; else this._foreBrush = new SolidBrush(value);  }
    }
    }
    }
      

  7.   

    boyxia(无天刀绝):
    小弟水平太差,还是不行,以下是小弟的代码,帮小弟改改吧
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace _20040810
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.DataGrid datagrid1;
    private System.Windows.Forms.Button load;
    private System.Windows.Forms.Button save;
    private System.Windows.Forms.Button Exit;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
    private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
    private System.Data.SqlClient.SqlConnection sqlConnection1;
    private _20040810.DataSet1 dataSet11;
    private System.Windows.Forms.DateTimePicker datatimepicker1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.datagrid1 = new System.Windows.Forms.DataGrid();
    this.load = new System.Windows.Forms.Button();
    this.save = new System.Windows.Forms.Button();
    this.Exit = new System.Windows.Forms.Button();
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.dataSet11 = new _20040810.DataSet1();
    this.datatimepicker1 = new System.Windows.Forms.DateTimePicker();
    ((System.ComponentModel.ISupportInitialize)(this.datagrid1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
    this.SuspendLayout();
    // 
    // datagrid1
    // 
    this.datagrid1.DataMember = "checkbox";
    this.datagrid1.DataSource = this.dataSet11;
    this.datagrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
    this.datagrid1.Location = new System.Drawing.Point(8, 8);
    this.datagrid1.Name = "datagrid1";
    this.datagrid1.Size = new System.Drawing.Size(384, 240);
    this.datagrid1.TabIndex = 0;
    // 
    // load
    // 
    this.load.Location = new System.Drawing.Point(128, 256);
    this.load.Name = "load";
    this.load.Size = new System.Drawing.Size(64, 24);
    this.load.TabIndex = 1;
    this.load.Text = "刷新";
    this.load.Click += new System.EventHandler(this.load_Click);
    // 
    // save
    // 
    this.save.Location = new System.Drawing.Point(208, 256);
    this.save.Name = "save";
    this.save.Size = new System.Drawing.Size(64, 24);
    this.save.TabIndex = 2;
    this.save.Text = "保存";
    this.save.Click += new System.EventHandler(this.save_Click);
    // 
    // Exit
    // 
    this.Exit.Location = new System.Drawing.Point(344, 256);
    this.Exit.Name = "Exit";
    this.Exit.Size = new System.Drawing.Size(64, 24);
    this.Exit.TabIndex = 3;
    this.Exit.Text = "退出";
    this.Exit.Click += new System.EventHandler(this.Exit_Click);
    // 
    // sqlDataAdapter1
    // 
    this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "checkbox", new System.Data.Common.DataColumnMapping[] {
      new System.Data.Common.DataColumnMapping("userid", "userid"),
      new System.Data.Common.DataColumnMapping("name", "name"),
      new System.Data.Common.DataColumnMapping("address", "address"),
      new System.Data.Common.DataColumnMapping("pruview", "pruview")})});
    this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
    // 
    // sqlSelectCommand1
    // 
    this.sqlSelectCommand1.CommandText = "SELECT userid, name, address, pruview FROM checkbox";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO checkbox(userid, name, address, pruview) VALUES (@userid, @name, @add" +
    "ress, @pruview); SELECT userid, name, address, pruview FROM checkbox WHERE (user" +
    "id = @userid)";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@userid", System.Data.SqlDbType.VarChar, 10, "userid"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.DateTime, 8, "name"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.VarChar, 10, "address"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pruview", System.Data.SqlDbType.Bit, 1, "pruview"));
      

  8.   

    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = @"UPDATE checkbox SET userid = @userid, name = @name, address = @address, pruview = @pruview WHERE (userid = @Original_userid) AND (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (name = @Original_name OR @Original_name IS NULL AND name IS NULL) AND (pruview = @Original_pruview OR @Original_pruview IS NULL AND pruview IS NULL); SELECT userid, name, address, pruview FROM checkbox WHERE (userid = @userid)";
    this.sqlUpdateCommand1.Connection = this.sqlConnection1;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@userid", System.Data.SqlDbType.VarChar, 10, "userid"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.DateTime, 8, "name"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@address", System.Data.SqlDbType.VarChar, 10, "address"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pruview", System.Data.SqlDbType.Bit, 1, "pruview"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_userid", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "userid", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_address", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "address", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_name", System.Data.SqlDbType.DateTime, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "name", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_pruview", System.Data.SqlDbType.Bit, 1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "pruview", System.Data.DataRowVersion.Original, null));
    // 
    // sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = @"DELETE FROM checkbox WHERE (userid = @Original_userid) AND (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (name = @Original_name OR @Original_name IS NULL AND name IS NULL) AND (pruview = @Original_pruview OR @Original_pruview IS NULL AND pruview IS NULL)";
    this.sqlDeleteCommand1.Connection = this.sqlConnection1;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_userid", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "userid", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_address", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "address", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_name", System.Data.SqlDbType.DateTime, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "name", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_pruview", System.Data.SqlDbType.Bit, 1, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "pruview", System.Data.DataRowVersion.Original, null));
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "workstation id=JIANHUNAG;packet size=4096;integrated security=SSPI;data source=JI" +
    "ANHUNAG;persist security info=False;initial catalog=jian";
    // 
    // dataSet11
    // 
    this.dataSet11.DataSetName = "DataSet1";
    this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(400, 273);
    this.Controls.Add(this.Exit);
    this.Controls.Add(this.save);
    this.Controls.Add(this.load);
    this.Controls.Add(this.datagrid1);
    this.Name = "Form1";
    this.Text = "Form1";
    ((System.ComponentModel.ISupportInitialize)(this.datagrid1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Exit_Click(object sender, System.EventArgs e)
    {
    this.Close();
    } private void save_Click(object sender, System.EventArgs e)
    {
    this.sqlDataAdapter1.Update(dataSet11);
    } private void load_Click(object sender, System.EventArgs e)
    {
    this.datagrid1.TableStyles.Clear();
    this.sqlDataAdapter1.Fill(dataSet11);
    this.dataSet11.Tables["checkbox"].Columns["pruview"].DefaultValue=0;
    this.dataSet11.Tables["checkbox"].Columns["pruview"].AllowDBNull=false;

    DataGridTableStyle dgdtblstyle=new DataGridTableStyle();
    dgdtblstyle.MappingName="checkbox";
    datagrid1.TableStyles.Add(dgdtblstyle);
    DataGridTextBoxColumn dgtb=(DataGridTextBoxColumn)datagrid1.TableStyles[0].GridColumnStyles[2];
    ComboBox cmb=new ComboBox();
    cmb.Items.AddRange(new object[]{"1111","2222","3333","5555"});
    cmb.Cursor=Cursors.Arrow;
    cmb.Dock=DockStyle.Fill;
    cmb.SelectionChangeCommitted+=new EventHandler(cmb_SelectionChangeCommitted);
    dgtb.TextBox.Controls.Add(cmb);
               
    DataGridTextBoxColumn dgtb1=(DataGridTextBoxColumn)datagrid1.TableStyles[0].GridColumnStyles[1];
    DateTimePicker datatimepicker1=new DateTimePicker();
    datatimepicker1.Cursor=Cursors.Arrow;
    datatimepicker1.Dock=DockStyle.Fill;
    this.datatimepicker1.Width=100;
    datatimepicker1.MinDate=new DateTime(1985, 6, 20);
    //datatimepicker1.MaxDate=DateTime.Today;
    datatimepicker1.TextChanged+=new EventHandler(datatimepicker1_good);

    dgtb1.TextBox.Controls.Add(datatimepicker1);
    //this.datagrid1.DataSource=dataSet11;
    //this.datagrid1.DataMember="checkbox";
    }
    private void cmb_SelectionChangeCommitted(object sender,EventArgs e)
    {
    this.datagrid1[this.datagrid1.CurrentCell]=((ComboBox)sender).SelectedItem.ToString();
    }
    private void datatimepicker1_good(object sender,EventArgs e)
    {
    this.datagrid1[this.datagrid1.CurrentCell]=((DateTimePicker)sender).Value;
    } }
    }
      

  9.   

    msdn上有一个DateTimePicker的源代码,改一改就行了
    主要是重写三个方法
    1.Edit() 当该单元格或的焦点时发生,这时将控件属性visible=true
    2.Commit() 提交,离开单元格时发生,visible=false
    3.Paint() 重绘制单元格
    下面是几个链接:
    http://www.codeproject.com/cs/miscctrl/DataGridZen.asp?df=100&forumid=31224&exp=0&select=715228http://www.codeproject.com/cs/miscctrl/RenDataGridComboBoxColumn.asphttp://www.c-sharpcorner.com/winforms/ComboBoxInDataGridSKJ.asp最好为内嵌控件添加Leave 事件,这样在增加新行时不会出错
    MSDN 上那个内嵌DateTimePicker的在增加新行时就有问题,我改了一下
    // This example shows how to create your own column style that
    // hosts a control, in this case, a DateTimePicker.
    public class DataGridTimePickerColumn : DataGridColumnStyle 
    {
    private DateTimePicker myDateTimePicker = new DateTimePicker();
    // The isEditing field tracks whether or not the user is
    // editing data with the hosted control.
    private bool isEditing;
    private int iRowNum;
    private CurrencyManager cmSource; public DataGridTimePickerColumn() : base() 
    {
    myDateTimePicker.Visible = false;
    myDateTimePicker.CustomFormat="yyyyM.dd  HH:mm";
    myDateTimePicker.Format=DateTimePickerFormat.Custom;
    myDateTimePicker.ShowUpDown=true;
    myDateTimePicker.Leave+=new EventHandler(myDateTimePicker_Leave);
    } protected override void Abort(int rowNum)
    {
    isEditing = false;
    myDateTimePicker.ValueChanged -= 
    new EventHandler(TimePickerValueChanged);
    Invalidate();
    } protected override bool Commit
    (CurrencyManager dataSource, int rowNum) 
    {
    myDateTimePicker.Bounds = Rectangle.Empty;
             
    myDateTimePicker.ValueChanged -= 
    new EventHandler(TimePickerValueChanged); if (!isEditing)
    return true; isEditing = false; try 
    {
    DateTime value = myDateTimePicker.Value;
    SetColumnValueAtRow(this.cmSource,this.iRowNum, value);

    catch (Exception) 
    {
    Abort(rowNum);
    return false;
    } Invalidate();
    return true;
    } protected override void Edit(
    CurrencyManager source, 
    int rowNum,
    Rectangle bounds, 
    bool readOnly,
    string instantText, 
    bool cellIsVisible) 
    {
    DateTime dateTime;

    this.iRowNum=rowNum;
    this.cmSource=source; if(System.DBNull.Value==GetColumnValueAtRow(source, rowNum))
    {
    myDateTimePicker.Bounds = new Rectangle(bounds.X , bounds.Y,bounds.Width , bounds.Height);
    myDateTimePicker.Visible=true;

    }
    else
    {
    dateTime=(DateTime)GetColumnValueAtRow(source,rowNum);

    if (cellIsVisible) 
    {
    myDateTimePicker.Bounds = new Rectangle(bounds.X, bounds.Y, bounds.Width,bounds.Height);
    myDateTimePicker.Value = dateTime;
    myDateTimePicker.Visible = true;
    myDateTimePicker.ValueChanged += 
    new EventHandler(TimePickerValueChanged);

    else 
    {
    myDateTimePicker.Value = dateTime;
    myDateTimePicker.Visible = false;
    }
    }
    // this.isEditing=true;
    } protected override Size GetPreferredSize(
    Graphics g, 
    object value) 
    {
    return new Size(100, myDateTimePicker.PreferredHeight + 4);
    } protected override int GetMinimumHeight() 
    {
    return myDateTimePicker.PreferredHeight + 4;
    } protected override int GetPreferredHeight(Graphics g, 
    object value) 
    {
    return myDateTimePicker.PreferredHeight + 4;
    } protected override void Paint(Graphics g, 
    Rectangle bounds, 
    CurrencyManager source, 
    int rowNum) 
    {
    Paint(g, bounds, source, rowNum, false);
    }
    protected override void Paint(
    Graphics g, 
    Rectangle bounds,
    CurrencyManager source, 
    int rowNum,
    bool alignToRight) 
    {
    Paint(
    g,bounds, 
    source, 
    rowNum, 
    Brushes.Black, 
    Brushes.Blue, 
    alignToRight);
    }
    protected override void Paint(
    Graphics g, 
    Rectangle bounds,
    CurrencyManager source, 
    int rowNum,
    Brush backBrush, 
    Brush foreBrush,
    bool alignToRight) 
    { string text;
    DateTime date;
    if(System.DBNull.Value==GetColumnValueAtRow(source, rowNum))
    {
    text=this.NullText;
    }
    else
    {
    date = (DateTime)GetColumnValueAtRow(source, rowNum);
    text=date.ToString("d");
    }
    Rectangle rect = bounds;
    g.FillRectangle(backBrush,rect);
    rect.Offset(0, 2);
    rect.Height -= 2;
    g.DrawString(text,this.DataGridTableStyle.DataGrid.Font,foreBrush, rect);
    } protected override void SetDataGridInColumn(DataGrid value) 
    {
    base.SetDataGridInColumn(value);
    if (myDateTimePicker.Parent != null) 
    {
    myDateTimePicker.Parent.Controls.Remove 
    (myDateTimePicker);
    }
    if (value != null) 
    {
    value.Controls.Add(myDateTimePicker);
    }
    } private void TimePickerValueChanged(object sender, EventArgs e) 
    {
    this.isEditing = true;
    base.ColumnStartedEditing(myDateTimePicker);
    } private void myDateTimePicker_Leave(object sender, EventArgs e)
    {
    object value = myDateTimePicker.Value;
    if(value==null)
    {
    value=System.DBNull.Value;
    }
    SetColumnValueAtRow(this.cmSource,this.iRowNum, value);
    this.myDateTimePicker.Visible=false;
    }
    }
      

  10.   

    //我把调用代码提炼了一下,你看看吧,这个是最基础的DataGrid列样式定义代码,一定要学会。//调用方法:
    //先建立一个类,把我贴的第一篇代码代码完全复制过去。
    //using System;
    //using System.Data;
    //using System.Drawing;
    //using System.Collections;
    //using System.Windows.Forms;//namespace ss_manage 
    //{
     //public class DataGridComboBoxColumn : System.Windows.Forms.DataGridTextBoxColumn 
     //{
     //..................
     //}
     //}
    ----------------------------------- #region Datagrid样式重载
    private void ss_gxdy21_ColumnStyle_Reload(DataGrid myDataGrid,bool editFlag,int typeFlag)
    {
    DataTable myDataTable = new DataTable();
    //声明DataGridTableStyle
    DataGridTableStyle myTableStyle = new DataGridTableStyle( );
    myTableStyle.MappingName = "ss_gxdy21"; //这个是DataGrid最原是的DataGridTextBoxColumn
    DataGridTextBoxColumn ColumnStyle2 = new DataGridTextBoxColumn();
    ColumnStyle2.MappingName = "C_CNAME";
    ColumnStyle2.HeaderText = "列名";
    myTableStyle.GridColumnStyles.Add(ColumnStyle2); //从这里开始调用扩展功能的DataGridTextBoxColumn,也就是
    sql = "SELECT 代码,名称 FROM 字典表";
    //这里我用自己的方法生成了一个DataTable,这个DataTable只需包括ComboBox对应的内部值和显示值两列就行了。一般从字典里生成
    myDataTable = myWs.GetDs(sql,"ColumnName").Tables[0];

    //这里开始使用DataGridComboBoxColumn这个类
    DataGridComboBoxColumn ColumnStyle7=new DataGridComboBoxColumn(myDataTable, myDataTable.Columns[1].ColumnName, myDataTable.Columns[0].ColumnName, true, false, myDataGrid);
    ColumnStyle7.MappingName = "C_TYPE";//把mapingname和headertext对应改一下就行了。其他不用该
    ColumnStyle7.HeaderText = "类型";
    myTableStyle.GridColumnStyles.Add(ColumnStyle7); //................

    myDataGrid.TableStyles.Clear();
    myDataGrid.TableStyles.Add( myTableStyle );
    }
    #endregion
      

  11.   

    如下:有详细的说明
    http://blog.csdn.net/zhzuo/archive/2004/05/31/22036.aspx
    有问题在我的blog上留言
      

  12.   

    我是否能只修改以下内容
    DataGridTableStyle dgdtblstyle=new DataGridTableStyle();
    dgdtblstyle.MappingName="checkbox";
    datagrid1.TableStyles.Add(dgdtblstyle);
    DataGridTextBoxColumn dgtb=(DataGridTextBoxColumn)datagrid1.TableStyles[0].GridColumnStyles[2];
    ComboBox cmb=new ComboBox();
         cmb.DataSource=dataSet21.Tables["customers"];
    cmb.DisplayMember="customersid";
    cmb.ValueMember="customersid";

    //cmb.Items.AddRange(new object[]{"1111","2222","3333","5555"});
    cmb.Cursor=Cursors.Arrow;
    cmb.Dock=DockStyle.Fill;

    cmb.DropDown+=new EventHandler(cmb_SelectionChangeCommitted);
    dgtb.TextBox.Controls.Add(cmb);private void cmb_SelectionChangeCommitted(object sender,EventArgs e)
    {
    this.datagrid1[this.datagrid1.CurrentCell]=((ComboBox)sender).SelectedItem.ToString();
    this.BindingContext[this.datagrid1.DataSource,this.datagrid1.DataMember].EndCurrentEdit(); 
    }现在的问题是,我可以从数据库中将数据放到下拉列表中,但是选择所要数据后,显示system.dat 
    不知为何