简体   繁体   中英

Save datagridView into sql database

First time saving datagrid to database thanks for any help.
I fill dataGrid this way:

 private void FillGrid1()
    {
        frmEditovat frm2 = new frmEditovat(this);
        DataTable DT = null;
        DataRow newRow;
        int pTypNastaveniaID = 0;
        string pNazov = "", pHodnota = "";

        DataConnector_My dataConnector = new DataConnector_My(mDataRoot.ConnectCentrum.ConnectionString);


        try
        {
            dataConnector.Init(mDataRoot.ConnectCentrum);

            DT = dataConnector.Get_Nastavenia();

            /* Odstranim zdroj pre Grid. Musi byt koli triedeniu gridu. */
            dataGridView1.DataSource = null;
            /* Smazanie Tabulky. */
            mDTable1.Clear();

            for (int row = 0; row < DT.Rows.Count; row++)
            {
                pTypNastaveniaID = 0; pNazov = ""; pHodnota = "";

                if (DT.Rows[row]["TypNastaveniaID"] != DBNull.Value) pTypNastaveniaID = Convert.ToInt32(DT.Rows[row]["TypNastaveniaID"]);
                if (DT.Rows[row]["Nazov"] != DBNull.Value) pNazov = DT.Rows[row]["Nazov"].ToString();
                if (DT.Rows[row]["Hodnota"] != DBNull.Value) pHodnota = DT.Rows[row]["Hodnota"].ToString();

                newRow = mDTable1.NewRow();

                newRow["TypNastaveniaID"] = pTypNastaveniaID;
                newRow["Nazov"] = pNazov;
                newRow["Hodnota"] = pHodnota;


                mDTable1.Rows.Add(newRow);
            }


            /* Datovy zdroj zpet. */
            dataGridView1.DataSource = mDTable1;

            Application.DoEvents();
        }
        catch (SqlException e)
        {
            MessageBox.Show(this.GetType() + ".FillGrid: " + Environment.NewLine + e.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            DT.Dispose();
        }

    }

I want to save datagriView1 to the database after click action in button :

  private void btnSave_Click_1(object sender, EventArgs e)

Or some example with similar code would be appreciate .

Since you didn't specify if you're using any TableAdapters I guess your just stuck with your Datatable ...

The simplest would be to first look for the changes and then iterate over them. According to their nature (NEW, UPDATE, DELETED) perform your own queries against the Database.

var dataTable = ((DataTable)dataGridView1.DataSource).GetChanges();
if(dataTable != null && dataTable.Rows.Count > 0)
{
  foreach (DataRow row in dataTable.Rows)
        {
            switch (row.RowState)
            {
                case DataRowState.Added:
                    // DO INSERT QUERY
                    break;
                case DataRowState.Deleted:
                    // DO DELETE QUERY
                    break;
                case DataRowState.Modified:
                    SqlCommand command = new SqlCommand("UPDATE YOURTABLE SET TypNastaveniaID = @typ, Nazov = @title, Hodnota = @amount");
                    command.Parameters.Add(new SqlParameter("@typ", row["TypNastaveniaID"]));
                    command.Parameters.Add(new SqlParameter("@title", row["Nazov"]));
                    command.Parameters.Add(new SqlParameter("@amount", row["Hodnota"]));
                    command.ExecuteNonQuery();
                    break;
            }
        }
    ((DataTable)dataGridView1.DataSource).AcceptChanges();
}

Hope you get the idea. Note the AcceptChanges, which needs to be called on the root Table. It will set the changes to "DONE", so next time you click you wont iterate over the same rows again.

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