我正在使用一个文本框和datagridview。其中我使用gridview单元格进行了计算(总金额),整个单元格的总量显示在文本框中。但它仅在编辑模式下显示。我想要总值也显示在表格加载时间。
private void datagrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
double value1 = datagrid.Rows[e.RowIndex].Cells[2].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[2].Value);
double value2 = datagrid.Rows[e.RowIndex].Cells[3].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[3].Value);
double value3 = datagrid.Rows[e.RowIndex].Cells[4].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[4].Value);
double value4 = datagrid.Rows[e.RowIndex].Cells[5].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[5].Value);
datagrid.Rows[e.RowIndex].Cells[6].Value = ((value2 + (value3/100)) * value1) - (value4/100);
if (e.ColumnIndex == 5)
{
summition();
}
}
void summition()
{
double sum = 0;
foreach (DataGridViewRow row in datagrid.Rows)
{
if (!row.IsNewRow)
sum += Convert.ToDouble(row.Cells[6].Value.ToString());
}
texttotal.Text = sum.ToString();
}
答案 0 :(得分:0)
您可以将代码移动到单独的函数(CalculateSum)
中,并在加载Form并更改DataGridCell时调用此函数。
因此您需要另外调用Form_Load事件来调用(CalculateSum)
函数。
订阅Form_Load事件如下:
this.Load += new System.EventHandler(this.Form1_Load);
完整代码
this.Load += new System.EventHandler(this.Form1_Load);
private void datagrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
CalculateSum();
}
private void Form1_Load(object sender, EventArgs e)
{
CalculateSum();
}
private void CalculateSum()
{
double value1 = datagrid.Rows[e.RowIndex].Cells[2].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[2].Value);
double value2 = datagrid.Rows[e.RowIndex].Cells[3].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[3].Value);
double value3 = datagrid.Rows[e.RowIndex].Cells[4].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[4].Value);
double value4 = datagrid.Rows[e.RowIndex].Cells[5].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[5].Value);
datagrid.Rows[e.RowIndex].Cells[6].Value = ((value2 + (value3/100)) * value1) - (value4/100);
if (e.ColumnIndex == 5)
{
summition();
}
}
答案 1 :(得分:0)
两种选择:
您可以从datagrid_CellEndEdit
事件调用函数Form_Load
。类似的东西:
this.Load += new System.EventHandler(this.Form1_Load);
MyForm_Load(object sender, EventArgs e)
{
datagrid_CellEndEdit(null,null)
}
您可以创建另一个方法,该方法将从Form_Load
事件和datagrid_CellEndEdit
事件中调用。
例如:
this.Load += new System.EventHandler(this.Form1_Load);
private void datagrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
Calculate();
}
private void Form1_Load(object sender, EventArgs e)
{
Calculate();
}
private void Calculate()
{
double value1 = datagrid.Rows[e.RowIndex].Cells[2].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[2].Value);
double value2 = datagrid.Rows[e.RowIndex].Cells[3].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[3].Value);
double value3 = datagrid.Rows[e.RowIndex].Cells[4].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[4].Value);
double value4 = datagrid.Rows[e.RowIndex].Cells[5].Value == DBNull.Value ? 0 : Convert.ToDouble(datagrid.Rows[e.RowIndex].Cells[5].Value);
datagrid.Rows[e.RowIndex].Cells[6].Value = ((value2 + (value3/100)) * value1) - (value4/100);
if (e.ColumnIndex == 5)
{
summition();
}
}