Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add text box values to grid view by clicking on a add button and after that if we click a save button then the values from grid view must store in sql server database

step1:text box values must go into grid view after clicking a add button
step2: there is another button save if we click on this the values from grid view should store in database
Posted
Updated 11-Mar-12 0:24am
v4

1 solution

Hello,
To Do this
1st:create datatable
2nd:add row to datatable
3rd: bind to gridview and store that talbe in viewsate and repeat from step 1(by converting viewstate value back to datatable)


1st step:
create data table following way...
DataTable table = new DataTable();
add columns how many you want and give the names which you are binding to the gridvieww

table.Columns.Add("Ename", typeof(int));
table.Columns.Add("Sname", typeof(string));
table.Columns.Add("Department", typeof(string));
table.Columns.Add("DateOfJoin", typeof(string));

2nd step:
onclick of the button take the values entered in the textbox and add to the columns of the datatable.

table.Rows.Add(txtname.text.trim(),txtsname.text.trim() ,txtDptName.text.trim(),txtdate.text.trim() );

at the end bind this table to the gridview
grdEmpdetails.datasource=table;

and store this table in the viewstate
viewstate["dtTable"]=talbe;
again
3rd:onlcick of the button you have to convert viewstate value back to datatable and add the new row and again bind that to gridview..

Its Done
 
Share this answer
 
Comments
fresherincode 22-Mar-12 8:18am    
Thank u very much

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900