Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF DataGrid Help
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();
Posted 9-Apr-13 4:10am
Edited 10-Apr-13 0:01am
CHill6066.9K
v3
Comments
CHill60 at 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 at 9-Apr-13 9:30am
   
I am unsure how to do that. Can you give me some pointers?
CHill60 at 9-Apr-13 10:14am
   
Will get back to you ...
Member 9611735 at 9-Apr-13 10:32am
   
Thank you!
richcb at 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 at 9-Apr-13 10:32am
   
I will take that on board. Thank you!
CHill60 at 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 at 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
good
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.
 
datagrid.Rows.Add()
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
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