基于VARCHAR列显示复选框GridView的数据绑定 [英] Displaying CheckBox in Gridview DataBound based on varchar column

查看:88
本文介绍了基于VARCHAR列显示复选框GridView的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Server表的结构:

  ChapName VARCHAR(200)
状态VARCHAR(1)

要求


  • 我想从Visual Studio 2010在ASP.NET GridView控件来显示复选框
    如果状态列的值是T,让它进行检查和否则选中。
    但只显示文本框。

  • 我曾尝试< ASP:的TemplateField> < ASP:ItemTemplate中> ,但它会引发错误,如果我试图将此复选框绑定。

  • 任何样品code作为我在这个领域的初学者必须的。

在code我尝试:

 保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
            复选框C =(复选框)GridView1.FindControl(ChkStatus);
            文本框TB =(文本框)GridView1.FindControl(状态);        //Response.Write(TB.Text);
            如果(TB.Text ==T)
            {
                c.Checked = TRUE;
            }
            其他
            {
                c.Checked = FALSE;
            }
    }

我得到的错误


  

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


  
  

说明:执行当前Web的执行过程中发生未处理的异常
  请求。请检查堆栈跟踪有关的详细信息
  错误以及它起源于code。


  
  

异常详细信息:结果
  System.NullReferenceException:对象引用未设置为一个实例
  一个对象。


ASPX标记:

 < ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE
              的DataKeyNames =比较,QTYPE code的DataSourceID =SDS_QType_Edit
              背景色=白BORDERCOLOR =#DEDFDE边框=无
              边框宽度=1像素
              CELLPADDING =4前景色=黑网格=垂直
              AllowPaging =真AllowSorting =真
              onselectedindexchanged =GridView1_SelectedIndexChanged
              onrowdatabound =GridView1_RowDataBound>
    < AlternatingRowStyle背景色=白/>
    <柱体和GT;
        < ASP:CommandField中ShowEditButton =真ShowSelectButton =真/>
        < ASP:BoundField的数据字段=QTYPE code的HeaderText =QTYPE code
                        SORTEX pression =QTYPE codeInsertVisible =FALSE
                        只读=真/>
        < ASP:BoundField的数据字段=DESCR的HeaderText =DESCRSORTEX pression =DESCR/>
        < ASP:CheckBoxField字段的DataField =AnsReq的HeaderText =AnsReq只读=真
                           SORTEX pression =AnsReq/>
        < ASP:CheckBoxField字段的DataField =OptionPrint的HeaderText =OptionPrint
                           只读=真SORTEX pression =OptionPrint/>
        < ASP:BoundField的数据字段=状态的HeaderText =状态
                        SORTEX pression =状态/>
        < ASP:BoundField的数据字段=TransDate的HeaderText =TransDate
                        SORTEX pression =TransDate/>
        < ASP:BoundField的数据字段=用户名的HeaderText =用户名
                        SORTEX pression =用户名/>
        < ASP:的TemplateField的HeaderText =复选框>
            <&ItemTemplate中GT;
                < ASP:复选框ID =ChkStatus文本=TEXT=服务器/>
            < / ItemTemplate中>
        < / ASP:的TemplateField>
     < /专栏>
     < EditRowStyle裹=FALSE/>
     < EmptyDataRowStyle裹=FALSE/>
     < FooterStyle背景色=#CCCC99/>
     < HeaderStyle换行=FALSE背景色=#6B696BFONT-粗体=真前景色=白/>
     < PagerStyle背景色=#F7F7DE前景色=黑Horizo​​ntalAlign =右/>
     < RowStyle裹=FALSE背景色=#F7F7DE/>
     < SelectedRowStyle背景色=#CE5D5AFONT-粗体=真前景色=白
                       总结=FALSE/>
     < SortedAscendingCellStyle背景色=#FBFBF2/>
     < SortedAscendingHeaderStyle背景色=#848384/>
     < SortedDescendingCellStyle背景色=#EAEAD3/>
     < SortedDescendingHeaderStyle背景色=#575357/>
< / ASP:GridView的>


解决方案

假设你有ASMX定义见下文格

 < ASP:GridView控件ID =GridView1=服务器
    onrowdatabound =GridView1_RowDataBound的AutoGenerateColumns =FALSE>
    <柱体和GT;
        < ASP:BoundField的数据字段=ChapName的HeaderText =ChapName/>
        < ASP:的TemplateField的HeaderText =状态可见=假>
            <&ItemTemplate中GT;
                < ASP:标签ID =状态=服务器文本='<%#绑定(状态)%GT;'>< / ASP:标签>
            < / ItemTemplate中>
        < / ASP:的TemplateField>
        < ASP:的TemplateField的HeaderText =复选框>
        <&ItemTemplate中GT;
            < ASP:复选框ID =ChkStatus文本=TEXT=服务器/>
        < / ItemTemplate中>
        < / ASP:的TemplateField>
    < /专栏>
< / ASP:GridView的>

这行数据绑定事件,你可以找到控件下方

 保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{    复选框C = e.Row.FindControl(ChkStatus)的复选框;
    标签LBL = e.Row.FindControl(状态)作为标签;
    如果(C =空&安培;!&安培;!LBL = NULL)
    {
        c.Checked =(lbl.Text ==T);
    }}

SQL Server table structure:

ChapName        varchar(200)
Status          varchar(1)

Requirement:

  • I want to display checkbox in an ASP.NET gridview from Visual Studio 2010 if the value of status column is T, let it be checked and unchecked otherwise. but it shows only textbox.
  • I have tried <asp:templatefield> and <asp:itemtemplate> but it throws error if I try to bind this checkbox.
  • any sample code is required as I am beginner 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 got

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:
System.NullReferenceException: Object reference not set to an instance of an object.

Aspx markup:

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

解决方案

Assume you have grid defined as below on 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>

on Row Data Bound event you can find controls as below

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

}

这篇关于基于VARCHAR列显示复选框GridView的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆