简体   繁体   English

C#Winforms使用CheckBoxColumn在DataGridView中使用AutoGenerateColumns

[英]C# Winforms use AutoGenerateColumns in DataGridView with a CheckBoxColumn

I use a DataGridView with AutoGenerateColumns but would like to display one of the columns (integer) as a CheckBoxColum, is there an easy way or do I have to set AutoGenerateColumns off and add all of the columns manually ? 我使用带有AutoGenerateColumns的DataGridView,但想显示一个列(整数)作为CheckBoxColum,是否有一个简单的方法,还是我必须关闭AutoGenerateColumns并手动添加所有列? Changing the cellTemplate causes a error 更改cellTemplate会导致错误

dgvKlanten.AutoGenerateColumns = true;
dgvKlanten.DataSource = dsKlantenBeheer.Tables["Klanten"];
dgvKlanten.Columns["Nieuwsbrief"].Width = 70;
//dgvKlanten.Columns["Nieuwsbrief"].CellTemplate = new DataGridViewCheckBoxCell();

You could just hide the auto-generated column for that column, then manually add a checkbox column to take its place. 您可以隐藏该列的自动生成列,然后手动添加复选框列以取代它。

On a side note, I'm assuming this integer column only supports values zero and one? 另外,我假设这个整数列只支持零和一个值? Since this is an integer and not a boolean like the checkbox expects, you may have to use the grid events, such as CellFormatting, to read the value from the data source and set the checkbox accordingly, but I'm not sure. 由于这是一个整数而不是像复选框所期望的布尔值,您可能必须使用网格事件(如CellFormatting)从数据源中读取值并相应地设置复选框,但我不确定。 Or, if this is an object, you could expose the integer value as a boolean using another, custom property. 或者,如果这是一个对象,您可以使用另一个自定义属性将整数值公开为布尔值。 No matter what, you are likely going to need to do more work than simply adding a checkbox column and pointing it to the datasource column. 无论如何,您可能需要做更多的工作,而不仅仅是添加一个复选框列并将其指向数据源列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM