![](/img/trans.png)
[英]DataGridView CheckBox Column - Show CheckBox but store String
[英]Show CheckBox column for Nullable<bool> properties in DataGridView
經過大量搜索后,我無法找到解決此問題的方法。 我已經使用如下列表成功設置了DatagridView
的DataSource
。
class
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。 除最后一個 boolean 字段 IncludeInMkt 外,數據網格顯示所有數據。 它顯示文本值(真/假),而我希望它顯示為帶有 chInfoList 中相應值的復選框。 我還在設計器中將 TrueValue 設置為 True,將 FalseValue 設置為 False。
我哪里錯了,請建議。
DataGridView
將為bool
屬性生成DataGridViewCheckBoxColumn
。 但是對於bool?
屬性它將生成DataGridViewTextBoxColumn
。
您可以在設計時或運行時修復它,方法是用DataGridViewCheckBoxColumn
替換生成的列並將其ThreeState
屬性設置為 true。
示例 - 在 DataGridView 中顯示Nullable<bool>
的復選框
以下 function 將生成的列替換為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);
});
}
在上面的表格中,我使用了以下 model:
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
顯示為bool
或bool?
列,看看這篇文章,它對bool
屬性有同樣的作用,並對其進行一些更改以支持bool?
也是。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.