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

Two-Way databinding radgrid / radiobutton

6 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
martin
Top achievements
Rank 1
martin asked on 12 Sep 2008, 04:48 PM
Hi,

I'm trying to use asp:radiobuttons in a radgridview, but I'm experiencing some problems. On each row two radiobuttons for male/female. When not in Editmode they have to be disabled, when in editmode they have to be enabled.
I use the code-behind function OnItemDataBound to "translate" to databasevalue M or F to check the right radiobutton. So far so good.
in my .aspx file:

<telerik

:GridTemplateColumn HeaderText="Male" UniqueName="Gender"> <ItemTemplate>
 <asp:RadioButton ID="RadioButtonMaleItem" runat="server" GroupName="" Enabled="false"/>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="RadioButtonMaleEditItem" runat="server" GroupName="MF" />
</
EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Female" UniqueName="Female">
<
ItemTemplate>
<asp:RadioButton ID="RadioButtonFemaleItem" runat="server" GroupName="MF" Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton ID="RadioButtonFemaleItem" runat="server" GroupName="MF" />
</EditItemTemplate>
</telerik:GridTemplateColumn>

When I switch to EditMode the radiobuttons on the editrow are both unchecked how come? And how can I read the new checked value and update the database (similar to OnItemDataBound, but then the other way around)

regards martin

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Sep 2008, 06:54 AM
Hello Martin,

Try binding the RadioButtons using their Checked properties as shown in the code below.
aspx:
 <telerik:GridTemplateColumn UniqueName="Male" HeaderText="Male"
           <ItemTemplate> 
               <asp:RadioButton ID="RadioButton1" Enabled="false" runat="server" /> 
           </ItemTemplate> 
           <EditItemTemplate> 
               <asp:RadioButton ID="RadioButton2" GroupName="MF" Checked='<%#Bind("Male")%>'  runat="server" /> 
           </EditItemTemplate> 
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn UniqueName="Female" HeaderText="Female"
            <ItemTemplate> 
                <asp:RadioButton ID="RadioButton3" Enabled="false" runat="server" /> 
            </ItemTemplate> 
            <EditItemTemplate> 
                <asp:RadioButton ID="RadioButton4" GroupName="MF" Checked='<%#Bind("Female")%>' runat="server" /> 
            </EditItemTemplate> 
            </telerik:GridTemplateColumn> 

You can get the new values in the UpdateCommand/ItemCommand of the grid as shown below.
cs:
 protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.Item is GridEditableItem) 
        { 
            GridEditableItem dataItem = (GridEditableItem)e.Item; 
            RadioButton rdbtn1 = (RadioButton)dataItem["Male"].FindControl("RadioButton2"); 
            RadioButton rdbtn2 = (RadioButton)dataItem["Female"].FindControl("RadioButton4"); 
            string strtxt1 = rdbtn1.Checked.ToString(); 
            string strtxt2 = rdbtn2.Checked.ToString(); 
        } 
    } 

Princy.
0
martin
Top achievements
Rank 1
answered on 15 Sep 2008, 11:35 AM

Hi Princy,

Thx for the answer, but it is not the solution for binding the date to the buttons in the EditTemplateColumn. I cannot Bind("Male"): 

DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Male'.

Martin

0
Yavor
Telerik team
answered on 15 Sep 2008, 12:04 PM
Hello martin,

Attached to this message, is a small application, which handles a functionality close to the one that you mentioned.
I hope it helps.

Greetings,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
martin
Top achievements
Rank 1
answered on 15 Sep 2008, 12:26 PM
Hi Yavor,

In your database called test you have two colums: one for male and one for female.
That's to easy :-) I have ONE column in my table named Gender and it contains a F for Female and a M for Male.
I issued a support ticket to telerik 161905 with a demo example.

regards martin
0
Yavor
Telerik team
answered on 15 Sep 2008, 01:22 PM
Hi martin,

We will review the ticket, and address it shortly. We can continue our communication there, in order to avoid duplicate posts.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
martin
Top achievements
Rank 1
answered on 15 Sep 2008, 08:04 PM
For the interested reader. The solution that worked for me:

The GridTemplateColumns with the radiobuttons:

<telerik:GridTemplateColumn HeaderText="Male" UniqueName="Male" > 
   <ItemTemplate> 
      <asp:RadioButton ID="RadioButtonMaleItem" runat="server" Enabled="false"/> 
   </
ItemTemplate> 
   <EditItemTemplate>
      <
asp:RadioButton ID="RadioButtonMaleEditItem" runat="server" GroupName="MF" Checked='<%# RadGrid1.MasterTableView.IsItemInserted ? false : (string) Eval("Gender") == "M" %>'/>
   </EditItemTemplate>
</telerik:GridTemplateColumn>

<
telerik:GridTemplateColumn HeaderText="Female" UniqueName="Female" >
   <ItemTemplate>
      <asp:RadioButton ID="RadioButtonFemakeItem" runat="server" Enabled="false" />
   </ItemTemplate>
   <EditItemTemplate>
      <asp:RadioButton ID="RadioButtonFemaleEditItem" runat="server" GroupName="MF" Checked='<%# RadGrid1.MasterTableView.IsItemInserted ? true :(string) Eval("Gender")== "F" %>'/>
   </EditItemTemplate>
</telerik:GridTemplateColumn>

To check the right radiobutton when the data is selected from the database, I use OnItemDatBound:

protected

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
     if (e.Item is GridDataItem)
     {
        GridDataItem item = e.Item as GridDataItem;
        if (item["Gender"].Text == "M")
            (item.FindControl(
"RadioButtonMaleItem") as RadioButton).Checked = true;
        else
            if (item["Gender"].Text == "F")
               (item.FindControl(
"RadioButtonFemaleItem") as RadioButton).Checked = true;
     }
}


case solved (for me)

regards martin



Tags
Grid
Asked by
martin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
martin
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or