Radiobutton in RadGridView

13 posts, 2 answers
  1. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 11 Apr 2011 Link to this post

    Hi,

    I am using the code from the link http://www.telerik.com/support/kb/winforms/gridview/creating-a-radradiobuttoncellelement.aspx to create a RadRadioButtonElement. Unlike the example provided, I am using just one radiobutton per cell instead of three. 

    At the row level the RadRadioButtonElement should function like a checkbox. User should be allowed to set the ToggleState to On or Off on a particular row.

    Among the rows in the RadGridView, there cannot be more than one row which has this column's ToggleState set to On.

    Please check the attached image to get an idea.
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 14 Apr 2011 Link to this post

    Hello Jack, I modified the KB article to make it suitable for your case. Please check the attached file and tell whether it helps.

    Should you have any further questions, we will be glad to help.

    Greetings, Svett
    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
  3. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 14 Apr 2011 Link to this post

    Hi Svett,

    Thanks for the modification and it does work. But the other functionality which I need is to be able to set the ToggleState to Off when a user clicks on a cell which is already set to "On" similar to a checkbox functionality.

    Following is the code change I made in the mousedown event. I am trying to set the Value to zero for the current cell if it's already checked but it's not working. It's checking it back again.

    private void radioButtonElement1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.Value = 1;
     
                foreach (GridViewRowInfo row in this.ViewTemplate.Rows)
                {
                    if (row != this.RowInfo)
                    {
                        row.Cells[this.ColumnInfo.Name].Value = 0;
                    }
                    else
                    {
                        //If current cell is already checked, uncheck it.
                        if (Convert.ToInt32(this.RowInfo.Cells[this.ColumnInfo.Name].Value) == 1)
                        {
                            row.Cells[this.ColumnInfo.Name].Value = 0;
                        }
                    }
                }
            }
  4. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 18 Apr 2011 Link to this post

    Hello Jack,

    I am enclosing a modified version that implements the desired scenario.

    Greetings,
    Svett
    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
  5. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 19 Apr 2011 Link to this post

    Hi Svett,

    It just worked perfectly.

    Thanks again for your help.
  6. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 20 Apr 2011 Link to this post

    Hi Svett,

    I came across a problem wherein I am unable to remove a selected row from the RadGridView. This error can be reproduced only if the value in the RadioButtonColumn (of the selected row) is toggled.

    rgvDocuments.Rows.Remove(rgvDocuments.SelectedRows[0]);

    ArgumentException:
    Message: Cannot remove objects not in the list.
    Stack Trace:
       at System.Data.DataView.System.Collections.IList.Remove(Object value)
       at Telerik.WinControls.Data.RadListSource`1.RemoveAt(Int32 index)
       at Telerik.WinControls.UI.GridViewRowCollection.RemoveAt(Int32 index, Boolean selectCurrentRow)
       at Telerik.WinControls.UI.GridViewRowCollection.Remove(GridViewRowInfo item, Boolean selectCurrentRow)
       at Telerik.WinControls.UI.GridViewRowCollection.Remove(GridViewRowInfo item)
       at DocMinder5.frmNewEdit3.DeleteSelectedDocument() in C:\Projects\DocMinder 5 Development\WordTech DocMinder 5\DocMinder 5\frmNewEdit3.cs:line 2102

    Please advise.
  7. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 22 Apr 2011 Link to this post

    Hi Jack,

    This issue appears, because the row was modified and the editing operation is still not finished. You can finish the operation by calling the EndCurrentEdit method of the currency manager. I modified your code the following way:
    private void button1_Click(object sender, EventArgs e)
    {
        if (this.radGridView1.SelectedRows.Count > 0)
        {               
            ICurrencyManagerProvider currencyManagerProvider = this.radGridView1.MasterTemplate.ListSource as ICurrencyManagerProvider;
            currencyManagerProvider.CurrencyManager.EndCurrentEdit();
            this.radGridView1.Rows.Remove(this.radGridView1.SelectedRows[0]);
        }
    }

    I hope this helps.
     
    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
  8. Jack
    Jack avatar
    25 posts
    Member since:
    Feb 2011

    Posted 25 Apr 2011 Link to this post

    Thanks for your help Jack. It did work.
  9. Martin Lundgard
    Martin Lundgard avatar
    8 posts
    Member since:
    Jun 2009

    Posted 30 Nov 2011 Link to this post

    This is exactly what I'm looking for and working smooth. But, is there any way to get the radiobutton to honour the textalign property on the column? Maybe extending the ArrangeOverride code a little?
  10. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 05 Dec 2011 Link to this post

    Hi Arve Asheim,

    Yes, your assumption is correct. In this case you should change the ArrangeOverride method the following way:
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        if (this.Children.Count == 1)
        {
            RectangleF clientRect = GetClientRectangle(finalSize);
            this.Children[0].Arrange(new RectangleF(
                0,
                (finalSize.Height / 2) - (this.Children[0].DesiredSize.Height / 2),
                clientRect.Width,
                this.Children[0].DesiredSize.Height));
        }
     
        return finalSize;
    }

    Then you can change the TextAlignment property of GridRadioButtonElement when handling the CreateChildElements method:
    radioButtonElement1.TextAlignment = ContentAlignment.MiddleRight;

    I hope this helps. Should you have any further questions, do not hesitate to write back.
     
    Greetings,
    Jack
    the Telerik team

    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.

  11. Mark
    Mark avatar
    2 posts
    Member since:
    Oct 2013

    Posted 19 Jun 2014 in reply to Svett Link to this post

    Hi,
     I have implemented the radio button column as specified in the RadRadioButtonCellElementCS-Modified.zip project. It works fine until I add child templates in unbound mode, when I select a different row and then select the radio button on that row, the previous radio button is not deselected. See attached screenshot. How can I resolve this.

    Thanks,

    Simon Davies.
  12. Mark
    Mark avatar
    2 posts
    Member since:
    Oct 2013

    Posted 19 Jun 2014 in reply to Mark Link to this post

    I have found a  solution to this.  If I add  BeginEdit() and EndEdit() for the cells in which the check is changing, the grid gets updated correctly e.g.

     private void radioButtonElement1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                this.Value = 1;

                foreach (GridViewRowInfo row in this.ViewTemplate.Rows)
                {
                    if (row != this.RowInfo)
                    {
                        row.Cells[this.ColumnInfo.Name].BeginEdit();
                        row.Cells[this.ColumnInfo.Name].Value = 0;
                        row.Cells[this.ColumnInfo.Name].EndEdit();

                    }
                }
            }
  13. Dimitar
    Admin
    Dimitar avatar
    2654 posts

    Posted 24 Jun 2014 Link to this post

    Hello Jack,

    Thank you for writing.

    It would be better if you enclose the iteration within Begin/End update block. This will improve the performance in case you have many rows and will update the values:
    private void radioButtonElement1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        this.Value = 1;
        this.ViewTemplate.BeginUpdate();
        foreach (GridViewRowInfo row in this.ViewTemplate.Rows)
        {
            if (row != this.RowInfo)
            {
                 
                row.Cells[this.ColumnInfo.Name].Value = 0;
             
            }
        }
        this.ViewTemplate.EndUpdate();
    }

    If you have any questions, please do not hesitate to contact us.

    Regards,
    Dimitar
    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.
     
Back to Top