如何为DataGridView添加一个定制的Column Type
编辑: 面试啦 发布时间: 2023-09-06 19:02:52
这个例子实现了一个把数据中的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改成以上的这个就可以了
词条:net面试题
上一篇:请解释流与文件有什么不同 下一篇:委托与事件是什么关系?为什么要使用委托
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题