在datagrid中添加行 [英] adding rows in datagrid

查看:59
本文介绍了在datagrid中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个非常基本的epos类型应用程序,一旦用户在texbox中添加条形码和数量并单击按钮,数据网格就会显示添加的信息。目前代码工作正常,直到我向datagrid添加新的销售。当我单击该按钮时,它会对添加到datagrid的第一个记录进行过度保护。当我将它们添加到数据网格时如何显示所有销售?



代码到目前为止:



I am developing a very basic epos type application where once the user adds a barcode and quantity into texbox and clicks a button, the datagrid shows the information added. At the moment the code works fine until I come to adding a new sale to datagrid. When I click the button, it over-rights the first recorded added to datagrid. How do I get all sales to show when I add them to datagrid?

Code so far:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            MySqlConnection conn = new MySqlConnection(connectionSQL);
            
           conn.Open();

            DataSet ds = new DataSet();

            MySqlDataAdapter da = new MySqlDataAdapter("Select barcode, name, price from Product WHERE barcode='" + textBox1.Text + "'", conn);

            MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);

            da.Fill(ds);
            ds.Tables[0].Columns.Add(new DataColumn("Quantity"));

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                ds.Tables[0].Rows[i]["Quantity"] = textBox2.Text;
            }

            this.dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
            conn.Close();

推荐答案

你有2种方法可以解决它



1.按照Chill60的状态,而不是每次在数据集中添加一行时创建一个新的数据集(dataset.Rows.Add())



2.在您的代码中,您要为网格分配数据源,不要这样做。向网格添加行,并为每一行分配值。



datagrid.Rows.Add()
You have 2 ways to solve it

1. As state by Chill60, instead of creating a new dataset every time add a row in the dataset (dataset.Rows.Add())

2. In you code you are assigning the datasource for the grid, dont do that. Add rows to the grid and for each row, assign values to each column.

datagrid.Rows.Add()


这篇关于在datagrid中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆