我已经在GridView列中输入值。
在GridView中输入一些数据后,我想让它计算特定列的总和。
我如何才能做到这一点?
发布于 2014-11-22 20:53:00
如果您需要的是在dataGridView中显示特定列的总和,那么这可能会有所帮助:
private void dataGridView2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5)
textBox9.Text = CellSum().ToString();
}
private double CellSum()
{
double sum = 0;
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
double d = 0;
Double.TryParse(dataGridView2.Rows[i].Cells[5].Value.ToString(), out d);
sum += d;
}
return sum;
}需要注意的一些事项:
发布于 2014-11-22 20:55:04
您必须对TextchangedEvent中的值进行求和,因此请尝试以下操作
protected void txtintroId_TextChanged(object sender, EventArgs e)
{
float total= 0;
foreach (GridViewRow gridViewRow in gridView.Rows)
{
lbltotal = (Label)gridViewRow.FindControl("lbltotal");
float rowValue = 0;
if (float.TryParse(lbltotal.Text.Trim(), out rowValue))
total += rowValue;
}
GridView1.FooterRow.Cells[3].Text = total.ToString();
}https://stackoverflow.com/questions/27077274
复制相似问题