[Solved] Problem with datagrid [datatable combine gridCheckboxcolumn]!

The error I encountered when I clicked on the gridcheckbox column as in the image, this is the code I used to create the datagrid:
string[] header = new[] { "Column1", "Column2", "Column3" };
MyTable = new DataTable();

foreach (string str in header)
    MyTable.Columns.Add(str, typeof(string));

MyTable.Rows.Add({"abc","abc1","abc2"});
f_DatagridInfo.DataSource = MyTable;

this.f_DatagridInfo.Columns.Add(new GridCheckBoxColumn()
{
    MappingName = "Active",
    HeaderText = "Active",
    CheckBoxSize = new Size(12, 12)
});
However I do not understand where the error is when i check columnCheckbox?

Cannot set Active.

************** Exception Text **************
System.Data.DataException: Cannot set Active.
   at System.Data.DataRowView.set_Item(String property, Object value)
   at Syncfusion.Data.ItemPropertiesProvider.SetDataTableView(Object record, String propName, Object value)
   at Syncfusion.WinForms.DataGrid.Renderers.GridCheckBoxCellRenderer.ChangeCheckBoxValue(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, CheckState state)
   at Syncfusion.WinForms.DataGrid.Renderers.GridCheckBoxCellRenderer.OnMouseUp(DataColumnBase dataColumn, RowColumnIndex rowColumnIndex, MouseEventArgs e)
   at Syncfusion.WinForms.DataGrid.GridHandler.OnMouseUp(Object sender, MouseEventArgs e)
   at System.Windows.Forms.MouseEventHandler.Invoke(Object sender, MouseEventArgs e)
   at System.Windows.Forms.Control.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks).


I want to create a table with 4 columns( last column is checkboxcolumn)
they don't have data yet, and after clicking button I will add data one by one (the default column checked = false)

4 Replies

TG The GridLock April 29, 2020 06:17 AM UTC

Hi,
I have 2nd question: why did it take me 2 clicks to make the checkbox column actually work!
First time: when I click the checkbox but only the row is selected while the checkbox is not checked (I think this might only work for the active datagrid), agree!
But : if I click on another row, the checkbox still doesn't work, only the selected row is selected.
Then click the 3rd time, now it works.
How should I handle it?

Dim header() As String = {"Column1", "Column2", "Column3"}
Mytable = New DataTable
Dim i As SByte
For Each str As String In header
    MyTable.Columns.Add(str, GetType(String))
Next
MyTable.Columns.Add("Active", GetType(Boolean))
f_DatagridInfo.DataSource = MyTable
Dim addNewRow As System.Data.DataRow = TryCast(f_DatagridInfo.View.AddNew(), System.Data.DataRow)
f_DatagridInfo.View.CommitNew()

addNewRow.SetField(0, "abc1")
addNewRow.SetField(1, "abc2")
addNewRow.SetField(2, "abc3")
addNewRow.SetField(3, false)
Edit:question 2 has been solved, but I want to use question 1 because the checkbox size here is quite large (It does not automatically resize with the office2019colorful theme)



SS Susmitha Sundar Syncfusion Team April 29, 2020 08:35 AM UTC

Hi GridLock, 
 
Thank you for the update. 
 
You can change the GridCheckBoxColumn size in AutoGeneratingColumnEvent. Please refer the below code example and UG link, 
 
public partial class Form1 : Form 
{ 
     Random r = new Random(); 
     public DataTable dt; 
     public Form1() 
     { 
         InitializeComponent(); 
         dt = new DataTable(); 
         dt.Columns.Add("EmployeeID", typeof(int)); 
         dt.Columns.Add("EmployeeName", typeof(string)); 
         dt.Columns.Add("Destination", typeof(string)); 
         dt.Columns.Add("IsChecked", typeof(bool)); 
 
         for (int i = 0; i < 10; i++) 
         { 
             System.Data.DataRow row = dt.NewRow(); 
             var name = employeeName[r.Next(employeeName.Count() - 1)]; 
             var destinaton = title[r.Next(title.Count() - 1)]; 
             row[0] = 1000 + i; 
             row[1] = name; 
             row[2] = destinaton; 
             row[3] = i % 2 == 0 ? true : false; 
             dt.Rows.Add(row); 
         } 
         this.sfDataGrid1.AutoGenerateColumns = true; 
         this.sfDataGrid1.AutoGeneratingColumn += SfDataGrid1_AutoGeneratingColumn; 
         this.sfDataGrid1.DataSource = dt;          
          
     } 
 
     private void SfDataGrid1_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e) 
     { 
         if (e.Column is GridCheckBoxColumn) 
             (e.Column as GridCheckBoxColumn).CheckBoxSize = new Size(12, 12); 
     } 
 
 
 
 
Please let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 
 



TG The GridLock April 29, 2020 11:49 AM UTC

Hi Susmitha,
Set up right after adding datasource, it works fine for me now. 
Thanks!
f_DatagridInfo.DataSource = MyTable
TryCast(f_DatagridInfo.Columns(3), GridCheckBoxColumn).CheckBoxSize = New Size(12, 12)


SS Susmitha Sundar Syncfusion Team April 30, 2020 04:53 AM UTC

Hi GridLock, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Susmitha S 


Loader.
Up arrow icon