Click here to Skip to main content
13,004,446 members (74,933 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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);
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter("Select barcode, name, price from Product WHERE barcode='" + textBox1.Text + "'", conn);
            MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);
            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;
Posted 9-Apr-13 3:10am
Updated 9-Apr-13 23:01pm
CHill60 9-Apr-13 9:26am
Your data set only ever has one row in it and you're creating a new DataSet each time. Instead of creating a new dataset, add the new row to it
Member 9611735 9-Apr-13 9:30am
I am unsure how to do that. Can you give me some pointers?
CHill60 9-Apr-13 10:14am
Will get back to you ...
Member 9611735 9-Apr-13 10:32am
Thank you!
richcb 9-Apr-13 10:20am
Another thing to take into consideration is the vulnerability of sql injection your code presents. Look into parameterized queries to thwart that danger.
Member 9611735 9-Apr-13 10:32am
I will take that on board. Thank you!
CHill60 10-Apr-13 5:01am
I haven't got WPF dataGrid here so I'm going to delete my solution and add the WPF tag to your question so that it goes back into the pot
Member 9611735 10-Apr-13 10:51am
thank you for your help CHill60. I have decided to make a temporary transactions table to help me with my problem.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170627.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100