简体   繁体   中英

c# winforms gridview and selection boxes

I have a winforms c# app that displays tabular data from a DB into a gridview control.

I need to programmatically add a final column with a tickbox for each row, in order to find out which rows have been ticked form the current view.

How to go about doing something like that, as the tickbox column does not exist in the DB?

You could add the extra column directly to your datasource before binding to the DataGridView. Supposing that you are using a DataTable then

 DataColumn dc = table.Columns.Add("Select", typeof(bool));
 dc.DefaultValue = false;
 grid.DataSource = dt;

Another method is define a DataGridViewCheckBoxColumn() and append to the current column list

 checkCol = new DataGridViewCheckBoxColumn();
 checkCol.HeaderText = "Select";     
 checkCol.Width = 80;
 checkCol.ReadOnly = false;         
 grid.Columns.Add(checkCol);  

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