2014-02-26 73 views
1

我有一個Gridview,我想計算具有零和空值的單元格。我研究了很多,但找不到便條。在GridView中計算零和空值

GridView控件:

Size  A  B  C  D  R 
1  5.5  2.0  null 6.5  1 
2  0  3.6  3.5  3.2  2 
3  3.2  1.2  5.6  2.3  3 
N/A  0  0  0  0  0 

現在我需要驗證空和空單元格,我需要這個公式適用於那些具有零個空值的單元格。在網格中現在C1 & A2具有零和空值。

GridView的驗證:

我可以使用此代碼來驗證空和空值,我怎麼可以在這裏添加我的公式。

foreach (DataGridViewRow rw in this.dataGridView1.Rows) 
{ 
    for (int i = 0; i < rw.Cells.Count; i++) 
    { 
     if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhitespace(rw.Cells[i].Value.ToString()) 
     { 
      // here is your message box... 
     } 
    } 
} 

公式:

For C1 = sum(C2/R2*R1) 
For A2 = sum(A3/R3*R2) 

但式不應該施加N/A行。

GridView控件綁定:

我不會因爲在GridView將是隻讀模式使用任何頁眉或ItemTemplate中。

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" Width="985px" AllowSorting="True" GridLines="None"> 
    <FooterStyle HorizontalAlign="Left" VerticalAlign="Middle" /> 
    <HeaderStyle Font-Bold="True" Font-Names="Times New Roman" Font-Size="Medium" Font-Underline="True" ForeColor="Blue" /> 
    <PagerStyle HorizontalAlign="Left" VerticalAlign="Top" /> 
    <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" /> 
</asp:GridView> 

我正在使用存儲過程來顯示gridview中的列,可能我會引用頁面加載事件中的SP。也是頁面加載,或者無論如何我必須驗證這些功能。 gridview只是一個報告,並沒有像添加,編輯,更新和刪除操作。

另外我應該能夠計算出即使其他單元格有零和空值,公式應該相應地應用。

真的很感謝這方面的幫助。

+1

ü應張貼它的一些編碼的GridView是如何結合 –

回答

0

你應該先嚐試一下這些,然後在這裏發帖,如果你有任何問題。

不管怎麼說,我已經在這裏

  1. 一些假設,這是獲得綁定到GridView是一個DataTable中的數據。如果沒有,那麼你可以得到GridView的數據源來獲得數據表
  2. 數據表中的列是字符串類型。如果沒有,那麼你可以根據相同的代碼來調整代碼

在綁定到gridview之前更新你的數據表。

下面的代碼應該做的伎倆。我已經測試了一下,但做了相同的徹底測試。

protected void Page_Load(object sender, EventArgs e) 
{ 
    String CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
    SqlConnection con = new SqlConnection(CS); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "EmployeeDetails"; 
    cmd.Connection = con; 

    //Create object of SqlDataAdapter here and pass object of SqlCommand 
    SqlDataAdapter ad = new SqlDataAdapter(cmd); 

    DataTable dt = new DataTable(); 

    //Call Fill method of dataadapter 
    ad.Fill(dt); 

    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     if (dt.Rows[i]["Size"].ToString() == "N/A") 
      continue; 

     for (int j = 0; j < dt.Columns.Count; j++) 
     { 
      object colVal = dt.Rows[i][j]; 
      if (colVal == DBNull.Value || Convert.ToDecimal(colVal) == 0) 
      { 
       decimal calVal = (Convert.ToDecimal(dt.Rows[i + 1][j])/Convert.ToDecimal(dt.Rows[i + 1]["R"])) * Convert.ToDecimal(dt.Rows[i]["R"]); 
       dt.Rows[i][j] = calVal; 
      } 
     } 
    } 

    //Replace this line of code 
    //GridView1.DataSource = cmd.ExecuteReader(); 
    GridView1.DataSource = dt; 

    GridView1.DataBind(); 

} 

如果這不是你所需要的,那麼請評論。

希望這會有所幫助。

+0

薩馬 - 我怎麼能設置行N/A驗證。因爲該公式不適用於N/A並更新了我的帖子。 N/A行將保持不變,即使它有空值或零值。 – Learner

+0

對於特定行,包含「N/A」的列的名稱是什麼?還是像您已將行標記爲「N/A」,其中所有列都將包含零? – samar

+0

大小是列名稱。 – Learner

1

您可以嘗試爲因德爾:

private void dataGridView1_CellFormatting(object sender, 
    DataGridViewCellFormattingEventArgs e) 
{ 
    String value = e.Value as string; 
    if ((value != null) && value.Equals(e.CellStyle.DataSourceNullValue)) 
    { 
     e.Value = e.CellStyle.NullValue; 
     e.FormattingApplied = true; 
    } 
} 
0

如何嘗試計算在RowDataBound事件的聚合列。您可以確定單元格值是否爲0或null,然後進行必要的更改。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){ 

if (e.Row.RowType == DataControlRowType.DataRow){ 

//check the cell values here .... 

} 

} 
0

到目前爲止,

protected void Page_Load(object sender, EventArgs e) 
{ 
    String CS = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
    SqlConnection con = new SqlConnection(CS); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "EmployeeDetails"; 
    cmd.Connection = con; 
    DataTable dt = new DataTable(); 
    for (int i = 0; i < dt.Rows.Count; i++) 
    { 
     if (dt.Rows[i]["Size"].ToString() == "N/A") 
      continue; 

     for (int j = 0; j < dt.Columns.Count; j++) 
     { 
      object colVal = dt.Rows[i][j]; 
      if (colVal == DBNull.Value || Convert.ToDecimal(colVal) == 0) 
      { 
       decimal calVal = (Convert.ToDecimal(dt.Rows[i + 1][j])/Convert.ToDecimal(dt.Rows[i + 1]["R"])) * Convert.ToDecimal(dt.Rows[i]["R"]); 
       dt.Rows[i][j] = calVal; 
      } 
     } 
    } 
GridView1.DataSource = cmd.ExecuteReader(); 
GridView1.DataBind(); 

} 
+0

在代碼的開始處,您需要創建「SqlDataAdapter」對象。這個類的構造函數將以「SqlCommand」對象作爲參數。然後調用「Fill」方法並將數據表對象傳遞給它。那麼我的代碼就會來。然後你的gridview1綁定代碼就會到來。這是一個不連貫的架構。你使用的是連接架構。我的代碼不適用於連接的體系結構。我修改了我的答案,並在有變更的地方給出評論。請檢查。 – samar