views:

269

answers:

2

Howdy, using vs2008 winforms.

I want to be able to use a slightly customised datagridview but cant think of a way to do it.

i have 1. a sqldataadaptor fill a dataset 2. a binding source bound to the dataset 3 a datagridview with the bindingsource set as the datasource.

I want the binding to allow sync between the dataset and datagridview, so i can edit data and then update to database with sqldataadaptor. update.

I want to show some custom columns that are calculated results. And i want to show a final bottom row that is totals of all the columns in the DVG.

My problem is once the DGV is bound i cant add a custom column or row, it wont let me. I know i could add it directly to the dataset that is the underlying datasource but then by changing the structure of the dataset i cant update to a database once edits have taken place.

or can i ???

Can someone tell me how i can add my custom columns and a final total row to a bound DGV.

Also while im here, if i click on the top of a column to sort on it, in a bound DGV, will it also re-sort the underlying dataset, so i i edit things will still stay synced ?

thanks in advance for any help

A: 

To be able to add additional unbound columns to your DataGridView you could also set its AutoGenerateColumns property to false and then add custom columns with the following method:

dataGridView->Columns->Add(...)

To calculate the contents of the unbound columns you can handle the CellValueNeeded and CellValuePushed events but you must set the DataGridView into 'virtual mode' by setting the VirtualMode property to true in order for the eventhandlers to be invoked.

Hope this helps a little....

Sascha Hoffmann
A: 

Yes, you can.

The adapter does not care about structure. It only cares about column names used in the Select/Insert/Update/Delete commands. You can add custom columns, expression columns, columns for subtotals, columns for totals, or whatever you need. Even change the order of the columns. I do advise adding a sort column, so you can keep the custom rows in the proper place when you or the user sorts.

You can do the same thing for custom rows. When you update through the adapter, you handle the RowUpdating event and set the SqlRowUpdatingEventArgs.Status to SkipCurrentRow for these custom rows. I strongly advise creating a row type column so you know which rows to skip when updating. (You can also use the sort column's value as a key to skip rows.)

There are a couple of articles on MSDN or KB that illustrate how to add total rows.

In my experience, manipulate the data table first before binding. Do what you need to do in order to support the grid and the grid's interface. It is okay to manipulate the data, add/change/remove columns and rows, then rebind when needed.

AMissico