Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have a datagridview view that looks like this:
 
ProductId | Name | Price | Quantity
 
The datagridview starts with all cells empty.
 
The user will enter data in each of the columns. The number of products the user enters changes(is unknown). The user could enter only one product or five products.
 
So, once the user has entered all the products he has to enter, i want that when he clicks a button, all products will be saved to the database.
 
The table in the SQL database looks like the datagrid:
 
ProductId | Name | Price | Quantity
 
As the number of products that the user will enter is unknown, how can I save the data to the database?
 
[Edit]Shouting removed[/Edit]
Posted 12-Feb-13 8:07am
Edited 12-Feb-13 8:08am
ProgramFOX127.9K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just treat each product as an individual item and just loop through each product that needs to be inserted and do it.
Dealing with multiple rows is pretty much exactly the same as dealing with individual rows of data. The only difference is that multiple rows are dealt with in a loop of some kind.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

On inserting new row in datagridview save that row's value in datatable and then save that datatable to viewstate["dt"].On each insert new row( i.e. insert command), Bind datagridview with datatable which saved in viewstate["dt"] .your all data come in the viewstate["dt"].You can make loop on this viewState["dt"] to add save data in database.
  Permalink  
Comments
Zain -Ul- Arifeen at 13-Feb-13 11:07am
   
dear sir,
if u will guid me on this question with code or demo.
that is better for me.
thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 325
2 Richard MacCutchan 265
3 Mathew Soji 220
4 BillWoodruff 210
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411028.1 | Last Updated 12 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100