C# 如何根据其Id更新datagridview?

C# 如何根据其Id更新datagridview?,c#,.net,sql-server,wpf,datagridview,C#,.net,Sql Server,Wpf,Datagridview,我正在开发一个软件,在wpf中我有一个datagridview,在这个软件中,当单击insert按钮时,我成功地通过datagridview中的字段插入了值。但当我点击更新时,我得到了以下异常。请在下面找到错误截图: 我自己尝试过的代码如下: public partial class Machine : Window { SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename

我正在开发一个软件,在wpf中我有一个datagridview,在这个软件中,当单击insert按钮时,我成功地通过datagridview中的字段插入了值。但当我点击更新时,我得到了以下异常。请在下面找到错误截图:

我自己尝试过的代码如下:

public partial class Machine : Window
{

    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\ATS\ATS Data Management System\ATS Data Management System\ATSDataManagementsystemDB.mdf;Integrated Security=True;User Instance=True");
    public Machine()
    {
        InitializeComponent();
        MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
        BindMyData();
    }
    public void BindMyData()
    {
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM tblmachines", conn);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(ds);
            gridmachine.ItemsSource = ds.Tables[0].DefaultView;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        finally
        {
            conn.Close();
        }
    }


    private void btninsert_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("INSERT INTO tblmachines VALUES('" + txtid.Text + "','" + txtsalesdate.Text + "','" + txtname.Text + "','" + txtpartname.Text + "','" + txtprice.Text + "','" + txtquantity.Text + "','" + txttotalamount.Text + "','" + paidpending.Text + "','" + txtpaidamount.Text + "','" + txtpendingamount.Text + "','" + txtcreditamountpaid.Text + "','" + txtpaiddate.Text + "','" + txtpaymentmode.Text + "')", conn);
            comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        finally
        {
            conn.Close();
            BindMyData();
        }
    }

    private void btnupdate_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("UPDATE tblmachines SET SalesDate='" +txtsalesdate.Text+ "',Name='" +txtname.Text+"',Part Name='"+txtpartname.Text+"',Price="+txtprice.Text+",Quantity="+txtquantity.Text+",Total Amount="+txttotalamount.Text+",PaymentStatus='"+paidpending.Text+"',Paid Amount="+txtpaidamount.Text+",Pending Amount="+txtpendingamount.Text+",Credit Amount Paid="+txtcreditamountpaid.Text+",Paid Date='"+txtpaiddate.Text+"',Payment Mode='"+txtpaymentmode.Text+"'WHERE Id=" + txtid.Text + "", conn);
            comm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
        finally
        {
            conn.Close();
            BindMyData();
        }
    }
}
}


我希望数据得到更新并显示在数据网格视图中,但它没有发生。

检查您的
零件名称
已付款金额
待付款金额
已付款信用金额
列,我认为错误是列名称

SqlCommand comm = new SqlCommand("UPDATE tblmachines SET SalesDate='" +txtsalesdate.Text+ "',Name='" +txtname.Text+"',Part Name='"+txtpartname.Text+"',Price="+txtprice.Text+",Quantity="+txtquantity.Text+",Total Amount="+txttotalamount.Text+",PaymentStatus='"+paidpending.Text+"',Paid Amount="+txtpaidamount.Text+",Pending Amount="+txtpendingamount.Text+",Credit Amount Paid="+txtcreditamountpaid.Text+",Paid Date='"+txtpaiddate.Text+"',Payment Mode='"+txtpaymentmode.Text+"'WHERE Id=" + txtid.Text + "", conn);
您还应该在
中添加空格,其中

显示您的表结构

这是我的表结构截图:@nante gwapooh我明白了,我认为您应该在+txtname.text+和其他textboxesSqlCommand comm=new SqlCommand(“更新tblmachines SET SalesDate=”+txtsalesdate.text+”,Name=“+txtname.text+”,Part Name=”,Part Name=“+txtpartname.text+”,Price=”+txtprice.Text+“”,数量=“+txtquantity.Text+”,总金额=“+txttotalamount.Text+”,付款状态=“+paidpending.Text+”,已付款金额=“+txtpaidamount.Text+”,待付款金额=“+txtpendingamount.Text+”,已付款金额=“+txtCreditAmountPaidAmountPaid.Text+”,付款日期=“+txtpaiddate.Text+”,付款方式=“+txtpaymentmode.Text+”+“'WHERE Id=“+txtid.Text+”,康涅狄格州);您尝试过在字段名中加大括号吗?例如:[Part name]=”“+txtpartname.Text+””PS。我不是MSSQL用户,我使用MYSQL:),但我会尝试帮助您完成它的工作我尝试过在字段名周围加大括号!谢谢!