We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to Uncheck slected checkbox in sfDatagrid

[1] How to Uncheck selected checkbox in sfDataGrid on Button Click Event. ?
[2] How to Loop through selected checkbox rows of sfDataGrid ?

7 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team April 8, 2019 11:58 AM UTC

Hi Avinash, 
 
Thanks for using Syncfusion product. 
 
Query 
Solution 
How to Uncheck selected checkbox in sfDataGrid on Button Click Event. ? 
You can able to uncheck the selected checkbox from selected items by using the below code snippet. 
 
if (this.sfDataGrid1.SelectedItems.Count > 0) 
{ 
    foreach (OrderInfo selectedItem in this.sfDataGrid1.SelectedItems) 
    { 
        if (selectedItem.IsClosed == true) 
            selectedItem.IsClosed = false; 
    } 
} 
 
If you want to unselect all the selected checkboxes in button click, you can refer the below code snippet. 
 
private void button3_Click(object sender, EventArgs e) 
{ 
    var selectedRows = this.sfDataGrid1.View.Records.Where(item => (item.Data as OrderInfo).IsClosed == true).ToList(); 
    foreach (var row in selectedRows) 
    { 
        (row.Data as OrderInfo).IsClosed = false; 
    } 
} 
 
 
How to Loop through selected checkbox rows of sfDataGrid? 
You can get the selected checkbox rows from SfDataGrid control by using the following code snippet, 
 
var selectedRows = this.sfDataGrid1.View.Records.Where(item => (item.Data as OrderInfo).IsClosed == true).ToList(); 
foreach (var row in selectedRows) 
{ 
 
} 
 
 
 
You can refer the below sample and video for your reference, 
 
Video : http://www.syncfusion.com/downloads/support/forum/143800/ze/CheckBoxColumn_Video546435496                                                                                   
 
If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further. 
 
Regards, 
Jagadeesan 



AV Avinash April 11, 2019 05:34 AM UTC

[1] How to count checked items on check changed event in SFDataGrid ?
[2] How to check Header checkbox when all check selected ?
[3] How to uncheck Header checkbox when all check not selected ?


JP Jagadeesan Pichaimuthu Syncfusion Team April 11, 2019 12:51 PM UTC

Hi Avinash, 
 
Thanks for your update. 
 
You can able to get the checked items count by using the CellCheckBoxClick event and also you can able to enable or disable the header checkbox in this event. Here the HeaderState value is get by using the reflection and changed the values. Please refer the below code snippet for your reference, 
 
this.sfDataGrid1.CellCheckBoxClick += sfDataGrid1_CellCheckBoxClick; 
 
void sfDataGrid1_CellCheckBoxClick(object sender, CellCheckBoxClickEventArgs e) 
{ 
    var selectedRows = this.sfDataGrid1.View.Records.Where(item => (item.Data as OrderInfo).IsClosed == true).ToList(); 
    var checkedItems = selectedRows.Count + (e.NewValue == CheckState.Checked ? 1 : -1); 
 
    GridCheckBoxColumn column = e.Column as GridCheckBoxColumn; 
    PropertyInfo info = column.GetType().GetProperty("HeaderState", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 
 
    if (checkedItems == this.sfDataGrid1.View.Records.Count) 
    { 
        info.SetValue(column, CheckState.Checked); 
    } 
    else 
    { 
        info.SetValue(column, CheckState.Unchecked); 
    }             
} 
 
 
 
If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.  
 
Regards, 
Jagadeesan


AV Avinash April 11, 2019 03:33 PM UTC

Given solution is working only on click event, but when is used spacebar(button) to check & uncheck it is not working.


JP Jagadeesan Pichaimuthu Syncfusion Team April 12, 2019 06:23 AM UTC

Hi Avinash, 
 
Thanks for your update. 
 
The CellCheckBoxClick event will be fired only when mouse click on the checkbox and it is not fired with key pressing. But you can able to achieve your requirement by using the SfDataGrid.View.RecordPropertyChanged event. This event will fire on both mouse and keyboard operations and please find the code snippet below. 
 
sfDataGrid1.View.RecordPropertyChanged += View_RecordPropertyChanged; 
 
void View_RecordPropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    if (e.PropertyName == "IsClosed") 
    { 
        var selectedRows = this.sfDataGrid1.View.Records.Where(item => (item.Data as OrderInfo).IsClosed == true).ToList(); 
        GridCheckBoxColumn column = this.sfDataGrid1.Columns["IsClosed"] as GridCheckBoxColumn; 
        PropertyInfo info = column.GetType().GetProperty("HeaderState", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 
 
        if (selectedRows.Count == this.sfDataGrid1.View.Records.Count) 
        { 
            info.SetValue(column, CheckState.Checked); 
        } 
        else 
        { 
            info.SetValue(column, CheckState.Unchecked); 
        } 
    } 
} 
 
 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan


TA TakamuraGo February 29, 2020 04:39 PM UTC

¿as it would be if instead of the OrderInfo class I fill the information from a DataSource to my sfDataGrid1?

Attachment: OrderInfo_27657da.rar


SS Susmitha Sundar Syncfusion Team March 2, 2020 01:03 PM UTC

Hi TakamuraGo, 
 
Thank you for using Syncfusion controls. 
 
Yes. You can replace your underlying datasource’s object type (model class) to get your required APIs. 
 
Please let us know if you need further assistance on this. 
 
Regards, 
Susmitha S 


Loader.
Live Chat Icon For mobile
Up arrow icon