我想在datagridview每个列的列头添加一个combobox,但是不知道怎么绘制combobox我用这个方法添加了checkbox列头
   class datagridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
        {
            Point checkBoxLocation;
            Size checkBoxSize;
            bool _checked = false;
            Point _cellLocation = new Point();
            System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
            public event datagridviewcheckboxHeaderEventHander OnCheckBoxClicked;
            //绘制列头checkbox
            protected override void Paint(System.Drawing.Graphics graphics,
            System.Drawing.Rectangle clipBounds,
            System.Drawing.Rectangle cellBounds,
            int rowIndex,
            DataGridViewElementStates dataGridViewElementState,
            object value,
            object formattedValue,
            string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                dataGridViewElementState, value,
                formattedValue, errorText, cellStyle,
                advancedBorderStyle, paintParts);
                Point p = new Point();
                Size s = CheckBoxRenderer.GetGlyphSize(graphics,
                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
                p.X = cellBounds.Location.X +
                (cellBounds.Width / 2) - (s.Width / 2) - 1;//列头checkbox的X坐标
                p.Y = cellBounds.Location.Y +
                (cellBounds.Height / 2) - (s.Height / 2);//列头checkbox的Y坐标
                _cellLocation = cellBounds.Location;
                checkBoxLocation = p;
                checkBoxSize = s;
                if (_checked)
                    _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.CheckedNormal;
                else
                    _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.UncheckedNormal;
                CheckBoxRenderer.DrawCheckBox
                (graphics, checkBoxLocation, _cbState);
            }
        }
但是不知道怎样用这个方法绘制一个combobox,我在网上找到下面这个方法,但是好像只能绘制出个箭头protected override void Paint(
            Graphics graphics, Rectangle clipBounds, Rectangle cellBounds,
            int rowIndex, DataGridViewElementStates cellState,
            object value, object formattedValue, string errorText,
            DataGridViewCellStyle cellStyle,
            DataGridViewAdvancedBorderStyle advancedBorderStyle,
            DataGridViewPaintParts paintParts)
        {
            // Use the base method to paint the default appearance. 
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
                cellState, value, formattedValue,
                errorText, cellStyle, advancedBorderStyle, paintParts);
            // Retrieve the current button bounds. 
            Rectangle buttonBounds = DropDownButtonBounds;
            // Continue only if the buttonBounds is big enough to draw.
            if (buttonBounds.Width < 1 || buttonBounds.Height < 1) return;
            if (Application.RenderWithVisualStyles)
            {
                ComboBoxState state = ComboBoxState.Normal;
                //if (dropDownListBoxShowing)
                //{
                //    state = ComboBoxState.Pressed;
                //}
                ComboBoxRenderer.DrawDropDownButton(
                    graphics, buttonBounds, state);
            }
            else
            {
                // Determine the pressed state in order to paint the button 
                // correctly and to offset the down arrow. 
                Int32 pressedOffset = 0;
                PushButtonState state = PushButtonState.Normal;
                //if (dropDownListBoxShowing)
                //{
                //    state = PushButtonState.Pressed;
                //    pressedOffset = 1;
                //}
                ButtonRenderer.DrawButton(graphics, buttonBounds, state);
                graphics.DrawPolygon(SystemPens.ControlText, new Point[] {
                        new Point(
                            buttonBounds.Width / 2 + 
                                buttonBounds.Left - 1 + pressedOffset, 
                            buttonBounds.Height * 3 / 4 + 
                                buttonBounds.Top - 1 + pressedOffset),
                        new Point(
                            buttonBounds.Width / 4 + 
                                buttonBounds.Left + pressedOffset,
                            buttonBounds.Height / 2 + 
                                buttonBounds.Top - 1 + pressedOffset),
                        new Point(
                            buttonBounds.Width * 3 / 4 + 
                                buttonBounds.Left - 1 + pressedOffset,
                            buttonBounds.Height / 2 + 
                                buttonBounds.Top - 1 + pressedOffset)
                    });
            }        }谁有比较好的办法实现呢?

解决方案 »

  1.   

    直接在datagridview里加各模版列,里面放一个combobox就好啦
      

  2.   

    这是winform 不是webform的啊 哪来的模板列呢?
      

  3.   

    winform不是很清楚,应该也可以吧。
      

  4.   

    http://msdn.microsoft.com/en-us/library/aa480727.aspx
      

  5.   

    不需要绘制,datatridvie里本身就有combobox。
    类型是DataGridViewComboBoxColumn。
    我觉得系统自带的combobox应该能满足你的需求了。
    你好好看看datagridview的ColumnType吧
      

  6.   

    在窗体的编辑界面,在DataGridView上右键,添加列,在列的类型里选择datagridviewcomboboxcolumn这个就可以了啊
    就把一个下拉框添加进来了
    可以给它附加数据源代码:
     string sql = "select ck_name from  WH_cangku";
                DataSet ds = new DataSet();
                ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql);//这里是执行sql语句的
                this.Column19.DataSource = ds.Tables[0];
                this.Column19.DisplayMember = "ck_name";
      

  7.   

    的确是不需要绘制,因为datatridvie里本身就有combobox。类型是DataGridViewComboBoxColumn。
    正如LS所说的系统本身自带的combobox就能满足你的需求了。
    LZ还是要好好看看的 慢慢就会更好的 加油哈~
    http://www.uplookingsh.com
      

  8.   

    DataGridViewComboBoxColumn 是设置列的格式的 但是列头上没法用这个添加combobox吧?
      

  9.   

    DataGridViewComboBoxColumn 是设置列的格式的 但是列头上没法用这个添加combobox吧?