Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to Edit and Update in the DataGridview in winform app using c# .
If click Update it is Changed in the database also...
Posted
Comments
Menon Santosh 8-Nov-11 0:05am    
Sampath U can find these solution in Ur text book :)
Hasun Muhmdi 22-May-21 1:05am    
Because that performed correctly several times

If You use dataSet then see whether it have query for update or not in table adapter.
Now take a button on which click the data will be saved in database.
on this button click event use

C#
private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                yourTableAdapter.Update(yourDataSet.tablename);
                MessageBox.Show("Record Updated", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Some Error in Update", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
 
        }
 
Share this answer
 
Comments
Sam Path 8-Nov-11 0:14am    
How can i define the tablename....
in the line
yourTableAdapter.Update(yourDataSet.tablename);
uspatel 8-Nov-11 0:33am    
are you bind your datagridview by coding or Wizard.
Sam Path 8-Nov-11 0:45am    
by coding...
uspatel 8-Nov-11 1:59am    
when you will create it from wizard, table adapter and queries for table adapter are auto generated.
I think it will be easy to do through wizard.
otherwise
set table of dataset as
myDataSet = new DataSet();
mySqlDataAdapter.Fill(myDataSet,"tablename");
Sam Path 8-Nov-11 4:05am    
Thank you got it :)
Dataset is the main part in all your operations.

1. Fetch the values in dataset from database.
2. Bind it with gridview. Put gridview editable (so user edit values whatever
they want)
3. after editing put gridview table to dataaset.
4. finally update dataset to database.

Gridview changes to dataset.

C#
DataTable dt = (DataTable)GridView1.DataSource; 
 Dataset ds = new dataSet;
 ds.tables[0] =  dt 


and then Save Dataset Changes to a Database[^]
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
v2

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