简体   繁体   English

如何将复选框控件添加到数据表?

[英]How to add a checkbox control to a datatable?

How can i add a checkbox to a datatable and bind it to a datagrid? 如何在数据表中添加复选框并将其绑定到数据网格?

DataTable ColumnList = new DataTable();
ColumnList.Columns.Add("Column Fields");

int j = 1, i = 0;
CheckBox colCheckbox = new CheckBox();
foreach (Columns col in ColumnNames)
{
    colCheckbox.Name = col.ColumnName;       
    ColumnList.Rows.Add(colCheckbox); // This is getting displayed as System.Windows.Forms.CheckBox,CheckState=0
}

Please help. 请帮忙。

You'll have to have a boolean field (column) in the DataTable. 你必须在DataTable中有一个boolean字段(列)。 When you bind the DataTable to the DataGridView, a checkbox column will be created for that boolean field. 将DataTable绑定到DataGridView时,将为该boolean字段创建一个复选框列。

Sample Code: 示例代码:

var dt = new DataTable();
dt.Columns.Add(new DataColumn("Selected", typeof(bool))); //this will show checkboxes
dt.Columns.Add(new DataColumn("Text", typeof(string)));   //this will show text

var dgv = new DataGridView();
dgv.DataSource = dt;

This will bind the dt DataTable to the dgv DataGridView. 这会将dt DataTable绑定到dgv DataGridView。 The DataGridView will automatically display a DataGridViewCheckBoxColumn for the first DataColumn ( Selected ) and a DataGridViewTextBoxColumn for the second DataColumn ( Text ). 在DataGridView将自动显示一个DataGridViewCheckBoxColumn用于第一的DataColumn( 选择的 )和DataGridViewTextBoxColumn用于第二的DataColumn( 文本 )。

You have to specify the type of the column. 您必须指定列的类型。

ColumnList.Columns.Add("Column Fields", gettype(CheckBox)); 

But why do you want to add a control to a DataTable? 但是为什么要将控件添加到DataTable? Why not just a string or an integer? 为什么不只是一个字符串或整数?

暂无
暂无

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

相关问题 如何在DataTable中添加复选框? - How to add checkbox in DataTable? 如何添加复选框(输入html控件),该复选框将动态显示在asp.net的数据表中? - How to add checkbox(input html control) which will be appear dynamically in datatable in asp.net? 如何在DatagridviewCheckBoxColumn的Header添加复选框控件? - How to add checkbox control at the Header of DatagridviewCheckBoxColumn? 如何在 DataGrid 控件中添加复选框列? - How to add CheckBox Column in DataGrid Control? 如何在数据表中的行旁边添加复选框以能够选择每一行 - How to add a checkbox beside the rows in datatable to be able to select each row 如何在datagrid控件的每一行的开头添加复选框控件? - How to add checkbox control at the beginning of every row in datagrid control? 如何在网格控制dotnetbar winforms中添加复选框作为列标题 - How to add checkbox as column header in grid control dotnetbar winforms 如何以编程方式将复选框控件添加到Excel单元格或选中或取消选中现有复选框 - How to add a checkbox control to an Excel cell programatically or check or uncheck an existing checkbox 如何在checkboxlist控件中的复选框之前或之后添加AjaxControlToolkit的Gravatar控件 - How to add AjaxControlToolkit's Gravatar Control before or after checkbox in checkboxlist control DataTable-添加一列(复选框)-将DataTable行读入数组-将数组作为DataTable中的行添加 - DataTable - add a column (checkbox) - read DataTable row into an Array - add an array as row in a DataTable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM