Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView Binding with DataTable, not updating after deleting row

So the basics: I've got a window with a ListView on it, which is populated by my grid's datacontext:

mainGrid.SetBinding(Grid.DataContextProperty, 
    new Binding() { 
        Source = new DataView() 
            { Table = SQLHandler.GetHandler[classType.ToString()] } 
    }
);

in xaml:

<ListView Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding}">

everything works fine, it's populated. As you can see above, i've got an SQLHandler class which can be accessed by Singleton, and I can access my tables with an indexer.

The problem: window loads up, i'm selecting a row, clicking the Edit button, new window loads up, where i get the selected row's details. when i delete this row via this new window and close it, the main window (where the complete datatable is shown) is not updated accordingly. i know what the solution should be, but I can't make it work. (inotifyproperty changed interface to SqlHandler class, Binding.IndexerName etc..)

here is the main thing: the dataset is not in my SqlHandler class, it's in SqlExecuter, where all of my sqlcommands are being executed.

public override DataTable this[string key]
{
    get
    {
        if (sqlExecuter.GetDataSet.Tables.Contains(key)) 
            return sqlExecuter.GetDataSet.Tables[key];
        throw new KeyNotFoundException("The specified key was not found");
    }
}

where GetDataSet is:

public DataSet GetDataSet
{
    get { return ds; }
}

How can I make this work? When I delete a row in a different window and close that one, the mainwindow's listview doesn't update itself. The only option I have is to put a refresh button up, and then rebind the datacontext property, then of course it's working, but my goal is to have a 'live' update system, that's what Binding is for after all.

What I've tried: GetDataSet in SqlExecuter: implemented the inotifypropertychanged interface, but nothing changed. and i can't have inotifypropertychanged implemented on my indexer in SqlHandler, because it doesn't have a setter, I'm always just accessing the tables from code-behind, my sqldataadapter is populating them (Fill method)

p.s: i don't really plan on creating an ObservableCollection, because 90% of my code should be rewritten and when i delete a row, i clear my dataset and fill it up again, so I'm not even expecting it to notice every change, just when i refill my datatable, my listview should know about it.. and refresh itself

like image 244
user1930132 Avatar asked Dec 26 '12 15:12

user1930132


1 Answers

I think using a second window as a popup might be the problem here. If you were doing the editing on the same page then you could use a simple

ListView1.DataBind()

To refresh the contents of the list at the end of the delete command. Or you could use the IsPostBack method to update the list if the page was bring redrawn rather than reloaded.

You could try calling the page name and then the list view from your other window but I'm not sure if you can perform that kind of command from another window.

Alternatively you could do the editing on a different page, rather than a separate window so when you return to the original page the listview is being redrawn.

Like yourself I'm sure there is a simpler solution to your problem, but unfortunately I don't know it.

like image 96
Matthew Price Avatar answered Oct 06 '22 00:10

Matthew Price