Display CheckBox in Gridview DataBound based on varchar column

SQL Server table structure:

ChapName        varchar(200)
Status          varchar(1)

Demand

  • I want to show a checkbox in gridview ASP.NET from Visual Studio 2010 if the value of the status column is T, let it be checked and not checked otherwise. but it only shows the text box.
  • I tried <asp:templatefield>and <asp:itemtemplate>, but it throws an error if I try to link this checkbox.
  • any sample code is required as I start in this field.

The code I tried:

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;
            }
    }

The error I received

The reference to the object is not set to the instance of the object.

Description. An unhandled exception occurred during the execution of the current network. inquiry. View the stack trace for more information on and where it originated in the code.

:
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>
+5
1

, , 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>

Row Data Bound ,

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");
    }

}
+3

All Articles