Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to save send data into database in c# from gridview

[Edit - additional details from repost]
How to save send data into database in c# from gridview
means i want to save all of data from gridview to database with one time button click..
as like we save from textboxes

SQL
insert into table values(datagridview1.column1 + "," + datagridview1.column2 + "," + datagridview1.column2 +");
Posted
Updated 23-Jul-17 18:44pm
v2
Comments
[no name] 27-Jul-13 11:04am    
You connect to whatever database you are using, write a query to insert or update the data you want to save, execute that query and then disconnect from whatever database you are using.
Mahesh Bailwal 27-Jul-13 11:47am    
Well you can loop through all values in datagridview and insert them in database one by one inside loop.
Is this what you are looking for?
pradiprenushe 27-Jul-13 12:39pm    
how you are populating data in datagrid? why you want to insert data which is already present? As said by @Mahesh Bailwal it can be done by looping datagrid but really you required to insert all data. Or only update changed data?

1 solution

Do something like this..
C#
for(int i=0; i< dataGridView1.Rows.Count-1;i++)
 {
    Strquery= @"Insert into yourTableName(date,column1,....) values (" 
                + System.DateTime.Now +", "
                + dataGridView1.Rows[i].Cells[1].Value +".......);";
    cmd.CommandText = Strquery;
    cmd.ExecuteNonQuery();
 }

Or,follow this post..
http://social.msdn.microsoft.com/Forums/en-US/8cee37d0-732b-4fa9-a88c-4f46a2202a06/how-to-save-datagridview-data-into-a-database[^]
 
Share this answer
 

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