2014-04-10 45 views
0

的問題來自的上下文中使用)!= DBNull.Value)? (double)(Eval(「Premium」)):0;的GridView數據綁定方法如eval()函數,XPath的(),並綁定()只能在數據綁定控件

我需要幫助改變它。

我正在使用Gridview。

protected void dgCustomer_DataBound(object sender, System.EventArgs e) 
    { 
      Label pLabel = new Label(); 
      double dItemPremium = (Eval("Premium")!= DBNull.Value) ? (double)(Eval("Premium")) : 0; 
      pLabel.Text = dItemPremium.ToString("0.00"); 
      dTotal_m += dItemPremium; 

     dgCustomer.HeaderRow.TableSection = TableRowSection.TableHeader; 
     dgCustomer.FooterRow.TableSection = TableRowSection.TableFooter; 
     dgCustomer.FooterRow.Controls.Add(pLabel); 

    } 


    protected void dgCustomer_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

     dTotal_m += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,"Premium")); 

     } 
     else if (e.Row.RowType == DataControlRowType.Footer) 
     { 
      e.Row.Cells[0].Text = "Totals:"; 
      e.Row.Cells[1].Text = dTotal_m.ToString("0.00"); 

      e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right; 
      e.Row.Font.Bold = true; 
     } 
    } 

在HTML我用

<asp:TemplateField> 
         <ItemTemplate> 
          <%#DataBinder.Eval(DataItem,"Premium")%></ItemTemplate> 
         <HeaderTemplate> 
          Premium Amount Paid</HeaderTemplate> 
        </asp:TemplateField> 

我加的DataBinder和DataItem的,但仍然一無所獲。

謝謝!

回答

0

您可以使用控件訪問代碼中的綁定值。 Eval,Bind等方法將用於將值綁定到控件。有訪問代碼bebind約束值的方法很多,其中之一是

e.Row.Cells[0].Text 

另一個是把在標籤綁定值和訪問它像,

string tempstr=((Label)dgCustomer.Rows[e.RowIndex].Cells[<cell_number>].FindControl("Label_ID")).Text; 
+0

謝謝它的作品! – user3508633

+0

@ user3508633我很高興爲您服務。 – Nanosoft

相關問題