简体   繁体   English

c#winforms gridview和选择框

[英]c# winforms gridview and selection boxes

I have a winforms c# app that displays tabular data from a DB into a gridview control. 我有一个winforms c#app,它将数据库中的表格数据显示为gridview控件。

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? 如何做这样的事情,因为数据库中不存在tickbox列?

You could add the extra column directly to your datasource before binding to the DataGridView. 在绑定到DataGridView之前,您可以将额外的列直接添加到数据源。 Supposing that you are using a DataTable then 假设您正在使用DataTable

 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 另一种方法是定义DataGridViewCheckBoxColumn()并追加到当前列列表

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

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

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