如何在windowsforms中使用c#对Datagridview的多行特定单元进行求和操作? [英] How to do Sum operation on multiple rows specific cells of Datagridview in windowsforms using c#?
问题描述
我有一个网格,列ID,名称,余额。
这是我的DatagGridView的数据:
Id名称余额
1 Sai 10000Cr。
2 Ram 3000Dr。
3 Anu 5000Cr。
4 Swathi 4000Dr。
其中 Cr。
和博士。
相应的意思是:
Cr。
=信用,
博士。
=借记。
我在GridView外面有一个TextBox。我想对上面的4个值进行求和操作,结果( 8000Cr
)应该存储在TextBox中。在这里我没有使用CheckBox和哪个DataGridView点击事件我们会编写代码?
提前谢谢。
通常最好对底层数据结构进行操作,例如DataTable。
1.将以下成员变量添加到您的类中。
private DataTable dtRecord;
2.我必须在某处添加初始化代码
private void Form1_Load( object sender,EventArgs e)
{
dtRecord = new DataTable( Record);
dtRecord.Columns.Add( Id, typeof (System。 Int32 ));
dtRecord.Columns.Add( 名称, 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.您可以使用例如事件CellEndEdit
private void dataGridView1_CellEndEdit( object sender,DataGridViewCellEventArgs e)
{
bindingSource1.EndEdit(); // 将更改应用于数据表
if (e.ColumnIndex == 2 )
{
CalculateSum();
}
}
4.方法CalculateSum是最简单的实现
private void CalculateSum()
{
double sum = 0 。 0 ;
// 带有命名组的正则表达式可以轻松提取数据
正则表达式balanceExpression = new 正则表达式( @ (?<值> \d +)(?< type> cr | dr),RegexOptions.IgnoreCase);
foreach (DataRow dr in dtRecord.Rows)
{
string balance = dr [ Balance ]的ToString();
匹配m = balanceExpression.Match(余额);
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 > ;
当然还有很多其他方法可以做同样的事情,例如使用LINQ计算总和
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.
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.
private DataTable dtRecord;
2. I had to add init code somewhere
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
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
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.
这篇关于如何在windowsforms中使用c#对Datagridview的多行特定单元进行求和操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!