2013-05-01 60 views
5

SQL Server表結構顯示覆選框中的GridView數據綁定:基於VARCHAR列

ChapName  varchar(200) 
Status   varchar(1) 

要求

  • 我想從Visual Studio 2010 在ASP.NET GridView控件顯示覆選框如果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> 
+0

能你用aspx文件中的gridview代碼更新問題? – Damith 2013-05-01 17:14:07

回答

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

} 
+0

它不工作。相同的錯誤對象引用未設置爲對象的實例 – 2013-05-01 18:15:49

+0

好的,哪一行?你可以用aspx中的gridview代碼更新這個問題嗎? – Damith 2013-05-01 18:17:43

+1

您沒有爲狀態添加模板字段,這就是爲什麼這是失敗。添加模板字段,並添加標籤,如我的答案。給身份證號爲狀態 – Damith 2013-05-01 18:30:16