Configure related datagridview

Howdy using winforms vs2008.

I want to be able to use a slightly customized datagridview, but I can’t think how to do it.

I have 1. sqldataadaptor populate a dataset 2. a binding source bound to a data set 3 a datagridview with a binding source set as the data source.

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

I want to show some custom columns that are calculated results. And I want to show the last bottom row, which is the result of all the columns in the DVG.

My problem is when the DGV is bound, I cannot add a custom column or row, this will not allow me. I know that I can add it directly to the data set, which is the basic data source, but then, having changed the structure of the data set, I can not update the database after the changes have been made.

or can i

Can someone tell me how I can add my own columns and the final common row to the associated DGV.

Also, while im here, if I click on the top of the column to sort by it in the associated DGV, will it also re-sort the underlying dataset, so that I would still edit everything while remaining in sync?

in advance for any help

+4
source share
2 answers

Yes, you can.

The adapter does not care about the structure. This applies only to the column names used in the Select / Insert / Update / Delete commands. You can add your own columns, expression columns, columns for subtotals, columns for totals, or whatever you need. Even reorder the columns. I suggest adding a sort column so that you can save your own rows in the right place when you or the user sort.

You can do the same for custom strings. When you RowUpdating adapter, you handle the RowUpdating event and set SqlRowUpdatingEventArgs.Status to SkipCurrentRow for these custom strings. I highly recommend creating a row type column so that you know which rows to skip when updating. (You can also use the value of the sort column as a key for line skipping.)

There are several articles on MSDN or KB that illustrate how to add complete lines.

In my experience, first process the data table before binding. Do what you need to do to support the grid and grid interface. It is normal to manipulate data, add / modify / delete columns and rows, and then restore if necessary.

+1
source

To add additional disparate columns to the DataGridView , you can also set its AutoGenerateColumns property to false , and then add custom columns in the following way:

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

To calculate the contents of unrelated columns, you can handle the CellValueNeeded and CellValuePushed , but you must set the DataGridView to "virtual mode" by setting the VirtualMode property to true so that event handlers are called.

Hope this helps a bit.

0
source

Source: https://habr.com/ru/post/1301434/


All Articles