CellClick change the checkbox value

35 posts, 1 answers
  1. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 28 Jul 2010 Link to this post

    I have a RadGridView with GridViewCheckboxColumn.

    When  I click on any cell in the grid I catch the CellClick event of the grid and   check/uncheck the checkboxcolumn in the specific current row.

    It work fine in all the cells except for click on the checkbox cell that do change the value but  the result doesn’t appear in the checkbox.

     The changes are seen only after I move the cursor.

    How it is possible that the value is changed but the checkbox isn’t checked/ unchecked ?

      void RadGridView_CellClick(object sender, GridViewCellEventArgs e)

            {

                if (e.RowIndex > -1)

                {

                    if (e.Row.Cells["Sel"] != null)

                    {

                        if (e.Row.Cells["Sel"].Value != DBNull.Value)

                            e.Row.Cells["Sel"].Value = !Convert.ToBoolean(e.Row.Cells["Sel"].Value);

                           

                        else

                            e.Row.Cells["Sel"].Value = true;

                           

                    }

                }

               

            }

  2. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 28 Jul 2010 Link to this post

    Hello Dror,

    to make the changes visible you should call this after changing the checkbox values:

    RadGridView1.TableElement.Update(GridUINotifyAction.DataChanged)
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 29 Jul 2010 Link to this post

    Thank you for your quick replay.

     

    I tried your solution but it didn’t worked.

     

    There is no actually property as TableElement for RadGridView, instead I tried to use the GridElement as follow:

    RadGridView1.GridElement.Update(GridUINotifyAction.DataChanged)

     

    but it didn’t work.

    Did I missed anything?

  5. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 29 Jul 2010 Link to this post

    Are you using an older version of the RadControls? In Version 2010 Q2 there is the TableElement. I also have some part in my code that sets the CheckBoxes programmatically. You could use the GridUINotifyAction.Reset instead of the DataChanged, that should work in all cases, but may loose the scroll position in large grids. Telerik support told me that it will be fixed in the next Service Pack coming soon.

    RadGridView1.TableElement.Update(GridUINotifyAction.Reset)

  6. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 29 Jul 2010 Link to this post

    Hi

    i use RadControls for WinForms Q1 2010 SP2.
    This is the link to download q2 ?
    http://www.telerik.com/account/free-trials/download-trial-file.aspx?pid=523

    How do i know about th new SP ?
  7. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Jul 2010 Link to this post

    Hello Dror,

    Is your grid readonly or not? If not then you might set readonly for the checkbox column, because otherwise when you click on the checkbox in the column it will change the value once for the click and again for the cell click...

    Hope this helps,
    Emanuel Varga
  8. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 29 Jul 2010 Link to this post

    Hi

    i download the setup from http://www.telerik.com/account/free-trials/download-trial-file.aspx?pid=523
    but when i try to install it i get an error "...is not a valid win 32 applictaion"
    i use win xp.

  9. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 29 Jul 2010 Link to this post

    Use a Download-Manager. The message means that the download was not okay.
  10. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 29 Jul 2010 Link to this post

    Bernd, thank you for your suggestions and help.

    Dror, we would recommend that you try our latest release (Q2 2010). We will appreciate your feedback and will help with the transition process.

     

    All the best,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Phillip Foster
    Phillip Foster avatar
    58 posts
    Member since:
    Apr 2010

    Posted 30 Jul 2010 Link to this post

    I also have this same issue. I have upgraded to the newest version. 

    Basically the radGridView_CellClick event will fire when I click on any other cell. However when i click on a cell containing a checkbox. It will add the check to the box, but the event does not fire. 
  12. Bernd Mueller
    Bernd Mueller avatar
    38 posts
    Member since:
    May 2010

    Posted 30 Jul 2010 Link to this post

    Yes, clicking the checkbox is not firing the cell click event, but the mouseclick event of the grid.
  13. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 02 Aug 2010 Link to this post

    Hello again,

    Sorry it took this long to get back to you but i've prepared something that will work, you should handle the on MouseDown event of the grid and it looks something like this: (Sorry i don't have time to clean it up more)

    private void radGridView1_MouseDown(object sender, MouseEventArgs e)
           {
               var dataCell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
               var checkBoxCell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadCheckBoxEditorElement;
               if (dataCell != null || checkBoxCell != null)
               {
                   if (dataCell != null)
                   {
                       try
                       {
                           var value = dataCell.RowInfo.Cells["Checked"].Value != null ? !(bool)dataCell.RowInfo.Cells["Checked"].Value : true;
                           dataCell.RowInfo.Cells["Checked"].Value = value;
                       }
                       catch (Exception)
                       {
                       }
                   }
                   else
                   {
                       switch (checkBoxCell.CheckState)
                       {
                           case Telerik.WinControls.Enumerations.ToggleState.Off:
                               checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.On;
                               break;
                           case Telerik.WinControls.Enumerations.ToggleState.On:
                               checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
                               break;
                           default:
                               break;
                       }
                   }
               }
           }


    Please let me know if this works for you,
    Best regards,
    Emanuel Varga
  14. Sason
    Sason avatar
    32 posts
    Member since:
    Aug 2012

    Posted 02 Aug 2010 Link to this post

    Thank you, iwill try and let you know.
  15. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 02 Aug 2010 Link to this post

    Hello again,

    Fix: Sorry for before i missed the fact that the first time you click it's unchecking and checking again the checkbox, but it works great if you use the MouseUp event.

    Best regards,
    Emanuel Varga
  16. Phillip Foster
    Phillip Foster avatar
    58 posts
    Member since:
    Apr 2010

    Posted 02 Aug 2010 Link to this post

    This still does not work for me. If you click directly on the checkbox within the datagrid. The Event fires, but both 
     "dataCell" and "checkBoxCell" are null and therefore nothing happens.


  17. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 02 Aug 2010 Link to this post

    Hello,

    If you click directly into the checkbox this should work as default if you don't have the grid set to readonly, if you have it on readonly you should check also for the checkMark click, using the following:
    (I've updated the code for this)
    void radGridView1_MouseUp(object sender, MouseEventArgs e)
            {
                var dataCell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridDataCellElement;
                var checkBoxCell = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadCheckBoxEditorElement;
                var checkMark = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadCheckmark;
                 
                if (dataCell != null || checkBoxCell != null || checkMark != null)
                {
                    if (dataCell != null)
                    {
                        try
                        {
                            var value = dataCell.RowInfo.Cells["Checked"].Value != null ? !(bool)dataCell.RowInfo.Cells["Checked"].Value : true;
                            dataCell.RowInfo.Cells["Checked"].Value = value;
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (checkBoxCell != null)
                    {
                        switch (checkBoxCell.CheckState)
                        {
                            case Telerik.WinControls.Enumerations.ToggleState.Off:
                                checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.On;
                                break;
                            case Telerik.WinControls.Enumerations.ToggleState.On:
                                checkBoxCell.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
                                break;
                        }
                    }
                    else
                    {
                        switch (checkMark.CheckState)
                        {
                            case Telerik.WinControls.Enumerations.ToggleState.Off:
                                checkMark.CheckState = Telerik.WinControls.Enumerations.ToggleState.On;
                                break;
                            case Telerik.WinControls.Enumerations.ToggleState.On:
                                checkMark.CheckState = Telerik.WinControls.Enumerations.ToggleState.Off;
                                break;
                        }
                    }
                }
            }

    Best Regards,
    Emanuel Varga
  18. Phillip Foster
    Phillip Foster avatar
    58 posts
    Member since:
    Apr 2010

    Posted 02 Aug 2010 Link to this post

    Similar problem. Now the event fires everytime, but if you clicked on the checkbox then the actual state box doesn't change. 
    for instance, if the box is checked and you click on the box, when the event fires, the checkMark.CheckState = true. Even though it should remove the check. So the next time the box is clicked, the checkMark.CheckState still = true. The checked value never changes. If you click the area in the cell outside of the checkbox, it works as expected. But thats not exactly reasonable.  I can add code to manually remove/add the checkmark to the box, but shouldn't that already be taken care of?

    Thanks!


    also, the cell on the gridview  is set to ReadOnly = false. 
  19. Phillip Foster
    Phillip Foster avatar
    58 posts
    Member since:
    Apr 2010

    Posted 02 Aug 2010 Link to this post

    Ok, i got it figured out. I was setting all of the columns to readonly except for the first column (the check box column). like this:
    gvMassTickets.MasterTemplate.Columns[0].ReadOnly = false;
    gvMassTickets.MasterTemplate.Columns[1].ReadOnly = true;
    gvMassTickets.MasterTemplate.Columns[2].ReadOnly = true;
    gvMassTickets.MasterTemplate.Columns[3].ReadOnly = true;

    however when i deleted that code and just set the entire grid to ReadOnly = true. It works. 

    Thanks. 
  20. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 02 Aug 2010 Link to this post

    Hello Phillip,

    If readonly is set to false then my first solution should have worked (with the MouseUp event) because it's normal if the check box is enabled for it to catch the click and change it's state and after that the row event comes and changes it again or the other way around (that's why i ignored this case in the first version),
    please try that one again on a new very small project and you will see it works, sadly i cannot send you my test project on the forums but it has to work like this.

  21. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 03 Aug 2010 Link to this post

    Bernd, thank you for your clarification. Yes, when clicking in the checkbox cell the CellClick event is not fired. This is because this is a special cell which contains a permanent editor. The editor handles the event and you can override this logic by handling the MouseClick event:

    void radGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        RadElement element = this.radGridView1.ElementTree.GetElementAtPoint(e.Location);
        if (element is RadCheckBoxEditorElement)
        {
            GridCheckBoxCellElement checkboxCell = (GridCheckBoxCellElement)element.Parent;
            //...
        }
    }

    I hope this helps.
     

    Best wishes,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  22. Fawad
    Fawad avatar
    38 posts
    Member since:
    May 2009

    Posted 06 Apr 2011 Link to this post

    Okay this is an old thread but this might help someone, if their GridView is read only. Its a shorter, cleaner version of codes presented  here and below one worked for me.

    private void RgList_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
    {
          GridHeaderCellElement headerelement = sender as GridHeaderCellElement;
     
          if (headerelement == null)
           {
             bool flag = RgList.MasterTemplate.Rows[e.RowIndex].Cells["Chk1"].IsSelected;
     
            RgList.MasterTemplate.Rows[e.RowIndex].Cells["Chk1"].IsSelected = !flag;
            RgList.MasterTemplate.Rows[e.RowIndex].Cells["Chk1"].Value = !flag;              
          }
    }


    Regards.
  23. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 07 Apr 2011 Link to this post

    Hello Fawad, thank you for sharing your solution with the community. 
     
    Kind regards,
    Jack
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  24. Kannan
    Kannan avatar
    1 posts
    Member since:
    Apr 2011

    Posted 10 Aug 2011 Link to this post

    Hello Telerik Support,


    Basically the radGridView_CellClick event will fire when I click on any other cell. However when i click on a cell containing a checkbox. It will add the check to the box, but the event does not fire. 

    reg:Validation of check box control  in gridview.


    I am using GridviewCheckbox column inside the telerik grid.When I am clicking inside the check box column the cell click event is not firing,for another columns its firing.
    my concern is,
    example:->while user selecting the check box inside the grid
    If(textbox1.text=="")
    {
     messagebox.show("check box selection  is not allowed");

    and the old value of check box should be retained.Is there anyway to do this.

    thanks in advance
    Kannan
  25. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 11 Aug 2011 Link to this post

    Hello Kannan,

    Thank you for writing.

    There is not a straight-forward way to implement your requirement. However, one approach is to make your check-box column read only in order to retain its values and show a message box on mouse click.
    void radGridView1_MouseClick(object sender, MouseEventArgs e)
    {
        RadCheckmark checkmark = this.radGridView1.ElementTree.GetElementAtPoint(e.Location) as RadCheckmark;
        if (checkmark != null)
        {
            MessageBox.Show("CheckBox cannot be checked/unchecked");
        }
    }

    Hope this helps. Let me know if you have further questions.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  26. Rajendra
    Rajendra avatar
    2 posts
    Member since:
    Aug 2012

    Posted 24 Aug 2012 Link to this post

    Hello Martin

    *I am using Telerik:RadGrid  

    When the data type is string or Float the value should be in text,  if Data type is Boolean, value should be in Checkbox. When It is Enum, value should be a comboBox. Please find the attached "requirment.png" for more detail.

     Please let me know, How do I achieve this ? 
    *I am using Telerik:RadGrid 


  27. Svett
    Admin
    Svett avatar
    728 posts

    Posted 28 Aug 2012 Link to this post

    Hi Ranjendra,

    The desired scenario is supported by the latest version of RadGridView out of the box. You should ensure that the AutoGenerateColumns property is set to true.

    Regards,
    Svett
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  28. Answer
    Med Amine
    Med Amine avatar
    1 posts
    Member since:
    Oct 2012

    Posted 04 Oct 2012 Link to this post

    Solution : elle marche
    private void dataGridViewAdresse_MouseUp(object sender, MouseEventArgs e)
            {
                Telerik.WinControls.UI.GridHeaderCellElement headerelement = sender as Telerik.WinControls.UI.GridHeaderCellElement;
     
                if (headerelement == null)
                {
     
                    if (bool.Parse(dataGridViewAdresse.CurrentRow.Cells["03_AdrPpl"].Value.ToString()) == false)
                    {
                        for (int i = 0; i < dataGridViewAdresse.RowCount; i++)
                        {
     
                            if (i != dataGridViewAdresse.CurrentRow.Index)
                            {
     
                                if (bool.Parse(dataGridViewAdresse.ChildRows[i].Cells["03_AdrPpl"].Value.ToString()) == true)
                                {
     
                                    dataGridViewAdresse.ChildRows[i].Cells["03_AdrPpl"].Value = "False";
     
                                }
     
                            }
     
                        }
                    }
     
                }
            }
  29. Jeen
    Jeen avatar
    13 posts
    Member since:
    Jul 2014

    Posted 04 Aug 2014 Link to this post

    Hi, everyone.
    I use a radgridview component with the first column in it of type "GridViewCheckBoxColumn".
    I also have a label in which I have to show the number of rows checked.
    The problem is - the number of rows checked always lags behind (one or sometimes even two steps behind).
    For example: I load data into the radgrid and initially the checkbox cells are all not checked. Then I click on one of the checkbox cells, it gets checked, but the number in the label does not update at once as it should.
    I tried using  various events (MouseClick/MouseDown/MouseUp, ValueChanged/ValueChanging, Validate/Validating, CellClick).
    I also tried writing the following lines within the body of the event:
          RadGridView1.TableElement.Update(GridUINotifyAction.Reset) ;
          RadGridView1.EndEdit()";
    and also these:
          RadGridView1.TableElement.Update(GridUINotifyAction.DataChanged) ;
          RadGridView1.EndEdit() ;

    All these don't help. The number of rows checked still doesn't update immediately after the change of the state of the checkbox cell.
    Is there a way to do it right after the checkbox cell click?

    P.S.: I have not found an identical topic on the forum, so I wrote here. If this issue has already been solved elsewhere on the forum,  please excuse me and redirect me.
     
    Thank you in advance.
    Best Regards,
    Jeen.
  30. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 07 Aug 2014 Link to this post

    Hello Jeen,

    Thank you for writing.

    When you click a cell which belongs to GridViewCheckBoxColumn, the cell enters edit mode. Although, the check box state is immediately changed as it is a permanent editor the cell's value is still not updated until the editor's value is committed by pressing Enter key for example. However, you can detect when the RadCheckBoxEditor.Value is changed using its ValueChanged event. In the event handler you can iterate through all the rows and take the value in the check box cell, except for the currently updating cell. For the editing cell, we will take the value from the editor.

    I would like to note again that the editor's value is not stored in the cell's value yet as the grid is in edit mode. The user is allowed either to accept the changes by pressing Enter key, or cancel the changes by pressing Escape key. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
     
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("IsActive", typeof(bool));
     
        for (int i = 0; i < 20; i++)
        {
            dt.Rows.Add(i, "Name" + i, false);
        }
     
        this.radGridView1.DataSource = dt;
        this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
    }
     
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        RadCheckBoxEditor cbEditor = e.ActiveEditor as RadCheckBoxEditor;
     
        if (cbEditor != null)
        {
            cbEditor.ValueChanged -= cbEditor_ValueChanged;
            cbEditor.ValueChanged += cbEditor_ValueChanged;
        }
    }
     
    private void cbEditor_ValueChanged(object sender, EventArgs e)
    {
        int checkedRows = 0;
     
        foreach (GridViewRowInfo row in radGridView1.Rows)
        {
            if ((bool)row.Cells["IsActive"].Value == true && row != this.radGridView1.CurrentRow)
            {
                checkedRows++;
            }
             
        }
        RadCheckBoxEditor cbEditor = sender as RadCheckBoxEditor;
        if ((ToggleState)cbEditor.Value == ToggleState.On)
        {
            checkedRows++;
        }
        this.radLabel1.Text = "Checked Rows: " + checkedRows;
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  31. Jeen
    Jeen avatar
    13 posts
    Member since:
    Jul 2014

    Posted 18 Aug 2014 in reply to Dess Link to this post

    Hi, Desislava.
    Excuse me, please, for the delay to answer.
    Thank you very much for your help and the sample project!!  I made a slight modification to your code, and now it works great for me!!)
    However, I have one more question.
    The logics has it that everytime I click on one of the previously checked cells to uncheck it, the number in the label text should diminish by one. I have tried to modify you code to achieve this, but so far with no success.
    Would you please give me a hint on this?
    Thanks in advance.

    Best Regards,
    Jeen.
Back to Top
UI for WinForms is Visual Studio 2017 Ready