冻结的最后一行DataGridView作为列的总和?

是否可以将DataGridView的最后一行作为列的总和,并且最后一行始终显示/被冻结?

解决方案实际上非常简单,只需要你在盒子外思考一下。

通常,一旦将数据加载到网格视图中,底部会有一个深灰色的行:

您可以利用这个空间来获得优势。 您需要做的就是将一些标签拖到您的表单上,放在网格视图中,并应用背景颜色:

在您的代码中,添加如下方法:

 void UpdateTotal(Label Label, int Number) { // Called from another thread; facilitate safe cross-thread operation if(this.InvokeRequired) this.Invoke(new Action(UpdateTotal), new object[] {Label, Number}); // Called from this thread or invoked else // Format the number with a thousands separator Label.Text = Number.ToString("N0"); } 

然后,从更新网格视图的任何地方,调用UpdateTotal(labelPostsAffected, 2000); ,使用标签的名称和您计算的总和(或在方法中计算)。

结果是这样的:

对的,这是可能的。 首先,您必须遍历网格并计算所需列的总和,然后计算必须创建新DataGridRow的总和并使用计算值填充它,将DataGridRow的Frozen属性设置为True然后将新DataGridRow添加到Datagridview。 DataGridView中的Sum Column值

您可以在数据源上添加一个带有求和值的新行,然后找到DataGrid上的最后一行,并将该行的属性设置为Frozen = True,而不是创建新的DataGridRow。

为此,您必须将ShowFooter属性设置为True

然后在代码后面添加你的欲望值在页脚


   // other part of gridview codes eg columns or blah blah blah  // in code-behind int totalValue = 2 * 10; grdList.Columns[2].FooterText = totalValue.ToString();