C# datagridview行中两个多列的结果
我得到了这段代码,它计算了C# datagridview行中两个多列的结果,c#,winforms,datagridview,C#,Winforms,Datagridview,我得到了这段代码,它计算了datagridview中所有行的两个相乘列的摘要,并在textbox中显示结果 我有第三列名为sum,我想在其中显示每行的特定结果。我很抱歉,因为我不知道该怎么做,所以我还没有尝试过任何东西。你能告诉我怎么走吗 谢谢大家的时间、评论或回答 private void vypocti_odvody(int price, int vat, int tot_rows2) { try { double outVal =
datagridview
中所有行的两个相乘列的摘要,并在textbox
中显示结果
我有第三列名为sum
,我想在其中显示每行的特定结果。我很抱歉,因为我不知道该怎么做,所以我还没有尝试过任何东西。你能告诉我怎么走吗
谢谢大家的时间、评论或回答
private void vypocti_odvody(int price, int vat, int tot_rows2)
{
try
{
double outVal = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
outVal = outVal + (Convert.ToDouble(row.Cells[price].Value)/100) * Convert.ToDouble(row.Cells[vat].Value) + (Convert.ToDouble(row.Cells[price].Value));
}
// s_ub_celk.Text = (((a / 100) * b) + a).ToString();
Convert.ToDecimal ( result.Text = outVal.ToString()) ;
}
catch (Exception ex)
{
MessageBox.Show("Chybové hlášení K3 " + ex.Message.ToString());
}
}
您只需通过指定列的值
属性为列指定值:
private void vypocti_odvody(int price, int vat, int tot_rows2)
{
try
{
double outVal = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
double localSum = (Convert.ToDouble(row.Cells[price].Value)/100) * Convert.ToDouble(row.Cells[vat].Value) + (Convert.ToDouble(row.Cells[price].Value));
outVal = outVal + localSum;
row.Cells[sumcolumn].Value = localSum;
}
Convert.ToDecimal ( result.Text = outVal.ToString()) ;
}
catch (Exception ex)
{
MessageBox.Show("Chybové hlášení K3 " + ex.Message.ToString());
}
}
只需替换sumcolumn
名称。只需修改foreach
循环如下:
double outVal = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
var sum = (Convert.ToDouble(row.Cells[price].Value)/100) * Convert.ToDouble(row.Cells[vat].Value) + (Convert.ToDouble(row.Cells[price].Value));
row.Cells["sum"].Value = sum;
outVal += sum;
}
您必须引入一个新变量,该变量将保存两个单元格的总和值,以便在新单元格中进行设置,然后将其添加到总和中。datagridview有一个名为columns的属性,您可以从中查看列“总和”的位置
但对你来说,最简单的事情是:
row.Cells["sum"].Value=10;
非常感谢你,特里皮诺。很高兴再次见到你。我会在10分钟内接受。这很简单。嗨,我编辑答案是为了在列上只显示localSum,而不是整个sum。它是数据绑定的吗?如果是,数据源是什么?