-1

I have tow windows in WPF APP, In the first windows the admin will add a ticket information, and in other windows, the employee should show a list of all tickets with a new one once have been added(automatically). on the following simple code:

//On User Window, set the itemSource
     DataClasses1DataContext dc = new DataClasses1DataContext(Properties.Settings.Default.AJFactoryConnectionString);
     public MainWindow() 
      {
        if (dc.DatabaseExists()) AllTasksListView.ItemsSource = dc.TicketTables;};
//List View 
<ListView Name="AllTasksListView" >
//Admin Window that allow him to add anew ticekt 
  public DataClasses1DataContext dc = new DataClasses1DataContext(Properties.Settings.Default.AJFactoryConnectionString);
        public void InsertNewTickt(string Status,string Descrption)
        {
            TicketTable x = new TicketTable
            {
                CreatedDate = DateTime.Now,
                Status = Status,
                DeliveryDate = DateTime.Now,
                Descrption = Descrption

            };
            dc.TicketTables.InsertOnSubmit(x);
            try
            {
                dc.SubmitChanges();
            }
            catch (Exception ee)
            {
                dc.SubmitChanges();
            }
}

I need a good way to update listview immediately once the admin adds a new ticket(new row in sql). I am new in WPF, and I find a lot of solutions but in same windows, In my case, I have 2 windows.

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Mohamad Mahmoud Darwish
  • 3,865
  • 9
  • 51
  • 76
  • Possible duplicate of [SQL Server Database Change Listener C#](https://stackoverflow.com/questions/3364148/sql-server-database-change-listener-c-sharp) – Circle Hsiao Oct 31 '18 at 09:16
  • Possible duplicate of [How to update listview automatically after adding a new item into DB](https://stackoverflow.com/questions/15266044/how-to-update-listview-automatically-after-adding-a-new-item-into-db) – Anas Alweish Oct 31 '18 at 09:16
  • @Anas not same question, i am using 2 forms in this question the souition is on same form – Mohamad Mahmoud Darwish Oct 31 '18 at 09:18
  • 1
    I don't know what the truce term is in your culture but your last comment read as "No, Not, can't tag me Im on wood base", could you edit information about if you tried and how it didnt work. – Drag and Drop Oct 31 '18 at 09:27
  • @Anas .. i see right but i am using linq to sql – Mohamad Mahmoud Darwish Oct 31 '18 at 09:28
  • well , `new ObservableCollection(linqQueryResult)` or Warp your datacontext into an observable – Drag and Drop Oct 31 '18 at 09:34
  • @DragandDrop , i am thinking about that, and I am searching a way to apply/code it? – Mohamad Mahmoud Darwish Oct 31 '18 at 09:42
  • I don't have a IDE , or mcve. So I'm just throwing word, expecting them to make sense, I can't provide a test answer that compile out of my head. Btw, I really think that this question will be better with an [mcve]. A simple table, 2 simple form(one with a grid) the other with 2 buttons to add an item. With no exception control or anything complex just the basic. But will all existing limitation. (linq2sql, 2 form, edit from a form and external). It's close to what you already have. – Drag and Drop Oct 31 '18 at 09:49

1 Answers1

0

Set the ItemsSource of the ListView to an ObservableCollection<TicketTable> and add the newly created TicketTable object to this one. The admin window will need a reference to the MainWindow for this to work. You could either inject it with a reference or get one from the Application.Current.Windows property.

MainWindow:

if (dc.DatabaseExists()) 
    AllTasksListView.ItemsSource = new ObservableCollection<TicketTable>(dc.TicketTables);

AdminWindow:

...
dc.TicketTables.InsertOnSubmit(x);
dc.SubmitChanges();
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
if (mainWindow != null)
{
    var sourceCollection = mainWindow.AllTasksListView as ObservableCollection<TicketTable>;
    if (sourceCollection != null)
        sourceCollection.Add(x);
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • is this solution working, if I run the first pc on admin pc and the other windows in other pc? – Mohamad Mahmoud Darwish Nov 01 '18 at 13:29
  • 1
    Of course not. Then you are running two different processes on two different machines which is a totally different topic. – mm8 Nov 01 '18 at 13:30
  • I will test it locally and give you a feedback, i am trying to find a solution for this https://stackoverflow.com/questions/53086524/how-to-make-wpf-apps-on-same-netwrok-read-data-from-one-sql-server – Mohamad Mahmoud Darwish Nov 01 '18 at 13:51
  • @MikeDarwish: That's a competely different question. – mm8 Nov 01 '18 at 13:53
  • mm8, I know that, that is why i am trying to test it on 2 pcs – Mohamad Mahmoud Darwish Nov 01 '18 at 13:56
  • You need to insert the data in the same application instance as you expect it to show up in. There is no remove event being raised from the database to all clients applications that new data has been added. – mm8 Nov 01 '18 at 13:57