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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Set checkbox in code

Thread ID:

Created:

Updated:

Platform:

Replies:

149791 Dec 9,2019 05:59 PM UTC Dec 12,2019 11:58 AM UTC WinForms 2
loading
Tags: SfDataGrid
Patric Gehl
Asked On December 9, 2019 05:59 PM UTC

Hello,

my Problem is simple. I use a sfDatagrid that i bind to a BindingSource with an underlaying TableAdapter (ADO.NET to SqlServer; but only for selection!).

I fill the Grid and add an additional 'Checkbox-Column' in code. 

Now i use an second select to look for the rows where i want to set the checkstate in Code.

As far as i read, i should use a Statement like this:

foreach (var record in sf_DataGrid.View.Records)
                {
                    var Akt_Row = record.Data;
                    int iID = (int) ((System.Data.DataRowView)Akt_Row ).Row.ItemArray[0];
                    if (iID == ii)
                    {
                        ((System.Data.DataRowView)Akt_Row ).Row.ItemArray[6] = 1;  --Column with the Checkbox!
                    }
                } 

The Statement is running with no error, but also with no result!

What is my fault?
Should i use an other Grid? 
Should i use an other DataSource since i only Need a simple int ID, string Name structure from the database with an additional Checkbox that i set in Code?

Thanks

Patric


Patric Gehl
Replied On December 10, 2019 11:35 AM UTC

Hello,

my Problem is simple. I use a sfDatagrid that i bind to a BindingSource with an underlaying TableAdapter (ADO.NET to SqlServer; but only for selection!).

I fill the Grid and add an additional 'Checkbox-Column' in code. 

Now i use an second select to look for the rows where i want to set the checkstate in Code.

As far as i read, i should use a Statement like this:

foreach (var record in sf_DataGrid.View.Records)
                {
                    var Akt_Row = record.Data;
                    int iID = (int) ((System.Data.DataRowView)Akt_Row ).Row.ItemArray[0];
                    if (iID == ii)
                    {
                        ((System.Data.DataRowView)Akt_Row ).Row.ItemArray[6] = 1;  --Column with the Checkbox!
                    }
                } 

The Statement is running with no error, but also with no result!

What is my fault?
Should i use an other Grid? 
Should i use an other DataSource since i only Need a simple int ID, string Name structure from the database with an additional Checkbox that i set in Code?

Thanks

Patric


Ok, i found one solution.

I add a new column to my datatable and bind this table to the datagrid as datasource.

Then instead of Looping over the 'records' of the grid i loop over the datatable and set the new value.


            userTableAdapter1.Fill(this.message_SystemDataSet1.User); // Fill the table in the dataset
            dtUser = this.message_SystemDataSet1.User;                         // Copy to my datatable
            dtUser .Columns.Add("in_Gruppe", typeof(bool));                 // Add a column of type bool   
            sf_DG_User.DataSource = dtUser ;                                        // Bind to the datagrid
            Set_Grid();


void Set_Grid()
                for (int i = 0; i < iAnz_Ben; i++) // Array of user to check
                {
                    int iAkt_ID = int.Parse(Ben_IDs[i].ToString()); // ID of the User
                    for (int j = 0; j < dtUser .Rows.Count; j++)
                    {
                        int iTest = (int)dtUser .Rows[j].ItemArray[0];
                        if (iTest == iAkt_ID)
                        {
                            var change_Row = dtUser .Rows[j];
                            change_Row.BeginEdit();
                            change_Row[8] = true;
                            change_Row.EndEdit();
                            change_Row.AcceptChanges();
                        }
                    }
                }
                dtUser .AcceptChanges();

The changes in the datatable are sent to the datagrid and all works well.

Any better solution??

Patric




Mohanram Anbukkarasu [Syncfusion]
Replied On December 12, 2019 11:58 AM UTC

Hi Patric,   
  
Thanks for the update.   
  
We are glad to know that the reported problem has been resolved at your end. You can achieve your requirement same as in the solution you have shared. Could you please explain the scenario when you want to iterate the rows and set the check state? (like in initial loading, on occurrence of any event or anything like this). Based on those details we will check for any better solution and update you if any.  
  
Regards, 
Mohanram A. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon