This is a migrated thread and some comments may be shown as answers.

[Solved] dynamic radio button in gridview

4 Answers 240 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
vidya
Top achievements
Rank 1
vidya asked on 21 Jun 2010, 04:19 AM
How do I add a radio button in a dynamically created grid view? I would also like to access the events of the radio button.

Thanks,
Vidya

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Jun 2010, 06:27 AM
Hello,

 You can create custom column similar to this blog post.

Sincerely yours,
Vlad
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
0
Kakone
Top achievements
Rank 1
answered on 16 Sep 2010, 03:08 PM
Hello,

I try to do a GridViewRadioButtonColumn like the blog post. So I did this :

public class GridViewRadioButtonColumn : GridViewDataColumn
{
   public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
   {
      RadioButton radioButton = new RadioButton();
      radioButton.Margin = new Thickness(3.0, 0.0, 3.0, 0.0);
      if (DataMemberBinding != null)
      {
         radioButton.SetBinding(RadioButton.IsCheckedProperty, DataMemberBinding);
      }
      return radioButton;
   }
  
   public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
   {
      RadioButton radioButton = cell.Content as RadioButton;
      if (radioButton == null)
      {
         radioButton = new RadioButton();
         radioButton.Margin = new Thickness(3.0, 0.0, 3.0, 0.0);
         radioButton.IsEnabled = false;
      }
      if (DataMemberBinding != null)
      {
         radioButton.IsChecked = (bool?)GetCellContent(dataItem);
      }
      return radioButton;
   }
}

Visually, it seems correct but the behavior is wrong. When I check the radiobutton on a row, I must set the DataItem property to false for the last checked row. I don't know how I can do this in this GridViewRadioButtonColumn. Any idea ?

Cordially,
Kakone
0
Maya
Telerik team
answered on 21 Sep 2010, 03:01 PM
Hello Kakone,

You need to go one step further in your application and handle the Checked event of the RadioButton in the CreateCellEditTemplate method. What you need to do is to ensure that the property of all other items except the selected on is set to "False".
I am sending you a sample project implementing the functionality you need. 

 

Best wishes,
Maya
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
0
Kakone
Top achievements
Rank 1
answered on 21 Sep 2010, 04:41 PM
It's perfect.

Thanks a lot!
Tags
GridView
Asked by
vidya
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Kakone
Top achievements
Rank 1
Maya
Telerik team
Share this question
or