开发者

Totals Row in a DataGridView

I am developing a winform application application. I wanted to show sum of columns in last row of each column. This row must always be visible.

At moment I am thinking开发者_Go百科 about adding another datagridview just beneath my datagridview with records, and would show the sum in that bottom datagridview.

If there is a better way to achieve this task?


No, need of adding another datagridview

Solution 1: Please refer to this solution

Solution 2: If the above link is not exactly what you want then,

You can try to manually add last, summary, row in which you can display information that you need. For example, you can try to do the following:

  1. Read data from database and fill System.Data.DataTable
  2. Add one column to the newly created DataTable – that column might be set to true for the last, summary, row
  3. Programmatically add one extra row that contains suitable summary data
  4. Do the data binding to DataGridView control
  5. Using appropriate event, bold or otherwise graphically distinct summary row (row that have extra column value set to true)


You can do it in the same way as you suggest, like placing a datagridview for displaying the sum. you can also handle the Horizontal Scroll with this, if there are more columns.

Another method is there in this link

Another way you can Add Rows to your datasource itself to display the sum.


Even if this question is quite old-ish I'd like to propose an extension to Niraj Doshis answer. His answer holds true for a data bound DataGridView. I recently had the problem to calculate the summary in a user-editable DataGridView, whose solution differs in the details. Anyway It's quite straight-forward too. I am writing my functions on a higher abstraction level, which outlines the workflow, but elides the implementation details.

First of all you'll have to initialize the DataGridView, see

private void InitializeDataGridView()
{
    SetColumnTypes();
    AddEmptyRow();
    AddSummaryRow();
}

I set DataGridView.AllowUsersToAddRows to false for the new row would be located beneath the summary row. Hence I am adding an empty row, which the user may fill with his data. The summary row is set to ReadOnly since we do not want our user to edit it. Whenever a CellEndEdit is raised I am updating the DataGridView with the following method

private void UpdateDataGridView()
{
    RemoveSummaryRow();
    RemoveEmptyRows();
    UpdateRowNumbers();
    AddEmptyRow();
    AddSummaryRow();
}

First I remove the summary and all empty rows (you'll have to take care. If you are trying to delete the row you just edited an exception will be thrown. I've not yet figured out how to do this, but I just made up the solution. I'll amend when I came up with the solution.) Afterwards I'm setting a running number in each of the rows. This is not really required, but a detail of my implementation. At the end I add an empty row again which the user may use to add further data and then calculate and add the summary row.

As I said before this is not yet a ready-made solution, but rather I concept which works but with some quirks and bugs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜