在Gridview DataBound中显示基于varchar列的CheckBox

时间:2013-05-01 17:00:11

标签: c# asp.net visual-studio-2010 checkbox varchar

SQL Server表结构:

ChapName        varchar(200)
Status          varchar(1)

要求

  • 我想在Visual Studio 2010的ASP.NET网格视图中显示复选框 如果status列的值为T,则检查并取消选中。 但它只显示文本框。
  • 我尝试过<asp:templatefield><asp:itemtemplate>,但如果我尝试绑定此复选框,则会抛出错误。
  • 我需要任何示例代码,因为我是这个领域的初学者。

我试过的代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
            CheckBox c = (CheckBox)GridView1.FindControl("ChkStatus");
            TextBox TB = (TextBox)GridView1.FindControl("Status");

        //Response.Write(TB.Text);
            if (TB.Text == "T")
            {
                c.Checked = true;
            }
            else
            {
                c.Checked = false;
            }
    }

我得到的错误

  

对象引用未设置为对象的实例。

     

描述:执行当前Web期间发生了未处理的异常   请求。请查看堆栈跟踪以获取有关的更多信息   错误以及它在代码中的起源。

     

例外细节:
  System.NullReferenceException:未将对象引用设置为实例   一个对象。

Aspx标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
              DataKeyNames="Comp,QTypeCode" DataSourceID="SDS_QType_Edit" 
              BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" 
              BorderWidth="1px" 
              CellPadding="4" ForeColor="Black" GridLines="Vertical" 
              AllowPaging="True" AllowSorting="True" 
              onselectedindexchanged="GridView1_SelectedIndexChanged" 
              onrowdatabound="GridView1_RowDataBound">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
        <asp:BoundField DataField="QTypeCode" HeaderText="QTypeCode" 
                        SortExpression="QTypeCode" InsertVisible="False" 
                        ReadOnly="True" />
        <asp:BoundField DataField="Descr" HeaderText="Descr" SortExpression="Descr" />
        <asp:CheckBoxField DataField="AnsReq" HeaderText="AnsReq" ReadOnly="True" 
                           SortExpression="AnsReq" />
        <asp:CheckBoxField DataField="OptionPrint" HeaderText="OptionPrint" 
                           ReadOnly="True" SortExpression="OptionPrint" />
        <asp:BoundField DataField="Status" HeaderText="Status" 
                        SortExpression="Status" />
        <asp:BoundField DataField="TransDate" HeaderText="TransDate" 
                        SortExpression="TransDate" />
        <asp:BoundField DataField="UserName" HeaderText="UserName" 
                        SortExpression="UserName" />
        <asp:TemplateField HeaderText="Check Box" >
            <ItemTemplate>
                <asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
     </Columns>
     <EditRowStyle Wrap="False" />
     <EmptyDataRowStyle Wrap="False" />
     <FooterStyle BackColor="#CCCC99" />
     <HeaderStyle Wrap="False" BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
     <RowStyle Wrap="False" BackColor="#F7F7DE" />
     <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" 
                       Wrap="False" />
     <SortedAscendingCellStyle BackColor="#FBFBF2" />
     <SortedAscendingHeaderStyle BackColor="#848384" />
     <SortedDescendingCellStyle BackColor="#EAEAD3" />
     <SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>

1 个答案:

答案 0 :(得分:3)

假设您在asmx上定义了如下网格

<asp:GridView ID="GridView1" runat="server" 
    onrowdatabound="GridView1_RowDataBound" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="ChapName" HeaderText="ChapName" />
        <asp:TemplateField HeaderText="Status" Visible ="false">
            <ItemTemplate>
                <asp:Label ID="Status" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Check Box" >
        <ItemTemplate>
            <asp:CheckBox ID ="ChkStatus" Text="text" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在行数据绑定事件上,您可以找到如下控件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    CheckBox c = e.Row.FindControl("ChkStatus") as CheckBox;
    Label lbl = e.Row.FindControl("Status") as Label;
    if (c!= null && lbl != null )
    {
        c.Checked = (lbl.Text == "T");
    }

}