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
close icon

Set checkbox in code

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


2 Replies

PG Patric Gehl replied to Patric Gehl 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





MA Mohanram Anbukkarasu Syncfusion Team 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. 


Loader.
Live Chat Icon For mobile
Up arrow icon