简体   繁体   中英

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 ? Changing the cellTemplate causes a error

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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