c# - 为 Nullable 显示 CheckBox 列DataGridView 中的属性
问题描述
经过大量搜索后,我无法找到解决此问题的方法。我已经成功设置了DataSource
一个DatagridView
使用列表,如下所示。
班上
public class ChannelInfo
{
[Browsable(false)]
[DisplayName("ChannelId")]
public int channelId { get; set; }
[DisplayName("Channel")]
public string sysName { get; set; }
[DisplayName("Display Name")]
public string dispName { get; set; }
[DisplayName("Unit")]
public string unit { get; set; }
[DisplayName("Divide By")]
public int divideBy { get; set; }
[DisplayName("YAxis")]
public string yAxis { get; set; }
[DisplayName("Min Scale")]
public int scaleMin { get; set; }
[DisplayName("Max Scale")]
public int scaleMax { get; set; }
[DisplayName("Colour")]
public string colour { get; set; }
[DisplayName("Set Point")]
public double setPoint { get; set; }
[DisplayName("Limit(+/-)")]
public double? limit { get; set; }
[DisplayName("MKT")]
public bool? IncludeInMKT { get; set; }
/// <summary>
/// Default constructor
/// </summary>
public ChannelInfo()
{
}
/// <summary>
/// Copy constructor to create a copy of another object
/// </summary>
/// <param name="ci"> and object of the type ChannelInfo whos copy is to be created</param>
public ChannelInfo(ChannelInfo ci)
{
channelId = ci.channelId;
sysName = ci.sysName;
dispName = ci.dispName;
unit = ci.unit;
divideBy = ci.divideBy;
yAxis = ci.yAxis;
scaleMin = ci.scaleMin;
scaleMax = ci.scaleMax;
colour = ci.colour;
setPoint = ci.setPoint;
limit = ci.limit;
IncludeInMKT = ci.IncludeInMKT;
}
}
设置网格的数据源
static List<ChannelInfo> chInfoList;
dgvChannels.DataSource = chInfoList;
使用设计器将数据网格的最后一列的类型设置为 DataGridViewCheckBoxColumn。除最后一个布尔字段 IncludeInMkt 外,数据网格显示所有数据。它显示文本值(真/假),而我希望它显示为带有 chInfoList 中相应值的复选框。我还在设计器中将 TrueValue 设置为 True,将 FalseValue 设置为 False。
我哪里错了,请建议。
解决方案
DataGridView
DataGridViewCheckBoxColumn
将为bool
属性生成。但对于bool?
属性,它会生成DataGridViewTextBoxColumn
.
您可以在设计时或运行时通过将生成的列替换为 aDataGridViewCheckBoxColumn
并将其ThreeState
属性设置为 true 来修复它。
Nullable<bool>
示例 -在 DataGridView 中显示 CheckBox
以下函数将生成的bool?
属性列替换为树状态复选框列:
public void UseCheckBoxForNullableBool(DataGridView g)
{
g.Columns.Cast<DataGridViewColumn>()
.Where(x => x.ValueType == typeof(bool?))
.ToList().ForEach(x =>
{
var index = x.Index;
g.Columns.RemoveAt(index);
var c = new DataGridViewCheckBoxColumn();
c.ValueType = x.ValueType;
c.ThreeState = true;
c.DataPropertyName = x.DataPropertyName;
c.HeaderText = x.HeaderText;
c.Name = x.Name;
g.Columns.Insert(index, c);
});
}
在上面的表格中,我使用了以下模型:
public class Test
{
public int MyProperty1 { get; set; }
public bool? MyProperty2 { get; set; }
}
并应用将UseCheckBoxForNullableBool
生成的bool?
属性列更改为树状态复选框列:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.dataGridView1.DataSource = new BindingList<Test>() {
new Test(){ MyProperty1 = 1, MyProperty2= null},
new Test(){ MyProperty1 = 2, MyProperty2= true},
new Test(){ MyProperty1 = 3, MyProperty2= false},
};
UseCheckBoxForNullableBool(dataGridView1);
}
注意:如果您有兴趣显示ComboBox
forbool
或bool?
列,请查看这篇文章bool
,它对属性执行相同的操作,并对其进行一些更改以支持bool?
。
推荐阅读
- react-native - 如何优化 React Native 应用程序和 FalstList 的速度
- c# - 轰炸机的爆炸有时无法正常工作
- javascript - 使用 Spring Security 时,有什么方法可以使用引导模板登录?
- asp.net-mvc - MVC.NET 如何在编辑后保护提交表单
- mysql - 比较两个数据库表中的两个值
- angular - TSLint:禁止导入延迟加载的库:降级组件、在 AppModule 和角度引导中导入的模块
- javascript - Vue.js 应用程序从新页面的下拉菜单中打开 url 链接
- python - 如何通过python将生成的文本文件转换为tsv数据形式?
- ruby-on-rails - Selecting a nested record to share show page of with selected users in action mailer via checkbox selection
- postgresql - 不在 Postgresql 中使用“开始事务”和“提交”语句