开发学院软件开发C语言 C# WinForm下DataGridView单元按钮列 阅读

C# WinForm下DataGridView单元按钮列

 2009-07-01 07:07:41 来源:WEB开发网   
核心提示:在C# WinForm下做过项目的朋友都知道,其中的DataGridView控件默认只支持DataGridViewButtonColumn、DataGridViewCheckBoxColumn、DataGridViewComboBoxColumn、DataGridViewImageColumn、DataGridView

在C# WinForm下做过项目的朋友都知道,其中的DataGridView控件默认只支持DataGridViewButtonColumn、DataGridViewCheckBoxColumn、DataGridViewComboBoxColumn、DataGridViewImageColumn、DataGridViewLinkColumn和DataGridViewTextBoxColumn六种列类型,如果你想要在DataGridView的列中添加其它的子控件,则需要自己实现DataGridViewColumn和DataGridViewCell,这就意味着你需要从现有的列中继承并改写一些方法,如实现一个支持单选按钮的列,或支持三种选择状态的多选按钮的列。

C# WinForm下DataGridView单元按钮列

C# WinForm下DataGridView单元按钮列    

上面两个截图分别为RadioButton列和支持三种状态的CheckBox列在DataGridView中的实现效果,我是在Windows 2003中实现的,因此显示的效果跟在XP和Vista下有些区别,Vista下CheckBox的第三种状态(不确定状态)显示出来的效果是一个实心的蓝色方块。

下面我看具体来看看如何实现这两种效果。

要实现自定义的DataGridView列,你需要继承并改写两个类,一个是基于DataGridViewColumn的,一个是基于DataGridViewCell的,因为RadionButton和CheckBox的实现原理类似,因此我们可以将这两种列采用同一种方法实现。创建DataGridViewDisableCheckBoxCell和DataGridViewDisableCheckBoxColumn两个类,分别继承自DataGridViewCheckBoxCell和DataGridViewCheckBoxColumn。代码如下:

public class DataGridViewDisableCheckBoxCell: DataGridViewCheckBoxCell
{
    public bool Enabled { get; set; }

    // Override the Clone method so that the Enabled property is copied.
    public override object Clone()
    {
        DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base.Clone();
        cell.Enabled = this.Enabled;
        return cell;
    }

    // By default, enable the CheckBox cell.
    public DataGridViewDisableCheckBoxCell()
    {
        this.Enabled = true;
    }

    // Three state checkbox column cell
    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
        DataGridViewElementStates elementState, object value, object formattedValue, string errorText,
        DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {
        // The checkBox cell is disabled, so paint the border, background, and disabled checkBox for the cell.
        if (!this.Enabled)
        {
            // Draw the cell background, if specified.
            if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
            {
                SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor);
                graphics.FillRectangle(cellBackground, cellBounds);
                cellBackground.Dispose();
            }

            // Draw the cell borders, if specified.
            if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
            {
                PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
            }

            // Calculate the area in which to draw the checkBox.
            CheckBoxState state = CheckBoxState.MixedDisabled;
            Size size = CheckBoxRenderer.GetGlyphSize(graphics, state);
            Point center = new Point(cellBounds.X, cellBounds.Y);
            center.X += (cellBounds.Width - size.Width) / 2;
            center.Y += (cellBounds.Height - size.Height) / 2;

            // Draw the disabled checkBox.
            CheckBoxRenderer.DrawCheckBox(graphics, center, state);
        }
        else
        {
            // The checkBox cell is enabled, so let the base class, handle the painting.
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
        }
    }
}



public class DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn
{
    public DataGridViewDisableCheckBoxColumn()
    {
        this.CellTemplate = new DataGridViewDisableCheckBoxCell();
    }
}

1 2 3 4  下一页

Tags:WinForm DataGridView 单元

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读