在datagrid视图中添加总列值

时间:2014-03-26 15:07:07

标签: c#

我有一个datagridview和3个文本框,数据网格视图填满了这些列,法院费用和索赔权限 我不需要datagridview中的行数,总法庭费用和总索赔金额我已经对总法院进行了分类而没有请求但是总是要求索赔金额。

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 个答案:

答案 0 :(得分:1)

首先,我要研究GridView.RowDataBound方法来浏览网格视图。

其次,我会调查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');