Frozen last row of DataGridView as the sum of the columns?
Is it possible to make the last row of a DataGridView
as the sum of the columns, and t开发者_JS百科hat the last row always will show/be frozen?
The solution is actually very simple, and just requires you to think outside the box a little.
Usually, once data is loaded into your grid view, there's a dark grey row at the bottom of it:
You can use this space to your advantage. All you need to do is drag a few labels onto your form, placed just inside your grid view, with a background colour applied:
Within your code, add a method like this:
void UpdateTotal(Label Label, int Number)
{
// Called from another thread; facilitate safe cross-thread operation
if(this.InvokeRequired)
this.Invoke(new Action<Label, int>(UpdateTotal), new object[] {Label, Number});
// Called from this thread or invoked
else
// Format the number with a thousands separator
Label.Text = Number.ToString("N0");
}
Then, from wherever you update your grid view, call UpdateTotal(labelPostsAffected, 2000);
, using the name of your label, and the sum you've calculated (or calculate it in the method).
The result is something like this:
Yes it is possible. Firstly you have to iterate through the grid and calculate the sum of the required columns after calculating the sums you have to create a new DataGridRow and populate it with the calculated values, set the Frozen property of the DataGridRow to True then add the new DataGridRow to the Datagridview. Sum Column Values in DataGridView
Instead of creating a new DataGridRow you could just add a new row with the summed values on your data source then find the last row on your DataGrid and set the property of that row to Frozen=True
To do this you have to set ShowFooter attribute of True
and then in code behind add your desire value in the footer
<asp:GridView ID="grdList" runat="server" ShowFooter="True" >
// other part of gridview codes e.g columns or blah blah blah
</asp:GridView>
// in code-behind
int totalValue = 2 * 10;
grdList.Columns[2].FooterText = totalValue.ToString();
精彩评论