简体   繁体   中英

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)
{
        InitializeComponent();

        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();
        sda6.Fill(dt5);
        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)
             {
                 gridView1.CloseEditor();
                 gridView1.UpdateCurrentRow();
             }   
         }
}

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

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

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

Example:

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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM