4 Replies - 592 Views - Last Post: 29 April 2013 - 07:49 PM Rate Topic: -----

#1 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Adding from Datagrid to Listbox on another window through sql server

Posted 28 April 2013 - 06:43 PM

Hey guys, so here is my problem. I have a SearchScreen window, which has a datagrid in it. Now when a user selects a row of the datagrid and hits "Add to Cart" button. That row is added to a table (tblCart) in my table on sql server 2008. This works fine and the row goes to the table on the server. Now the problem is that when that row is added to the table on the server I want it to update the listbox on another window and it is not doing so. So basically, when I close the SearchScreen window, I want to see the new row on the listbox on the SalesScreen window(which is always open). Here is the codes I have thus far:

My addtocart button on the SearchScreen window:
private void addBtn_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drView = (DataRowView)dGrid.SelectedItem;
            String s = Convert.ToString(drView.Row["ID"]);
            String cmdString = "INSERT INTO tblCart(Title, GenreID, Price, Year, UML, Quantity) Select Title, GenreID, Price, Year, UML, Quantity FROM tblMovies WHERE ID = " + s;
            SqlCommand cmd = new SqlCommand(cmdString, cn);
            cmd.ExecuteNonQuery();
        }


The .xaml for the listbox on the SalesScreen:
<ListBox Height="261" HorizontalAlignment="Left" Margin="43,69,0,0" Name="listBox1" VerticalAlignment="Top" Width="240"/>


Is This A Good Question/Topic? 0
  • +

Replies To: Adding from Datagrid to Listbox on another window through sql server

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Adding from Datagrid to Listbox on another window through sql server

Posted 29 April 2013 - 04:31 AM

This is really more a C# / WinForms / WPF question than MS SQL.

What you want is to either call the window with the list box to tell it to refresh or have an event on the update form to notify others of change; the latter being preferable.

You have a main window. That window should know about both windows in question, if it's not one of them. Have that window respond to an event after your button click and call a refresh on the window that needs refreshing.
Was This Post Helpful? 1
  • +
  • -

#3 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Re: Adding from Datagrid to Listbox on another window through sql server

Posted 29 April 2013 - 08:01 AM

Hey again everybody, so I am having this new problem with a listbox in my main window (SalesScreen). I have a window called SearchScreen that has a datagrid that is populated from a table on a sql server. Now my goal is to be able to select a row from this datagrid and click a button that says "Add to Cart". When this button is clicked it will successfully sends the selected row to the new table that is populating a Listbox on the main window. Keep in mind both windows stay open at the same time. So the problem I am having is that the ListBox does not update automatically when the tblCart changes on the server. Any advice on how this could be done, once again I'm still in a beginners stage on c# ;)/>/>?

This is my xaml that starts the ListBox(just a basic ListBox):
<ListBox Height="261" HorizontalAlignment="Left" Margin="43,69,0,0" Name="listBox1" VerticalAlignment="Top" Width="240"/>


This is a method that is used for a textbox changed on the SalesScreen (The main window). When the textbox changed event takes place the row is added to the server and the ListBox is properly updated in the window. I tried to call this event with the "Add to Cart" button click but it had no change. Now if I use the "Add to Cart" button on the SearchScreen and then come back to the main window and do the textboxchanged event, it will update both items added.

public void addtoList()
        {
            cn.Open();
            String cmdString = "Select Title, Price from tblCart";
            SqlCommand cmd = new SqlCommand(cmdString, cn);
            SqlDataReader dr = cmd.ExecuteReader();
            double subT = 0;
            double tax = 1.09;
            double total = 0;
            while (dr.Read())
            {
                int count = dr.FieldCount - 1;
                for (int i = 0; i < count; i++)
                {
                 
                    listBox1.Items.Add(dr["Title"].ToString() + dr["Price"].ToString());
                    subT += Convert.ToDouble(dr["Price"]);
                    
                }
                
            }
            total = subT * tax;
            subTotal.Text = subT.ToString();
            totalBlk.Text = total.ToString();
            cn.Close();
        }


This is the addtoCart button:

private void addBtn_Click(object sender, RoutedEventArgs e)
        {
           SalesScreen ss = new SalesScreen();

            DataRowView drView = (DataRowView)dGrid.SelectedItem;
            String s = Convert.ToString(drView.Row["ID"]);
            String cmdString = "INSERT INTO tblCart(Title, GenreID, Price, Year, UML, Quantity) Select Title, GenreID, Price, Year, UML, Quantity FROM tblMovies WHERE ID = " + s;
            SqlCommand cmd = new SqlCommand(cmdString, cn);
            cmd.ExecuteNonQuery();

            ss.addtoList();
        }


So basically, I can update it from the same window using the textchanged event, but when it comes to sending it from one window to another I have been most unsuccessful with this challenge. Any advice would be more than greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: Adding from Datagrid to Listbox on another window through sql server

Posted 29 April 2013 - 08:20 AM

Duplicate topics merged.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Adding from Datagrid to Listbox on another window through sql server

Posted 29 April 2013 - 07:49 PM

Part of the issue is that even though you are using WPF, your are still writing code in the WinForms/Win32 style of imperative event driven programming. If you had gone all the way and also followed the MVC (or MVVM) approach, then you would have to simply update part of your Model, and your two views (eg. the two windows you are trying to get to communicate with each other) would automatically update themselves assuming that you've set up the correct data bindings.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1