SQL 将单选按钮列表绑定到 gridview 中的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3773911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:39:02  来源:igfitidea点击:

bind radio button list to column in gridview

asp.netsqlvb.netdata-bindingradiobuttonlist

提问by citronas

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.

我在需要绑定到列的 gridview 中有单选按钮列表。如果列中的值为 0,则选择第一个单选按钮,如果为 1,则选择另一个。

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

回答by citronas

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 yourcolumnis the int column you described.

yourcolumn您描述的 int 列在哪里。

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

或者通过RowDataBound事件。(伪代码,属性可能有不同的名称,我使用的是 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.

编辑:我看到您没有使用自定义类。您需要使用 调整线YOURDATAITEM。使用快速观察来了解如何投射对象以获得所需的属性。