2010-02-18 128 views
0

我正在全光照的GridView的RowDataBound事件做在GridView的列中的某些格式化。但是當我執行代碼並使用即時窗口進行調試時,我在e.Row.Cells [1] .Text中沒有找到任何東西。我從數據表填充gridview。它顯示的記錄,但我不知道爲什麼它沒有得到rowdatabound。
以下是我結合行數據綁定問題

<asp:GridView runat="server" AutoGenerateColumns="False" 
         ID="gviewTemplate" onrowdatabound="gviewTemplate_RowDataBound" DataKeyNames="F1" 
         onrowcommand="gviewTemplate_RowCommand" 
         onrowediting="gviewTemplate_RowEditing" 
         onrowcancelingedit="gviewTemplate_RowCancelingEdit" 
         onrowupdating="gviewTemplate_RowUpdating"> 
         <Columns> 
          <asp:TemplateField> 
           <EditItemTemplate> 
            <asp:Label ID="lblID" runat="server" Text='<%# Bind("F1") %>'></asp:Label> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label Runat="server" Text='<%# Bind("F1") %>' ID="lblID1"></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Uploaded Image"> 
          <EditItemTemplate> 
           <asp:LinkButton Text="Reload" runat="server" CommandArgument='<%# Bind("F1") %>' CommandName="reload" ID="lbtnReloadImage"></asp:LinkButton> 
          </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label Runat="server" Text='<%# Eval("Uploaded") %>' ID="lblUploaded"></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Template Name"> 
           <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" /> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtTemplateName" Width="50" Runat="server" Text='<%# Eval("F2") %>'></asp:TextBox> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" Runat="server" 
            ErrorMessage="You must provide a Product Name." ControlToValidate="txtTemplateName">*</asp:RequiredFieldValidator> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblTemplateName" runat="server" Text='<%# Eval("F2") %>'></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Heading"> 
           <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" /> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtHeading" Runat="server" Width="50" Text='<%# Eval("F3") %>'></asp:TextBox> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" Runat="server" 
            ErrorMessage="You must provide a Product Name." ControlToValidate="txtHeading">*</asp:RequiredFieldValidator> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblHeading" runat="server" Text='<%# Eval("F3") %>'></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 
          <asp:TemplateField HeaderText="Coupon Text"> 
           <ItemStyle VerticalAlign="Top" HorizontalAlign="Center" /> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtCouponText" Runat="server" Width="50" Text='<%# Bind("F4") %>'></asp:TextBox> 
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" Runat="server" 
            ErrorMessage="You must provide a Product Name." ControlToValidate="txtCouponText">*</asp:RequiredFieldValidator> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label Runat="server" Text='<%# Bind("F4") %>' ID="lblCouponText"></asp:Label> 
           </ItemTemplate> 
          </asp:TemplateField> 

代碼,這就是我的RowDataBound正在做

protected void gviewTemplate_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
     if (e.Row.Cells[1].Text != e.Row.Cells[2].Text) 
     { 
      e.Row.BackColor = System.Drawing.Color.Red; 
     } 
} 

如果顯示在網格中的記錄,我不能明白爲什麼不能我得到它的RowDataBound

回答

0

因爲使用網格模板字段,所以儘量調試並保持細胞,然後試圖找到它裏面的文本框,然後讀取它的值

+0

但是我怎麼cange單元格背景的顏色現在 – pankaj 2010-02-18 07:11:42

+0

你可以把一個cxontainer如表或面板模板內,然後應用在它的背面顏色 – 2010-02-18 07:15:34

+0

好,謝謝巨人,我要嘗試,現在 – pankaj 2010-02-18 07:16:50

0

試試這個:
需要,因爲行來檢查行類型的數據綁定它還包括頁眉,頁腳以及

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow & (e.Row.RowState == DataControlRowState.Normal | e.Row.RowState == DataControlRowState.Alternate)) { 
      //your code here 
     } 
    }