DataGridView and dataset best practice frustration
Im using c# 2008 winforms.
Ive found it very frustrating trying to find the best practice way to use datasets and datagridviews in winforms apps in general to achieve the results i want.
Basically i want to (and this is the generic methodology i want to use in all my applications) 1. populate a dataset from a sql table 2. populate a datagridview from the dataset 3. some colu开发者_StackOverflowmns in the datagridview will be calculated results from the dataset fields 4. edit data in the datagridview and have the edits synced back to the dataset source. 5. edits in the datagridview are only updated to the database source via stored procedures on the database, i never use the sqlAdaptor.update method 6.I want a bottom footer of summed column totals in the datagridview 7.I want a column to show all row summed totals
I can do all these individual things but am very frustrated to find my code ends up all over the place and different on wether i want all the above or just some of them.
For example i could sync information between the datagridview and the dataset, by binding the dataset to the datagridview but then i cant have custom columns and custom rows in the datagridview etc etc.
Its all very frustrating and i was hoping to get pointed to some best practice methods for achieving optimum results with the above. Any advice appreciated.
Thanks in advance
"I can do all these individual things but am very frustrated to find my code ends up all over the place and different on wether i want all the above or just some of them."
What I can get is, you need to organise your aaplication into layers or Tiers. You can place common functionality into a layer and assigh responsibility. Following is a Pattrn which Might give you an idea
Presentation
WebForms, Html Pages etc
Business Logic Layer (BLL)
Business Objects, Business Rules Objects
Data Access Layer (DAL)
Repository Objects (Responsible for Access your Datasets Stuff), Model (All you Data-sets / Table-Adapters etc.)
"i cant have custom columns and custom rows in the datagridview etc etc."
If One or more of your datagridviews need some data from TableAdapter (Model(DAL)), but Customised columns or rows, your BLL layer should server the purpose here. It should somewhat be as follows:
AccountGridViewClient (Presentation) --> AccountsManager(BLL)
--> AccountsRepository(DAL) -->AccountsTableAapter -- >DB
AccountGridViewAdmin (Presentation) --> AccountsManager(BLL)
--> AccountsRepository(DAL) -->AccountsTableAapter -- >DB
Have look at this thread as well.
Hope it helps
To add on Asad's it would be much more easy for you if you had a Business logic where you will do the computation that you want i.e Get the Data from the Database -> populate a collection that you later bind to the datagridview.
You can do Header edits by doing something like
` dgv.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; dgv.Columns["Name"].HeaderText = "Name";
dgv.Columns["Quantity"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
dgv.Columns["Quantity"].HeaderText = "Quantity";
dgv.Columns["TotalPrice"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
dgv.Columns["TotalPrice"].HeaderText = "Total Price";
dgv.Columns["TotalPrice"].DefaultCellStyle.Format = "0.00";`
6.I want a bottom footer of summed column totals in the datagridview
As for footer checkout this link: Add a footer to a datagridview
精彩评论