zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    7-3.自定义列表
    GoLang 使用协程与管道随机生成姓名
    [Unity3D] 点击物品显示物品信息
    [Unity3D] 碰撞物体添加到背包
    [PS] DDS文件导入插件
    [Unity3D] 给角色添加武器
    [Unity3D] 刚体 碰撞器 触发器
    [Unity3D] 物体的几种移动方法
    [Unity3D] 解决导入的模型出现闪烁的问题
    [Unity3D] 人物角色跳跃(动画跳跃&刚体跳跃)
  • 原文地址:https://www.cnblogs.com/jcdd-4041/p/3431037.html
Copyright © 2011-2022 走看看