Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have one Grid with columns Id, Name, Balance.
Here are my DatagGridView's data:
Id      Name    Balance
1       Sai      10000Cr.
2       Ram      3000Dr.
3       Anu      5000Cr.
4       Swathi   4000Dr.

where Cr. and Dr. mean accordingly:
Cr. = Credit,
Dr. = Debit.

I have a TextBox outside the GridView. I want to do sum operation on the above 4 values and the result(8000Cr) should be stored in TextBox. Here I didn't use CheckBox and which DataGridView click event we will write the code?

Thanks in advance.
Posted
Updated 17-Aug-15 10:09am
v3
Comments
Maciej Los 17-Aug-15 16:19pm    
Why Cr. and Dr. is added to numeric value?
[EDIT]
Wrong approach!
You have to store data as proper numeric data! I'd suggest to change Cr. and Dr. to proper numeric data (+/-).

1 solution

It is usually best to do operations on the underlying data structure, such as a DataTable.

1. Add the following member variable to your class.
C#
private DataTable dtRecord;


2. I had to add init code somewhere
C#
private void Form1_Load(object sender, EventArgs e)
{
    dtRecord = new DataTable("Record");

    dtRecord.Columns.Add("Id", typeof(System.Int32));
    dtRecord.Columns.Add("Name", typeof(System.String));
    dtRecord.Columns.Add("Balance", typeof(System.String));

    dtRecord.Rows.Add(1, "Sai", "10000Cr");
    dtRecord.Rows.Add(2, "Ram", "3000Dr");
    dtRecord.Rows.Add(3, "Anu", "5000Cr");
    dtRecord.Rows.Add(4, "Swathi", "4000Dr");

    bindingSource1.DataSource = dtRecord.DefaultView;
    dataGridView1.DataSource = bindingSource1;

    CalculateSum();
}



3. You can use for example the event CellEndEdit
C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    bindingSource1.EndEdit();   // Applies the changes to the data table
    if (e.ColumnIndex == 2)
    {
        CalculateSum();
    }
}


4. The method CalculateSum as the very simplest implementation
C#
private void CalculateSum()
{
    double sum = 0.0;
    // Regular expression with named groups makes it easy to extract the data
    Regex balanceExpression = new Regex(@"(?<value>\d+)(?<type>cr|dr)", RegexOptions.IgnoreCase);
    foreach (DataRow dr in dtRecord.Rows)
    {
        string balance = dr["Balance"].ToString();
        Match m = balanceExpression.Match(balance);
        if (m.Success)
        {
            if (m.Groups["type"].Value.ToUpper() == "CR")
                sum += double.Parse(m.Groups["value"].Value);
            else
                sum -= double.Parse(m.Groups["value"].Value);
        }
    }

    textBox1.Text = sum.ToString("N2");
}
</type></value>


Of course there are plenty of other ways to do the same thing, for example using LINQ for calculating the sum.
 
Share this answer
 
Comments
Maciej Los 17-Aug-15 16:24pm    
A 4! You had to mention that OP need to store data in proper data type (numeric data).
George Jonsson 18-Aug-15 5:37am    
You are right. I neglected to address that detail.
If at all the OP has the possibility to change the table structure in the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900