[英]Datagridview programmatically select multiple rows and sum up values in one column
Column1 Column 2
Payment 2001 $45.9
Refund 2002 $56.8
Refund 2003 $67.9
Payment 2004 $88.0
Payment 2006 $39.9
Hi. 你好。 I have a DataGridView
with two columns. 我有两列的DataGridView
。 One column is a text description and the other column is the actual values. 一栏是文字说明,另一栏是实际值。 I want to sum up just the payment values and display the decimal sum to a TextBox
by button click. 我只想总结支付值,并通过单击按钮将十进制总和显示到TextBox
。 My payment sum from above is $173.8 and this is what I want to display in my TextBox
. 我的付款总额为$ 173.8,这是我想要显示在我的TextBox
金额。 I've managed to cobble together enough code to select my initial payment value but I've been stuck since. 我设法拼凑了足够的代码来选择我的初始付款金额,但此后我一直处于困境。 :) :)
Any help will be appreciated. 任何帮助将不胜感激。 My code so far: 到目前为止,我的代码:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int index = 0; index < dataGridView1.ColumnCount - 1; index++)
{
DataGridViewCell cell = row.Cells[index];
if (cell.Value == DBNull.Value || cell.Value == null)
continue;
if (cell.Value.ToString().Contains("Payment"))
{
DataGridViewCell next = row.Cells[index + 1];
Solution 解
Declare a variable before your loops: 在循环之前声明一个变量:
decimal sum = 0m;
Then, using your same code, within your loops just parse out the amounts (with the appropriate CultureInfo
) and add them up as you loop: 然后,使用相同的代码,在循环内只需解析出金额(使用适当的CultureInfo
)并在循环时将它们加起来即可:
sum += Decimal.Parse(next.Value.ToString(), NumberStyles.Currency, myCultureInfo);
Example 例
I created the following test to show how it should parse out the same for any culture. 我创建了以下测试,以展示它应如何针对任何文化进行解析。
(I couldn't test GHS as I couldn't find the correlating
For GHS, you can create a custom culture.
CultureInfo
for it, but I still tested another multi-character one for demo purposes.)
(我无法测试GHS,因为找不到相关的
对于GHS,您可以创建自定义文化。 A fully customized example can be seen below, though for GHS you will probably only need change the CultureInfo
,但出于演示目的,我仍然测试了另一个多字符。)
custom.NumberFormat.CurrencySymbol
property. 可以在下面看到一个完全自定义的示例,尽管对于GHS,您可能只需要更改custom.NumberFormat.CurrencySymbol
属性。
decimal amount = 1234.56m;
CultureInfo us = new CultureInfo("en-US");
CultureInfo gb = new CultureInfo("en-GB");
CultureInfo by = new CultureInfo("be-BY");
// Values for...
CultureInfo custom = (CultureInfo)us.Clone(); // USD GHS
custom.NumberFormat.CurrencyDecimalDigits = 2; // 2 2
custom.NumberFormat.CurrencyDecimalSeparator = " DECIMAL "; // "." "."
custom.NumberFormat.CurrencyGroupSeparator = " GROUP "; // "," ","
custom.NumberFormat.CurrencyGroupSizes = new int[] { 2 }; // { 3 } { 3 }
custom.NumberFormat.CurrencyNegativePattern = 0; // 0 0
custom.NumberFormat.CurrencyPositivePattern = 0; // 0 0
custom.NumberFormat.CurrencySymbol = "SYMBOL "; // "$" "₵" or "GH₵" ?
NumberFormatInfo nf1 = us.NumberFormat;
NumberFormatInfo nf2 = gb.NumberFormat;
NumberFormatInfo nf3 = by.NumberFormat;
NumberFormatInfo nf4 = custom.NumberFormat;
string s1 = amount.ToString("c", nf1);
string s2 = amount.ToString("c", nf2);
string s3 = amount.ToString("c", nf3);
string s4 = amount.ToString("c", nf4);
Console.WriteLine(s1); // $1,234.56
Console.WriteLine(s2); // £1,234.56
Console.WriteLine(s3); // 1 234,56 Br
Console.WriteLine(s4); // SYMBOL 12 GROUP 34 DECIMAL 56
decimal d1 = Decimal.Parse(s1, NumberStyles.Currency, us);
decimal d2 = Decimal.Parse(s2, NumberStyles.Currency, gb);
decimal d3 = Decimal.Parse(s3, NumberStyles.Currency, by);
decimal d4 = Decimal.Parse(s4, NumberStyles.Currency, custom);
Console.WriteLine(d1); // 1234.56
Console.WriteLine(d2); // 1234.56
Console.WriteLine(d3); // 1234.56
Console.WriteLine(d4); // 1234.56
@OhBeWise That worked flawlessly. @OhBeWise完美无瑕。 Really appreciate the help. 非常感谢您的帮助。 Thank you for your patience. 感谢您的耐心等待。 Full code below 完整代码如下
private void button8_Click(object sender, EventArgs e)
{
decimal amount = 0;
CultureInfo us = new CultureInfo("en-US");
CultureInfo custom = (CultureInfo)us.Clone();
custom.NumberFormat.CurrencySymbol = "GHS ";
foreach (DataGridViewRow row in dataGridView1.Rows)
{
for (int index = 0; index < dataGridView1.ColumnCount - 1; index++)
{
DataGridViewCell cell = row.Cells[index];
if (cell.Value == DBNull.Value || cell.Value == null)
continue;
if (cell.Value.ToString().Contains("Payment"))
{
DataGridViewCell next = row.Cells[index + 1];
string s4 = next.Value.ToString();
amount+= Decimal.Parse(s4, NumberStyles.Currency, custom);
textBox22.Text = amount.ToString();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.