Home Ask Login Register

Developers Planet

Your answer is one click away!

Valentine February 2016

Saving gridview updated data to SQL Server?

I can save data in my gridView but I am unable to do so to my datasource. Could there a missing line of code or is there something that I am missing?

Here's my code:

public Marksheet(object val1)

        string connectionString = null;
        SqlConnection conn; 
        connectionString = "Server=localhost\\SQLEXPRESS;Integrated security=SSPI;database=jms";
        SqlDataAdapter sda6 = new SqlDataAdapter("SELECT * FROM grades WHERE class_code='" + val1 + "'", connectionString);
        conn = new SqlConnection(connectionString);
        DataTable dt5 = new System.Data.DataTable();
        gridControl1.DataSource = dt5;

private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
         if (e.Button.ButtonType == DevExpress.XtraEditors.NavigatorButtonType.EndEdit)
             if (MessageBox.Show("Do you want to commit changes to the current record?", "Confirm commit",

                 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) != DialogResult.No)

private void gridView1_CellValueChanged(object sender, CellValueChangedEventArgs e)
    //?? Could there be something I'm missing here? if yes, what could it be?


Aseem Gautam February 2016

When you make any changes to the grid control, the changes are reflected in the datasource, that is in your case a DataTable. If you think logically, it seems correct as the Grid Control is bound to the DataTable and is not aware about how the DataTable gets populated.

Now you can see that the DataTable is populated using DataAdapter. You need to call the DataAdapter.Update(dataTable) method to push the changes to database.

As described here - Posting Data to a Connected Database

Niranjan Kala February 2016

The best approach is already suggested by @Aseem that you need to implement ADO.net binding to commit changes back to the back-end using the DataAdapter. If you can implement this way then check below tutorial:

Tutorial: ADO.NET Data

When binding to a database using ADO.NET, you bind a control to a DataTable containing data from the database. When you change data via the Grid Control (adding, deleting or modifying records), the changes are accumulated in the DataTable. They are not automatically posted to the underlying database. Therefore, you need to manually call a specific method to post the changes.

If you are using custom data table and not willing to implement such binging then you have to handle GridView.RowUpdated Event and then you can post back then changes that you made in current updated row.

Refer this: Xtragrid Row Updated Event


Private Sub gridView1_RowUpdated(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.RowObjectEventArgs) Handles gridView1.RowUpdated
    Dim val As Object
    Dim row As DataRowView = CType(e.Row, DataRowView)
    val = row(0)
End Sub

Hope this help..

Post Status

Asked in February 2016
Viewed 2,270 times
Voted 11
Answered 2 times


Leave an answer

Quote of the day: live life