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.
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.
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 … }
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(!
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With