Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a checkbox control to a datatable?

Tags:

c#

.net

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.

like image 958
NewBie Avatar asked Apr 12 '11 05:04

NewBie


People also ask

How to use check box in DataTable?

A column can be shown with a checkbox that reflects the row's selected status simply through the use of the select-checkbox CSS class for that column ( columns. className ). Row selection can be restricted to that column using the select. selector option.

How check checkbox is checked or not in jquery DataTable?

function uncheckRow(trId) {<br> var table = $('#example'). DataTable(); var row = table. row("#"+trId); var rowData = row. data(); var name = rowData[1]; var age = rowData[2]; // need to check if the check box in the 1st column(rowData[0]) is checked or not, If it is checked, i have to uncheck … }

How do I select all checkboxes from all pages in jquery DataTable grid?

nodes(); // Check/uncheck checkboxes for all rows in the table $('input[type="checkbox"]', rows). prop('checked', this. checked); }); // Handle click on checkbox to set state of "Select all" control $('#example tbody'). on('change', 'input[type="checkbox"]', function(){ // If checkbox is not checked if(!


1 Answers

You'll have to have a boolean field (column) in the DataTable. When you bind the DataTable to the DataGridView, a checkbox column will be created for that boolean field.

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. The DataGridView will automatically display a DataGridViewCheckBoxColumn for the first DataColumn (Selected) and a DataGridViewTextBoxColumn for the second DataColumn (Text).

like image 170
Alex Essilfie Avatar answered Oct 01 '22 12:10

Alex Essilfie