如何在datagridview页脚中显示总计 [英] How to show grand total in datagridview footer
问题描述
大家好,
我想在datagridview页脚中显示总计,但是在这里我不使用任何数据库连接,我想在不使用任何数据库连接的情况下显示grandtotal,我编写了代码,但是不起作用
我尝试过的事情:
注意:我不使用任何数据库连接,我想显示没有任何数据库连接的总数
共2格朗姆:
私有 无效 dataGridView1_CellEndEdit_1(对象发件人,DataGridViewCellEventArgs e)
{
double cell1 = 0 ;
double cell2 = 0 ;
如果(e.ColumnIndex == 0 || e.ColumnIndex == 如果(!object.ReferenceEquals(dataGridView1.CurrentRow.Cells [ 0 ].Value,DBNull.Value) )
{
cell1 = Convert.ToSingle(dataGridView1.CurrentRow.Cells [ 0 ].Value);
}
如果(!object.ReferenceEquals(dataGridView1.CurrentRow.Cells [ 2 ].Value,DBNull.Value) )
{
cell2 = Convert.ToSingle(dataGridView1.CurrentRow.Cells [ 2 ].Value);
}
如果(cell1.ToString()!= " && cell2.ToString()!= " )
{
dataGridView1.CurrentRow.Cells [ 6 ].Value = cell1 * cell2;
}
}
}
GrandTotal代码:(此代码不起作用)
私有 无效 dataGridView1_DataSourceChanged_1(对象发件人,EventArgs e)
{
double 总计= 0 ;
for ( int i = 0 ; i < dataGridView1.Rows.Count- 1 ; i ++)
{
总计+ = Convert.ToDouble(dataGridView1.Rows [i] .Cells [" ].Value );
}
dataGridView1.Rows [dataGridView1.Rows.Count- 1 ].Cells [" ].值=总计;
lblsum.Text = Convert.ToString(总计);
}
您需要更改支持数据源中的值,而不是网格本身中的值.
马克
很抱歉,我花了很长时间才回复您对我评论的回复.
因此,我们已经确定这是一个空的dataGridView,用户将通过输入值来填充它.这就是为什么从不触发dataGridView1_DataSourceChanged_1事件的原因.仅当您执行了诸如dataGridView1.DataSource = someDataTable;之类的操作时,才会触发该事件;您不会在完全由用户填充的网格上执行此操作. >
CellEndEdit是要挂接到的正确事件,以获取总计.
这有效:
私有 无效 dataGridView1_CellEndEdit(对象发件人,DataGridViewCellEventArgs e) { double cell1 = 0 ; // 第0列的总计 double cell2 = 0 ; // 第2列的总计 // 只对第0列和第2列的更改感兴趣,因此忽略其他列 如果(e.ColumnIndex!= 0 && e.ColumnIndex!= 返回; // 计算总计...,如果仅需要此行的总计 // 然后注释掉foreach ... foreach (DataGridViewRow r in dataGridView1.Rows中) { double c1 = 0 ; double c2 = 0 ; 如果(r.Cells [ 0 ].Value!= 空) 如果( double .TryParse(r.Cells [ 0 ].Value.ToString(),输出 c1)) cell1 + = c1; 如果(r.Cells [ 2 ].Value!= 空) 如果(双 .TryParse(r.Cells [ 2 ].Value.ToString(),输出 c2)) cell2 + = c2; } // 将总计插入到此行 dataGridView1.Rows [e.RowIndex] .Cells [ 6 ].Value = cell1 + cell2; }我假设您只是将第0列和第2列中的值相加,并且当前行获取当前的总计,因此不会重新访问以前的行来更新总计.
您最初发布的代码对我来说很好用,除了它是根据每行
Hi all,
I want to show grand total in datagridview footer but here im not using any database connection i want to show grandtotal without using any database connection for this i wrote code but its not working
What I have tried:
Note : im not using any database connection i want to show this total without any database connection
Two Coloumn total :
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
double cell1 = 0;
double cell2 = 0;
if (e.ColumnIndex == 0 || e.ColumnIndex == 2)
{
if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[0].Value, DBNull.Value))
{
cell1 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[0].Value);
}
if (!object.ReferenceEquals(dataGridView1.CurrentRow.Cells[2].Value, DBNull.Value))
{
cell2 = Convert.ToSingle(dataGridView1.CurrentRow.Cells[2].Value);
}
if (cell1.ToString() != "" && cell2.ToString() != "")
{
dataGridView1.CurrentRow.Cells[6].Value = cell1 * cell2;
}
}
}
GrandTotal code :(this code is not working)
private void dataGridView1_DataSourceChanged_1(object sender, EventArgs e)
{
double Total = 0;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
Total += Convert.ToDouble(dataGridView1.Rows[i].Cells["Rupees"].Value);
}
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Rupees"].Value = Total;
lblsum.Text = Convert.ToString(Total);
}
You need to change the value in the backing data source, not in the grid itself.
Marc
Sorry it took me so long to respond to your reply to my comment.
So we''ve established that this is an empty dataGridView that the user will populate by typing in values. This is why the dataGridView1_DataSourceChanged_1 event is never fired. That event would only get fired if you did something likedataGridView1.DataSource = someDataTable;which you are not going to do on a grid that is populated entirely by the User.
The CellEndEdit is the correct event to hook on to, to get your grand total.
This works:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { double cell1 = 0; //Grand total of column 0 double cell2 = 0; //Grand total of column 2 // Only interested in changes in columns 0 and 2 so ignore other columns if (e.ColumnIndex != 0 && e.ColumnIndex != 2) return; //Calculate grand total ... if only the total for this row is required // then comment out the foreach... foreach (DataGridViewRow r in dataGridView1.Rows) { double c1 = 0; double c2 = 0; if (r.Cells[0].Value != null) if (double.TryParse(r.Cells[0].Value.ToString(), out c1)) cell1 += c1; if (r.Cells[2].Value != null) if (double.TryParse(r.Cells[2].Value.ToString(), out c2)) cell2 += c2; } // Insert the grand total into this row dataGridView1.Rows[e.RowIndex].Cells[6].Value = cell1 + cell2; }I''ve assumed you are only adding up values in columns 0 and 2 and the current row gets the current grand total, previous rows are not revisited to update the grand total.
The code you originally posted is working fine for me, except it is calculating the product (cell1 multiplied by cell2) per row
这篇关于如何在datagridview页脚中显示总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!