无法将Gridview的更改更新为数据库

时间:2013-02-04 14:55:15

标签: c# winforms

编辑gridview后无法更新对数据库的更改。没有抛出错误,但数据库中的表中未更新更改。我在这里使用Sql服务器,我有两个名为Form1和Form 2的Winforms。一个按钮(Form1中的btnSearchAll)在Form2中打开gridview,加载了所有数据。当我尝试编辑gridview中的数据(在Form2中)并尝试更新时,它不会更新更改。请帮忙

以下是我的表格1:

    namespace Tailor_app
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
        SqlDataAdapter da;
        DataTable dt;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
        txtFirstName.Focus();
        da = new SqlDataAdapter("select * from Measurement", con);
        da.Fill(ds, "Measurement");
        dt = ds.Tables["Measurement"];
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
            DataRow dr = dt.NewRow();
            dr["CellNumber"] = txtCellNo.Text.Trim();
            dr["FirstName"] = txtFirstName.Text;
            dr["LastName"] = txtLastName.Text;
            dr["Shirt"] = txtShirt.Text;
            dr["Pant"] = txtPant.Text;
            dr["DueDate"] = txtDueDate.Text;
            dr["Date"] = txtDate.Text;
            if (dr["CellNumber"] == "")
            {
                MessageBox.Show("Please enter Cell Number");
            }
            else if (dr["CellNumber"] != "")
            {
                dt.Rows.Add(dr);
                MessageBox.Show("Success");
            }

                try
                {
                    da.Update(ds, "Measurement");
                }
                catch (DBConcurrencyException ex)
                {
                    MessageBox.Show(ex.Message);
                    dt.Clear();
                    da.Fill(ds, "Measurement");
                }
        }

    private void btnSearchAllCustomers_Click(object sender, EventArgs e)
    {
        this.Hide();
        frmDgv_SearchResult frm2 = new frmDgv_SearchResult();
        frm2.Show();

        using (da = new SqlDataAdapter())
        {
            try
            {
                da.SelectCommand = new SqlCommand("Select * From Measurement", con);
                ds.Clear();
                da.Fill(ds,"Measurement");
                dt = ds.Tables["Measurement"];
                frm2.dgvSearchResults.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}

}

以下是我的表格2:

namespace Tailor_app
{

public partial class frmDgv_SearchResult : Form
{
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
    SqlDataAdapter da;
    DataTable dt;

    public frmDgv_SearchResult()
    {
        InitializeComponent();
    }

    private void dgvSearchResults_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

      private void frmDgv_SearchResult_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select * from Measurement", con);
        da.Fill(ds, "Measurement");
        dt = ds.Tables["Measurement"];
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
            {
                da.Update(ds, "Measurement");
            }
            catch (DBConcurrencyException ex)
            {
                MessageBox.Show(ex.Message);
                dt.Clear();
                da.Fill(ds, "Measurement");
            }
            finally
            {
                MessageBox.Show("success");
            }
        }

3 个答案:

答案 0 :(得分:1)

插入,更新或删除后您应dt.AcceptChanges();AcceptChanges提交自上次调用AcceptChanges以来对数据表所做的所有更改

答案 1 :(得分:0)

尝试请求数据适配器的更新命令: -

  private void frmDgv_SearchResult_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select * from Measurement", con);
        da.Fill(ds, "Measurement");
        dt = ds.Tables["Measurement"];
        SqlCommandBuilder cb = new SqlCommandBuilder(da);

        cb.GetInsertCommand();
        cb.GetUpdateCommand();
        cb.GetDeleteCommand();

        cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
    }

答案 2 :(得分:0)

我的看起来更像是这样:

da.Fill(dt)

Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

da.MissingSchemaAction = MissingSchemaAction.AddWithKey     ' includes default field value settings
da.InsertCommand = cb.GetInsertCommand      ' instantiate for transaction
da.UpdateCommand = cb.GetUpdateCommand