bind radio button list to column in gridview

user356808 picture user356808 · Sep 22, 2010 · Viewed 23.6k times · Source

I have radio button list in a gridview that needs to be bound to a column. If the value in a column is 0, the first radio button is selected, if 1, the other is selected.

This is the code, some of it is partially removed because it is not necessary

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<>"
    SelectCommand="" SelectCommandType="StoredProcedure" UpdateCommand="">
    <SelectParameters></SelectParameters>
    <UpdateParameters></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvBlockDetail" runat="server" AutoGenerateColumns="False"
    DataKeyNames="curriculumyear,electiveid,blockid" DataSourceID="SqlDataSource1"
    HorizontalAlign="Left" CellPadding="1" CssClass="news" GridLines="None" 
    BorderColor="#ebe9e2" BorderStyle="Solid" BorderWidth="1" >
    <AlternatingRowStyle BackColor="#ebe9e2" />
    <HeaderStyle BackColor="#660000" ForeColor="White" Font-Size="Small" />
    <RowStyle Font-Size="9pt" Wrap="false" ForeColor="#660000" HorizontalAlign="Center" />
    <Columns>
        <asp:TemplateField HeaderText="Add/Remove">
            <HeaderStyle Width="15%" />
            <ItemStyle Wrap="false" Width="80px" />
            <ItemTemplate>
                <asp:RadioButtonList ID="rblAddRemove" runat="server" RepeatDirection="Horizontal">
                    <asp:ListItem Text="Add" Value="0"></asp:ListItem>
                    <asp:ListItem Text="Remove" Value="1"></asp:ListItem>
                </asp:RadioButtonList>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Block">
            <HeaderStyle Width="15%" />
            <ItemStyle Wrap="false" Width="50px"  />
            <ItemTemplate>
                <asp:Label ID="lblBlock" runat="server" Text='<%# Bind("Block") %>'></asp:Label>
                <asp:Label ID="lblSection" runat="server" Text='<%# Bind("Section") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="daterange" HeaderText="Dates" ReadOnly="True"  SortExpression="daterange" />
        <asp:BoundField DataField="credithours" HeaderText="Credit Hrs" 
                                                    SortExpression="credithours" HeaderStyle-Width="10%" ItemStyle-Width="10%" />
        <asp:TemplateField HeaderText="Students&lt;br&gt;Per Block" HeaderStyle-Width="15%" SortExpression="studentsperblock">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("studentsperblock") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:TextBox ID="txtStudentsPerBlock" runat="server" MaxLength="3" Width="40px" Text='<%# Bind("studentsperblock") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>    
        <asp:BoundField DataField="enrolled" HeaderText="Enrolled" ReadOnly="True"
                                                    SortExpression="enrolled" ItemStyle-Width="200px" />  
        <asp:BoundField DataField="blockid" HeaderText="blockid" ReadOnly="True" 
                                                    SortExpression="blockid" Visible="false" />
    </Columns>
</asp:GridView>

Codebehind:

Protected Sub gvBlockDetail_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBlockDetail.RowDataBound

End Sub

Answer

citronas picture citronas · Sep 23, 2010

You could try inline binding:

<asp:RadioButtonList ID="rblAddRemove" runat="server" RepeatDirection="Horizontal" SelectedValue='<%# Bind("YOURCOLUMN") %>'>
   <asp:ListItem Text="Add" Value="0"></asp:ListItem>
    <asp:ListItem Text="Remove" Value="1"></asp:ListItem>
</asp:RadioButtonList>

Where yourcolumn is the int column you described.

Or via the RowDataBound event. (Pseudocode, the properties might have a different name and I'm using C#)

Protected Sub gvBlockDetail_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvBlockDetail.RowDataBound
  if(e.Row.RowType == RowType.DataRow)
  {
   RadioButtonList rbl = e.Row.FindControl("rblAddRemove") as RadioButtonList;
   if(rbl != null)
   {
     rbl.SelectedValue = ((YOURDATAITEM)(e.Row.DataItem).YourProperty.ToString();
   }
  }
End Sub

Edit: I see you aren't using custom classes. You need to adjust the line with YOURDATAITEM. Use quick watch to get to know how to cast the object to get ahold of the desired property.