add operation on multiple rows specific cells of DataGridVie
Hi,
I have one Grid with columns Id,Name,Balance.Here is my DatagGridView.Where Cr. and Dr. are Credit and Debit.
Id Name Balance
1 Sai 10000Cr.
2 Ram 3000Dr.
3 Anu 5000Cr.
4 Swathi 4000Dr.
And I have a TextBox outside of 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.
Answers (2)
0
Hello @sureshma you can write the code for DataBindingComplete event. Try this:
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
int Amount = 0;
foreach (DataGridViewRow gr in dataGridView1.Rows)
{
string str = gr.Cells[2].Value.ToString();
if (str.Contains("Dr."))
Amount -= int.Parse(str.Substring(0, str.Length - 3));
else
Amount += int.Parse(str.Substring(0, str.Length - 3));
}
if (Amount > 0)
textBox1.Text = Amount.ToString() + " Cr";
else
{
Amount = Amount * -1;
textBox1.Text = Amount.ToString() + "Dr";
}
}
Accepted 0
Thank You Mr.Rahul,it works perfectly...