smrithi
smrithi

Reputation: 89

adding total columns value in datagrid view

I have a datagridview and 3 textboxes and datagrid view is filled up with these columns, court fee, and claim amt I need no of rows in datagridview,total courtfee and total claim amount I have sorted the total court and no of requests but stuck with total claim amount.

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).

This is my code:

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;
        }    } 

Upvotes: 0

Views: 123

Answers (1)

joel
joel

Reputation: 156

First of all I'd look into GridView.RowDataBound method for lopping through gridviews.

Second, I'd look into PadLeft to add zeroes to your string.

To answer your question, in your loop you'll want something like this to get the total claim amount, rows and fee:

    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');

Upvotes: 1

Related Questions