How to apply changes from DataGridView to DataTable?

Hello,

I got a simple problem. I created a new DataGridView control and bound a DataTable on it. The problem is that if I alter data in the DataGrid nothing is changed in the DataTable not even if I call  DataTable.AcceptChanges().
I thought it should be simple task, but I just figured out some ways of changing data in combination with SQL-Databases.

bye


Answer this question

How to apply changes from DataGridView to DataTable?

  • xplain

    can you please explain, how you got it working. i have the same issue.thanks.
  • Dhori Alpesh

    Hi,

           I have a gridview which has datasource from a datatable. I have a

    System.Boolean column in the datatable which shows up as a checkbox in gridview.

    When I click on edit button on a row for second time: update, cancel buttons appear.

    Nothing happens for the first time. The same is the case for Update button. I dont know why.

     How to make, changes in the row  reflect permanently in gridview The gridview does not hold new edit values after the update button is pressed

    The code I have

    in .aspx page  

    <asp:GridView ID="Description" runat="server" autogeneratecolumns="true"

    autogenerateeditbutton="true" onrowediting="Description_RowEditing" OnRowUpdating="Description_RowUpdating" DataKeyNames="Column_name,Length">

    </asp:GridView>

    in aspx.cs

    protected void Description_RowEditing(Object sender, GridViewEditEventArgs e)

    {

    Description.EditIndex = e.NewEditIndex;

    }

    protected void Description_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

    Description.EditIndex = -1;

    }

     


  • MCAD_Infinity

    This is a common problem even for the coders out there. I am not a confident coder yet and so am not trying to tweek things that way. I posted some questions and information about this for the help instructions about Managing Your Records, FirstDataBase, Addresses,

    projects because the easy way does not work as the teachings explain.

    What I think is needed is some code to copy the data files that you did make changes in to some place safe.

    This is because, as is explained (here), the project data file gets copied over in the \bin\debug and bin\release directories when the project is run again.

    Could there be an easy solution to be put in the code file so that the updated file can be copied out of the working directory



  • t0PPy

    I may be having a similar problem.  How did you solve yours

  • TylerAKAsection8

    Hi,

           I got it working

    Thanks,

    Rajesh 


  • Bogdan B

    Hi,

    Do you mean that the changes in your DataGridView isn't sent directly to the Database Coz I just tried checking the datatable and it works. I added a datagridview and set a datatable as its datasource. After manually changing the data in the gridview, I checked the datatable and it was changed.

    First, the datatable is just a in-memory representation of your table. So any changes made to the datatable would not directly be sent to the database. To send these changes to the database, you would need an adapter, specify its INSERT, DELETE, UPDATE command, and just call the adapter.Update(datatable) to send your updates to the database...
    ex:



    SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Table1", conn);
    adapter.InsertCommand = new SqlCommand("INSERT INTO Table1 VALUES(@id, @name)", conn);
    adapter.InsertCommand.Parameters.Add("@Id", SqlDbType.Int, 8, "Id");
    adapter.InsertCommand.Parameters.Add("@Name", SqlDbType.Int, 8, "Id");
    // do similar to DELETE and UPDATE commands

     


    cheers,

    Paul June A. Domag


  • RajuRaman

    According to the help page that I posted this setting is not recommended because of unpredictable behavior. But thanks for the directions to where it is settable. My question was more like how to write some code to copy that updated file out to some temporary place maybe like another project to just view any changes since it gets copied over by the origianal project. This must be a typical topic for most database administrators as to the standard operating procedures for daily backups. Is the origianal copy saved for a period of time until it is no longer needed Where are the updated files typically stored Things of this nature. I'll post the full url for the help page again:

    ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vsnav/html/3ffa1aa9-17e4-422c-a02f-09224828cdfc.htm

    Also this beginners page is very useful:

    ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/dv_vbcnexpress/html/1ccdb8f1-4162-4a54-af17-231007eb529b.htm

    I've gone through this and it is very easy to include all the components to your form. The assumption that I made at first is that it didn't update after changes to the datagridview. But if the project is not run again yet, I can open the table in the \bin\debug directory and see that they were changed even though the directions never said anything about this step. The question is what shall we do with this file now Copy it back into the project Copy it to a temp directory What do the "big boys" do when they work with data files   I've see a lot of posts on other forums about this issue with these newer controls. I think that I understand them better now and that they are simple and they do work well.

      Could we discuss some next steps   

    There is a nice little code snippet to copy files I found. This won't work though because you cannot access a file while it is use. I did put a second project into the Addresses one to just read the updated file in the \bin\debug. Perhaps this could copy it before it is accessed.



  • Rob Buttenhoff

    What I think is needed is some code to copy the data files that you did make changes in to some place safe.

    This is because, as is explained (here), the project data file gets copied over in the \bin\debug and bin\release directories when the project is run again.

    Could there be an easy solution to be put in the code file so that the updated file can be copied out of the working directory


    In the Solution Explorer select the data base file. In the properties pane under the Advanced section there is a setting called Copy To Output Directory. Change this setting to "Copy if newer". This way if you make changes to the data in your program they will be preserved, but if you change the database in the project directory it will be copied over.


  • JuanJ

    Hi. I Am using a datagridview where I am allowing user to Add, update and delete rows from the datagrid.

    Update and delete are working fine but when I am trying to add more than one row then the problems comes . If I am adding 4 rows

    its saving the first rows values in all the four records. Can anybody have idea about this. I am using sqldataadapter for adding,updating and deleting rows.My code is below

    Dim myBuilder As SqlCommandBuilder = New SqlCommandBuilder(adapterGetBudget)

    Dim dtdate As Date = Date.Now

    Dim cmd As New SqlCommand()

    With cmd

    .CommandType = CommandType.StoredProcedure

    .CommandText = "sp_AddDetailsBudget"

    .Connection = Conn

    .Parameters.Add("@User", SqlDbType.VarChar).Value = currUserName.ToString

    .Parameters.Add("@Date", SqlDbType.SmallDateTime).Value = dtdate.Date.ToString

    .Parameters.Add("@Time", SqlDbType.VarChar).Value = dtdate.ToShortTimeString.ToString

    .Parameters.Add("@Lock", SqlDbType.Bit).Value = 0

    .Parameters.Add("Description", SqlDbType.VarChar).Value = txtBudgetDesc.Text.Trim

    .Parameters.Add("@BudgetID", SqlDbType.BigInt).Value = Me.txtBudgetID.Text

    .Parameters.Add("@Amount", SqlDbType.Real).Value = Me.dgbudgetdet.Rows(0).Cells(1).Value

    .Parameters.Add("@AccountID", SqlDbType.BigInt).Value = Me.dgbudgetdet.Rows(0).Cells(2).Value

    .Parameters.Add("@CostCenter", SqlDbType.VarChar).Value = Me.dgbudgetdet.Rows(0).Cells(3).Value

    .Parameters.Add("@Notes", SqlDbType.VarChar).Value = Me.dgbudgetdet.Rows(0).Cells(4).Value

    End With

    If Conn.State = ConnectionState.Open Then Conn.Close()

    Conn.Open()

    adapterGetBudget.InsertCommand = cmd

    If (ds.HasChanges) Then
    adapterGetBudget.InsertCommand
    adapterGetBudget.UpdateCommand = myBuilder.GetUpdateCommand()
    adapterGetBudget.DeleteCommand = myBuilder.GetDeleteCommand()
    adapterGetBudget.Update(ds)
    End If
    ds.AcceptChanges()



  • SSG2006

    Was your solution to get the page to do a postback

  • How to apply changes from DataGridView to DataTable?