如何为DataGridView添加一个定制的Column Type
这个例子实现了一个把数据中的Boolean值用Y或者N在DataGridView里面显示,步骤如下:
1. 建立一个继承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 && (bool)value == true)
{
graphics.DrawString(“Y”, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), cellBounds.X, cellBounds.Y);
}
}
}
}
2. 建立一个继承自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();
}
}
}
然后把你的DataGridView里面的Boolean列的ColumnType改成以上的这个就可以了
1. 建立一个继承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 && (bool)value == true)
{
graphics.DrawString(“Y”, cellStyle.Font, new SolidBrush(cellStyle.ForeColor), cellBounds.X, cellBounds.Y);
}
}
}
}
2. 建立一个继承自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();
}
}
}
然后把你的DataGridView里面的Boolean列的ColumnType改成以上的这个就可以了
【如何为DataGridView添加一个定制的Column Type】相关文章
1. 如何为DataGridView添加一个定制的Column Type
4. 什么是Connection-oriented Protocol/Connectionless Protocol面向连接的协议/无连接协议
5. 介绍JAVA 中的Collection FrameWork(及如何写自己的数据结构)
6. 什么是ARP(Address Resolution Protocol)地址解析协议
10. 介绍一下ICMP(Internet Control Message Protocol)Internet控制信息协议
本文来源:https://www.mianshiwenti.com/a12856.html
进入下载页面
上一篇:Ref与out有什么不同
下一篇:程序集与命名空间有什么不同
﹝如何为DataGridView添加一个定制的Column Type﹞相关内容
- Collection和Collections的区别
- 软件测试LoadRunner面试题: Types of Goals in Goal-Oriented Scenario
- 声明struct x1 { . . . }; 和typedef struct { . . . }x2;有什么不同
- 介绍一下Prototype的$()函数,$F()函数,$A()函数都是什么作用?
- Prototype是怎么扩展DOM的
- 软件测试LoadRunner面试题:What is the difference between Overlay graph and Correlate graph?
- Linux如何为某个操作添加别名
- Boolean b = new Boolean(“abcde”); 会编译错误码
- Java里面如何把一个Array数组转换成Collection, List
- OLEDBConnection和SQLConnection有什么区别