2

I am using LINQ to SQL with a Datagrid and binding the data to the auto generated fields. The data updates fine when when submitting changes via LINQ. The problem I am having is I cannot seem to refresh the updated data after I run a SQL statement on the database directly. The Databases data is updated correctly and closing down and re-starting the application the data is updated correctly.

I have tried,

MyTable.Items.Refresh();

And

private DatabaseDataContext sql = new DatabaseDataContext(
            Properties.Settings.Default.StaffConnectionString);

sql.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues);

The XAML used to bind the data to a Datagrid column, Info_Data is the auto generated field from the Database table.

<DataGridTemplateColumn Header="Info" Width="*" SortMemberPath="Words">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock FontWeight="Light" Text="{Binding Path=Info_Data, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>

By all accounts it seems that I need to create an observable collection and bind the data to that and update the collection to refresh the data-grid table. Is there any way around this?

KyloRen
  • 2,691
  • 5
  • 29
  • 59
  • Try: `CollectionViewSource.GetDefaultView(MyTable.ItemsSource)?.Refresh();` – Johan Larsson Jul 06 '16 at 12:39
  • Thanks @JohanLarsson, that is not working either. Rather frustrating this is. – KyloRen Jul 06 '16 at 12:47
  • Just put the items in an ObservableCollection, bind it in the XAML, and get on with your life. You don't get any bonus points for punishing yourself by doing things wrong. But if you're absolutely determined to waste your own time, have you tried raising PropertyChanged for the collection property? Or are you not even binding it? – 15ee8f99-57ff-4f92-890c-b56153 Jul 06 '16 at 13:09
  • @EdPlunkett, did not know this was the wrong way, thanks for the advise. This is my first real application, so still experimenting with a lot of stuff and I will have to re-write a bit of code to bind everything with `ObservableCollection`. Not sure I follow what you mean on the collection property? – KyloRen Jul 06 '16 at 13:23
  • How did you get the items into the grid? The "right" way is to expose the collection as a property of your viewmodel and bind it to DataGrid.ItemsSource in your XAML. I have a feeling that's not what you did, though. Btw I figured you knew ObservableCollection was the right way because you mentioned knowing that it would solve all your problems. But yes, it's not just *a* way, it's *the* way. – 15ee8f99-57ff-4f92-890c-b56153 Jul 06 '16 at 13:29
  • How did you get the items into the grid? The "right" way is to expose the collection as a property of your viewmodel and bind it to DataGrid.ItemsSource in your XAML. I have a feeling that's not what you did, though. Btw I figured you knew ObservableCollection was the right way because you mentioned knowing that it would solve all your problems. But yes, it's not just *a* way, it's *the* way. – 15ee8f99-57ff-4f92-890c-b56153 Jul 06 '16 at 13:29
  • @EdPlunkett, you are correct , I did not bind them that way. My thoughts I `Observablecollection` was that it was *another* way of binding data. – KyloRen Jul 06 '16 at 13:35
  • You *can* put stuff in the UI the way you're doing it (obviously -- there it is!), but, as you've found, you run into a lot of problems trying to reinvent a bunch of wheels that the Framework is eager to provide for you, if you just do things its way. BTW ObservableCollection isn't any way of binding data, it's a collection that raises notification events. If you're not using the [`Binding`](https://msdn.microsoft.com/en-us/library/system.windows.data.binding(v=vs.110).aspx) class, you're not *binding* anything. You're just assigning stuff. – 15ee8f99-57ff-4f92-890c-b56153 Jul 06 '16 at 13:39
  • The [MSDN Data Binding Overview](https://msdn.microsoft.com/en-us/library/ms752347(v=vs.100).aspx) may be helpful. The trouble for a lot of people new to XAML is that it's very far from obvious what the right way is, and the way you did it in winforms is almost always wrong. – 15ee8f99-57ff-4f92-890c-b56153 Jul 06 '16 at 13:40
  • I Just add something I think is better checking, you say you use linq to SQL, to be sure that the updates made to the data in the database are reflected to your list you have also to check that the data classes produced by your queries implement some kind of propertychanged elsewhere, the observable collection will reflect additions or deletions of the objects but if you change contents of the fields of your records they are not shown until you reload the collection. – Sabrina_cs Jul 06 '16 at 13:44
  • @Sabrina_cs, the updates made via LINQ are updating the database using `sqlDatacontext.SubmitChanges();`. I have a propertychanged event and that seems to be working as expected. – KyloRen Jul 06 '16 at 13:53
  • @EdPlunkett, thanks for the link. I will have to thoroughly go over that. – KyloRen Jul 06 '16 at 13:53

1 Answers1

1

You could try SqlDataAdapter and DataTable to load info from database and bind to DataGrid:

var da = new SqlDataAdapter("SELECT * FROM " + view, conn);
var dt = new DataTable();
da.Fill(dt);
var dg = new DataGrid();// Your DataGrid
dg.ItemsSource = dt.DefaultView;

On update:

dt.Rows.Clear();
da.Fill(dt);
dt.AcceptChanges();