SfDataGrid duplicate GridCheckBoxColumn on Refresh DataGrid

Hello,

I have a SfDataGrid and added an GridCheckBoxColumn.

private void LoadData()
        {
            dt = ConvertListToDataTable.ToDataTable(GetListLevantamentosUpdate().ToList());
            data.DataSource = dt;
            data.Columns.Add(new GridCheckBoxColumn()
            {
                MappingName = "Sel",
                HeaderText = "Sel",
                AllowThreeState = false,
                AllowText = true,
                AllowCheckBoxOnHeader = true
            });
            dt.Columns.Add("Sel", typeof(bool));
            data.Columns["Sel"].AllowEditing = true;
        }

Then, I have some operations in DataGrid, but when I Refresh it, the GridCheckBoxColumn is duplicated.
When I click in one of them, both get selected. If I refresh again, both are duplicated (now 4 Sel columns)... and so on...

How do I stop duplicating columns? Am I calling the Refresh DataGrid in the wrong way?
I'm calling LoadData() after I do my operations to Refresh DataGrid!

Thanks.

1 Reply

AA Arulraj A Syncfusion Team September 4, 2018 11:30 AM UTC

 
Thanks for contacting Syncfusion support. 
 
You have called the LoadData method after refreshing the DataGrid, which adds the GridCheckBoxColumn repeatedly to the Columns collection in the DataGrid. Here I have modified the LoadData method to avoid adding same column to the Columns Collection of the DataGrid. Please refer to the following code example and sample from the given location. 
 
Code Example: 
 
void LoadData() 
{ 
    var dt = GetDataTable(); 
    this.sfDataGrid1.DataSource = dt; 
    if (!this.sfDataGrid1.Columns.Contains(this.sfDataGrid1.Columns.FirstOrDefault(col => col.MappingName == "Sel"))) 
        this.sfDataGrid1.Columns.Add(new GridCheckBoxColumn() { MappingName = "Sel", HeaderText = "Sel", AllowThreeState = true, AllowCheckBoxOnHeader = true }); 
    dt.Columns.Add("Sel", typeof(bool)); 
    this.sfDataGrid1.Columns["Sel"].AllowEditing = true; 
} 
 
Also You can restrict adding same column to the DataGrid while refreshing the DataGrid by setting the SfDataGrid.AutoGenerateColumnsMode as SmartReset. 
 
 
Please let us know if you need any further assistance on this. 
 
Regards, 
Arulraj A 


Loader.
Up arrow icon