Q:

how to show opening and closing balance in an accounting winform application using datagridview

I am developing a windows form accounting application. The application generates accounting ledger of the entries. User selects a date period between which ledger has been generated. The opening balance is calculated logically after getting values from database. The problem is, how to display opening and closing balance on first and last row of datagridview respectively because the rows of datagridview are databound.
Code:
                dataGridView1.AutoGenerateColumns = false;
                dt = new DataTable();
                sda = new SqlDataAdapter("select CONVERT(varchar,date,101) as date,desc,credit,debit where user_Id='" + comboBox1.SelectedValue + "' and date BETWEEN'" + dateTimePicker1.Value.Date + "'AND'" + dateTimePicker2.Value.Date + "'", con);
                sda.Fill(dt);
                dataGridView1.ColumnCount = 3;
                dataGridView1.Columns[0].Name = "date";
                dataGridView1.Columns[0].HeaderText = "Date";
                dataGridView1.Columns[0].DataPropertyName = "date";
                dataGridView1.Columns[1].Name = "desc";
                dataGridView1.Columns[1].HeaderText = "Description";
                dataGridView1.Columns[1].DataPropertyName = "desc";
                dataGridView1.Columns[2].Name = "credit";
                dataGridView1.Columns[2].HeaderText = "Credit";
                dataGridView1.Columns[2].DataPropertyName = "credit";
                dataGridView1.Columns[3].Name = "debit";
                dataGridView1.Columns[3].HeaderText = "Debit";
                dataGridView1.Columns[3].DataPropertyName = "debit";

Now, when i calculate opening balance and add it directly using datagridview1.Rows.Add(), then it gives error saying that you can't add row programmatically because the rows are databounded.
So, please help me in my implementation. If you have some other for showing balance then please mention.

A:

user2748092,
As you found, you cannot add a row to a datbound datagridview. remove the binding, and add each row, calculating the totals as you go, then add the last row as the totals. Perhpas modify the last row color to differentiate the totals row. 
Another option, preferable in my opinion, is to present the totals near the gridview. Because it is bound, ensure the totals increase/decrease on change.
added code
This should help you find your totals. call after binding your datagrid. Rough edit, may require changes.
For Each gvRow as GridViewRow in dataGridView1.Rows
    total += double.parse(grvRow.Cells(0).Text
Next
TotalTextBox.Text = total

Using this method, you can loop your results of dt (datatable), and apply each row to the gridview. this is an exmaple to get you started. 
Dim row As String() = New String() {"1", "Product 1", "1000"}
 DataGridView1.Rows.Add(row)

Try to write the code, then ask questions about the code you have trouble with.

