在数据网格视图中添加总列值

本文关键字:添加 视图 数据 数据网 网格 | 更新日期: 2023-09-27 17:53:30

我有一个datagridview和3个文本框,datagridview由这些列、法庭费用和索赔金额填满我不需要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;
        }    } 

在数据网格视图中添加总列值

首先,我会查看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');