2014-03-26 79 views
0

我有一個datagridview和3個文本框和datagrid視圖填寫了這些列,法院費用和索賠amt 我不需要在datagridview,total courtfee和總索賠金額的行我有對總法院和沒有請求進行了排序,但堅持了總索賠金額。在datagrid視圖中添加總列值

no of rows in datagridviews, = 4 which should be in this format (00004) 
total court fee = total court fee value = 60 (15 each requests) (000006000) 
total claim amount = 4000 (which should be in this format 0000004000) but i get this value (0000001000). 

這是我的代碼:

所有的
decimal requests = 0; 
     decimal CFee = 0; 
     decimal CLAIMAMT = 0; 
     int j = 0; 
     for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     { 


      string strrequests = dataGridView1.RowCount.ToString(); 
      while (strrequests.Length < 5) 
       strrequests = "0" + strrequests; 
      textBox2.Text = strrequests.ToString(); 
      //string strCFee = (dataGridView1.Rows[j].Cells["CFee"].Value).ToString(); 
      string strCFee = ((Convert.ToDecimal(dataGridView1.Rows[i].Cells["CFee"].Value)/100) *  dataGridView1.RowCount).ToString(); 
      for (j = 0; j < 5; j++) 
       strCFee = "0" + strCFee; 
      while (strCFee.Length < 9) 
       strCFee += "0"; 
      textBox3.Text = strCFee; 
      // string strCLAIMAMT = (dataGridView1.Rows[j].Cells["CLAIMAMT"].Value).ToString(); 
      string strCLAIMAMT = ((Convert.ToDecimal(dataGridView1.Rows[i].Cells["CLAIMAMT"].Value)/100)).ToString(); 
      for (j = 0; j < 5; j++) 
       strCLAIMAMT = "0" + strCLAIMAMT; 
      while (strCLAIMAMT.Length < 10) 
       strCLAIMAMT += "0"; 
      textBox4.Text = strCLAIMAMT; 
     } } 

回答

1

首先我會考慮GridView.RowDataBound方法,通過GridView的截枝。

其次,我會考慮PadLeft爲您的字符串添加零。

要回答你的問題,你的循環,你會想是這樣獲得的索賠總額,行和費用:

decimal requests = 0; 
    decimal CFee = 0; 
    decimal CLAIMAMT = 0; 
    for (int i = 0; i < dataGridView1.Rows.Count; i++) 
    {   
     CLAIMAMT += Convert.ToDecimal(dataGridView1.Rows[i].Cells["CLAIMAMT"].Value); 
     CFee += Convert.ToDecimal(dataGridView1.Rows[i].Cells["CFee"].Value); 
     requests++; 
    }  


    lblClaimAmountTotal.Text = CLAIMAMT.ToString().PadLeft(10, '0'); 
    lblCFeeTotal.Text = CFee.ToString().PadLeft(9, '0'); 
    lblRequestTotal.Text = requests.ToString().PadLeft(5, '0'); 
+0

由於喬爾......這是超級..執行完美.. 。 – smrithi