BradG
BradG

Reputation: 13

Refresh a WPF Page

I have an application that I have written to view logs from custom applications that have been written for this company. I have it set up using an Entity Model and would like to dynamically change which database I am hitting (test database or production database) based on a menu selection. I have tried using an EntityConnectionStringBuilder and it appears to work with no errors, however I can not get the grid that displays the list of applications to update after changing the data connection string. (the connection strings for test and production servers are stored in the properties.settings) Here is what I have for switching to the test server

try
{
  XDocument doc = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
  var query1 = from p in doc.Descendants("connectionStrings").Descendants()
             select p;
  foreach (var child in query1)
  {
    foreach (var atr in child.Attributes())
    {
        if (atr.Name.LocalName == "name" && atr.Value == "AppsEntities")
        if (atr.NextAttribute != null && atr.NextAttribute.Name == "connectionString")
        {
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(atr.NextAttribute.Value);
            entityBuilder.ProviderConnectionString = Properties.Settings.Default["TestServer"].ToString();
            atr.NextAttribute.Value = entityBuilder.ToString();
        }
    }
  }
  doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
  lblDatabase.Content = "Connected to the Test Server";
}
catch (Exception ex)
{
  MessageBox.Show("changing connection string to Test - blew up!" + ex.Message, "massive error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}

Any help would be appreciated I am new to C# and WPF's so I may not be going about this the correct way.

 <telerik:RadGridView x:Name="LogApplicationGrid"
                         Width="900"
                         Height="Auto"
                         Margin="2,53,0,0"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         AutoGenerateColumns="false"
                         GridLinesVisibility="Horizontal"
                         IsReadOnly="true"
                         MouseDoubleClick="LogApplicationGrid_MouseDoubleClick"
                         ShowGroupPanel="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn MaxWidth="50"
                                        DataMemberBinding="{Binding AppID}"
                                        Header="AppID"
                                        IsFilterable="False"
                                        UniqueName="appNumber" />
            <telerik:GridViewDataColumn MaxWidth="300"
                                        DataMemberBinding="{Binding AppName}"
                                        Header="Application Name" />
            <telerik:GridViewDataColumn MaxWidth="350"
                                        DataMemberBinding="{Binding AppDesc}"
                                        Header="Application Description" />
            <telerik:GridViewDataColumn MaxWidth="150"
                                        DataMemberBinding="{Binding DateCreated}"
                                        Header="Date Created"
                                        IsFilterable="False" />
            <telerik:GridViewCheckBoxColumn MaxWidth="100"
                                            DataMemberBinding="{Binding ShowInSharePoint}"
                                            Header="SharePoint"
                                            IsFilterable="False" />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>

Upvotes: 1

Views: 2616

Answers (1)

Rachel
Rachel

Reputation: 132548

Bind your GridView to an ObservableCollection<T>, then to update your Grid's data, simply update the collection.

Use an ObservableCollection<T> instead of a List<T> because it will automatically alert the UI that it needs to be updated when the collection changes.

Also, be sure you're binding the value instead of setting it. Setting the value will set the value to an instance of the data, not the actual data itself, so it won't respond to changes.

For example, this will not update the UI

LogApplicationGrid = SomeCollection;
SomeCollection = GetObservableCollection();

But this will

var b = new Binding();
b.Source = SomeCollection;
LogApplicationGrid.SetBinding(RadGridView.ItemsSourceProperty, b);

SomeCollection = GetObservableCollection();

and this also will

<telerik:RadGridView ItemsSource="{Binding SomeCollection}" ... />

Upvotes: 1

Related Questions