北天软件工作室
北天软件集网站建设、网站开发、软件开发、网站优化SEO、网站宣传、网站开发成一体的网络公司。北天软件是专业的网站建设、网站开发、设计、制作和网站国际推广、搜索引擎推广的网络公司。口号:考虑企业所需,实现企业所想。JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!   旧版java技术网 | 设为首页 | 文章搜索 | RSS订阅地图
免费使用JavaCMS自助建站系统
  文章搜索:   
初学者园地  javascript  java技术  .Net技术 XML/WebService  数据库技术  web2.0技术  设计模式  设计框架  SEO技术  综合知识
您现在的位置是: 北天软件门户网>>.Net技术>>详细信息
C# WinForm下DataGridView单元按钮列
欢迎进入.NET社区论坛,与200万技术人员互动交流 >>进入

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

6-22-2009 3-32-18 PM

6-22-2009 3-31-44 PM     上面两个截图分别为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();
    }
}
       主要是要实现DataGridViewDisableCheckBoxCell的呈现方式,其中设置了CheckBoxState的状态为MixedDisabled,表示支持三种状态,这个是实现效果的核心,如果要实现RadioButton列的效果,只需要将Paint方法改成下面这样即可:

    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)
    {
        // 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.
        RadioButtonState state = value != null && (SelectedStatus)value == SelectedStatus.Selected ? RadioButtonState.CheckedNormal : RadioButtonState.UncheckedNormal;
        Size size = RadioButtonRenderer.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.
        RadioButtonRenderer.DrawRadioButton(graphics, center, state);
    }
      使用RadioButtonState代替CheckBoxState。

      当然,上面的代码只是实现了列和单元格的显示效果,在使用过程中当用户点击单选或多选按钮时如何改变状态则需要自己手动编写代码来实现,如在点击单选按钮时将DataGridView中其它行的单选按钮设置为未选择状态,点击多选按钮时在三种状态之间转换等。

      首先我们需要手动修改Form的Designer.cs文件中的代码,将CheckBox所在的列的类型由DataGridViewCheckBoxColumn改成DataGridViewDisableCheckBoxColumn,并设置ThreeState的值为true,这个代码是需要手动去修改的,因为默认情况下VS不支持对自定义DataGridView列类型进行可视化编辑。要支持CheckBox的三种状态,我们还需要定义一个枚举来给CheckBox的TrueValue、FalseValue和IndeterminateValue赋值。这个枚举很简单,有三个成员就够了。

public enum SelectedStatus
{
    Selected,
    NoSelected,
    Indeterminate
}
      然后设置CheckBox的TrueValue=SelectedStatus.Selected,FalseValue=SelectedStatus.NoSelected,IndeterminateValue=SelectedStatus.Indeterminate。

      好了!这个时候运行程序,可以看到经过我们改造的列已经可以正常显示了,但是有一个问题,

[1] [2] 下一页

关闭窗口 】   【 返回首页
推荐文章
· 再谈IIS与ASP.NET管...
· ASP.NET中缓存服务器...
· 趣味理解ADO.NET对象...
· ASP.NET多附件上传和...
· ASP.NET刷新页面的六...
· 2009业务流程管理(BPM...
· 在.NET环境下为网站...
· 扩展方法(2) Gri...
· 浅谈.net 中的...
· C#实现的多线程异步So...
· 在 Java 平台上进行...
· 多线程Java 应用程序...
· 通过Java编程处理XML...
· 详解SQL中FOR XML子...
· 总结用XML配置的十二...
· Windows 7采用Xml格...
· 在Silverlight3中消耗...
· Eclipse SDK 3.5RC2(S...
· NetBeans 6.7 RC1发布
· JDK7中的Java NIO.2 F...
北天软件工作室 粤ICP备06079815号 版权所有©2006-2008
精彩出品 JavaCMS自助建站 (C)2006-2008 www.it3838.com limited.all rights reserved.
Powered by JavaCMS V2.6.0