Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 that the last row always will show/be frozen?

like image 248
Gold Avatar asked Mar 06 '10 07:03

Gold


Video Answer


1 Answers

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:

like image 101
Danny Beckett Avatar answered Oct 26 '22 08:10

Danny Beckett