winform 制定DataGridViewTextColumn列(更改DataGridView的Cell的状态很有用)

先自定义一个类 继承DataGridViewTextBoxCell

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace com.Threes.CustomControl
{
    public class DataGridViewBooleanCell : DataGridViewTextBoxCell
    {
        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)
        {
            // Call the base class method to paint the default cell appearance.
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, "", errorText, cellStyle, advancedBorderStyle, paintParts);
            
            if (value is Boolean)
                if ((bool)value == true)
                {
                    graphics.DrawString("Y", cellStyle.Font, new SolidBrush(Color.Blue), cellBounds.X, cellBounds.Y);
                }
                else
                {
                    graphics.DrawString("N", cellStyle.Font, new SolidBrush(Color.Red), cellBounds.X, cellBounds.Y);
                }
            else if (value is int)
            {
                int v = (int)value;
                if (v == 1)
                {
                    graphics.DrawString("", cellStyle.Font, new SolidBrush(Color.Blue), cellBounds.X, cellBounds.Y);
                }
                else if (v == 0)
                {
                    graphics.DrawString("", cellStyle.Font, new SolidBrush(Color.Red), cellBounds.X, cellBounds.Y);
                }
                else
                {
                    graphics.DrawString("人妖", cellStyle.Font, new SolidBrush(Color.Green), cellBounds.X, cellBounds.Y);
                }
            }
        }

    }

}

再自定义一个类 继承DataGridViewColumn

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace com.Threes.CustomControl
{
    public class DataGridViewBooleanColumn : DataGridViewColumn
    {
        public DataGridViewBooleanColumn()
        {
            this.CellTemplate = new DataGridViewBooleanCell();
        }
    }
}

借鉴与:http://www.mianwww.com/html/2009/06/3345.html

转载于:https://www.cnblogs.com/jcdd-4041/p/3431037.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值