【问题标题】:How to update datagridview based on its Id?如何根据 ID 更新 datagridview?
【发布时间】:2019-01-29 05:52:33
【问题描述】:

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

https://imgur.com/a/CV3n77K

自己试过的代码如下:

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();
        }
    }
}

}

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

【问题讨论】:

    标签: c# .net sql-server wpf datagridview


    【解决方案1】:

    检查您的Part NamePaid AmountPending AmountCredit Amount Paid 列,我认为错误是列名

    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);
    

    您还应该在WHERE 中添加空格 显示你的表结构

    【讨论】:

    • 这是我的表格结构的截图:imgur.com/a/g1FmpyA@nante gwapo
    • 哦,我明白了,我认为您应该在 + txtname.text + 和其他文本框之间添加空格
    • SqlCommand comm = new SqlCommand("UPDATE tblmachines SET SalesDate='"+ txtsalesdate.Text +"',Name='"+ txtname.Text +"',Part Name='"+ txtpartname. Text +"',Price='"+ txtprice.Text +"',Quantity='"+ txtquantity.Text +"',总金额='"+ 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);
    • 您是否尝试在字段名称中放置大括号?例如:[零件名称]='"+ txtpartname.text +"' PS。我不是真正的 MSSQL 用户,我使用 MYSQL :),但我会尽力帮助你
    • 感谢它的工作我尝试在字段名称周围使用大括号!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多