简体   繁体   English

更新数据网格 WPF 中的单个单元格

[英]Update single cell in datagrid WPF

I'm having a datagrid in my WPF application.我的 WPF 应用程序中有一个数据网格。 In one coulumn I have an int (column name = Amount).在一个列中,我有一个 int(列名 = Amount)。

So for example there will be a number "4" in the cell.例如,单元格中会有一个数字“4”。 I can edit "4" in the DataGrid to "3".我可以将 DataGrid 中的“4”编辑为“3”。

After Editting I will push the button "Update" so my Database column Amount will be updatet.编辑后,我将按下“更新”按钮,以便我的数据库列金额将被更新。

It is working, but it update all the cells in the column Amount to the Id number of the chosen row.它正在工作,但它将 Amount 列中的所有单元格更新为所选行的 Id 号。

This is my code in de xaml.cs file:这是我在 de xaml.cs 文件中的代码:

 private void Update(object sender, RoutedEventArgs e)
    {
        DataRowView o = (DataRowView)g2.SelectedItem;
        int Amount= Convert.ToInt32(o.Row.ItemArray[0]);

        try
        {
            const string query = @"UPDATE [Stock] SET [STOCK].Amount = @Aantal;";
           
            using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"...."))
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.Add("@Amount", SqlDbType.Int).Value = Amount;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            MessageBox.Show("Update complete");
            binddatagrid();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error occurred:\r\n" + ex.Message);
        }

    }

What am I doing wrong?我究竟做错了什么?

Your database query is updating the Amount column in every row in the [Stock] table.您的数据库查询正在更新[Stock]表中每一行的Amount列。 You need to add a WHERE clause to your database query so that you only update the [Stock] row in the database that corresponds to the selected row in the DataGrid .您需要在数据库查询中添加WHERE子句,以便仅更新数据库中与DataGrid中选定行相对应的[Stock]行。

I don't know what your database schema looks like, but I'm assuming that the [Stock] table has an Id column.我不知道您的数据库架构是什么样的,但我假设[Stock]表有一个Id列。 If so, the query might look something like this:如果是这样,查询可能如下所示:

UPDATE [Stock] SET [Stock].Amount = @Anatal WHERE [Stock].Id = @Id

Notice that the query now has a second parameter, @Id .请注意,查询现在有第二个参数@Id That means that you'll need to get the Id from the selected row in much the same way that you're currently getting the Amount .这意味着您需要以与当前获取Amount相同的方式从所选行获取Id

int id = Convert.ToInt32(o.Row.ItemArray[1]);

I used o.Row.ItemArray[1] , but I don't know what index the Id will actually be stored at.我使用o.Row.ItemArray[1] ,但我不知道Id实际存储在哪个索引处。 You'll have to use that index to get the correct Id .您必须使用该索引来获取正确的Id

Since your query has a second parameter, you also need to add it to the Parameters collection of the SqlCommand instance.由于您的查询有第二个参数,因此您还需要将其添加到SqlCommand实例的Parameters集合中。 Just like how you're doing with Amount .就像您对 Am Amount所做的那样。

cmd.Parameters.Add("@Id", SqlDbType.Int).Value = id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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