#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Posts
    2
    Rep Power
    0

    Question Update table in database through datagridview in winform


    Hi all,

    Please help me in solving following query:

    I want to update table at backend in database throughmakeing changes in datagridview.

    What I want is, when User makes changes in datagridview and clicks save or update button, the changes which are made in datagridview should get reflect in table in databse.

    For ex:

    I am having "Datagridview", "Edit" and "Save" button on WinForm and table "EmpDetails" at backend in databse. I am filling "Datagridview" with table "EmpDetails". When user clicked "Edit" button he should allowed to make changes in "Datagridview". After making changes in "Datagridview" when user clicked "Save" button, whichever changes made in datagridview should get reflect at backend in table "EmpDetails".
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Location
    Noida/India
    Posts
    127
    Rep Power
    9
    Use this code to populate datagridview

    SqlDataAdapter da=new SqlDataAdapter("select * from product",strcon);
    SqlCommandBuilder cb=new SqlCommandBuilder(da);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dataGridView1.DataSource=ds.Tables[0];

    //now u can save changes to back end with
    da.Update(ds);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Posts
    2
    Rep Power
    0

    In case of joins in dataset what is the solution


    Thanks

    It is working fine if there is single table in dataset.

    But in my dataset there are 4 table with join (i.e. to fill datagrid I am combining data from 4 tables by using joins). So what should I do in this case?

    Thanks & regards,

    Swapnil
  6. #4
  7. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2004
    Location
    Switzerland
    Posts
    1,152
    Rep Power
    1901
    Hi. I'm working with that right now. I'm new to this so there may be a better solution than this:

    Make a DataAdapter for each Table (ie. Customers and Orders).
    For filling the dataset:
    Code:
    adapterCustomers.Fill(myDataSet, "Customers");
    adapterOrders.Fill(myDataSet, "Orders");
    For updating the Database:
    Code:
    adapterCustomers.Update(myDataSet, "Customers");
    adapterOrders.Update(myDataSet, "Orders");
    If your DataSet or your Database enforce some Relations you have to call the Fill and Update methods accordingly.
    I also had the problem of errors occuring when i altered the primary key of a line in the dataset. That can be solved by making the primaryKey column uneditable.

    Comments on this post

    • handshakeit agrees : I think there will be other solutions also but can't say those would be better....
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0
    Originally Posted by handshakeit
    Use this code to populate datagridview

    SqlDataAdapter da=new SqlDataAdapter("select * from product",strcon);
    SqlCommandBuilder cb=new SqlCommandBuilder(da);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dataGridView1.DataSource=ds.Tables[0];

    //now u can save changes to back end with
    da.Update(ds);

    Thanks it works!

IMN logo majestic logo threadwatch logo seochat tools logo