Home Forums WPF controls Xceed DataGrid for WPF Refreshing ADO-related data source

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #26028 |

    Hi, I must be missing something obvious, but I cannot manage to have the grid refresh properly when bound to a DataTable. The first time data are loaded OK, but whenever I re-bind the grid to a new DataTable as many rows as the expected records do appear, but there is nothing into them, they’re empty. Could anyone help?

    Here are the steps to reproduce the problem:

    1) create a new WPF application and add this XAML code to the main window (it’s just a button and a datagrid with some columns):

    <Window x:Class=”XcIssue1.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid&#8221;
    Title=”Window1″ Height=”300″ Width=”300″ Loaded=”Window_Loaded”>
    <StackPanel>
    <Button Content=”Refresh” Click=”_btnRefresh_Click”/>
    <xcdg:DataGridControl x:Name=”_dgSample” AutoCreateColumns=”False”>
    <xcdg:DataGridControl.Columns>
    <xcdg:Column FieldName=”id” Title=”ID” ReadOnly=”True”/>
    <xcdg:Column FieldName=”name” Title=”Name”/>
    </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
    </StackPanel>
    </Window>

    2) add code behind to fill the grid and refresh it whenever you click the button:

    private DataTable GetData()
    {
    // create dummy data table
    DataTable dt = new DataTable(“TDummy”);

    DataColumn col = new DataColumn(“id”, typeof(int));
    col.AllowDBNull = false;
    col.Unique = true;
    dt.Columns.Add(col);

    col = new DataColumn(“name”, typeof(string));
    col.AllowDBNull = false;
    col.DefaultValue = “”;
    dt.Columns.Add(col);

    // fill with some data
    System.Data.DataRow row;
    for (int i = 0; i < 10; i++)
    {
    row = dt.NewRow();
    row[“id”] = i + 1;
    row[“name”] = new string((char)(i + 65), 3);
    dt.Rows.Add(row);
    }

    return dt;
    }

    private void LoadData()
    {
    DataTable dt = GetData();
    DataGridCollectionView dgcv = new DataGridCollectionView(GetData().DefaultView);
    dgcv.SortDescriptions.Add(new SortDescription(“name”, ListSortDirection.Ascending));

    _dgSample.ClearValue(DataGridControl.ItemsSourceProperty);
    _dgSample.Items.Clear();
    _dgSample.ItemsSource = dgcv;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    LoadData();
    }

    private void _btnRefresh_Click(object sender, RoutedEventArgs e)
    {
    LoadData();
    }

    3) run the sample. The window loads and data are shown OK. Now click refresh: there will still be 10 rows, but empty.

    Imported from legacy forums. Posted by Mathetes (had 5618 views)

    Xceed Support
    Member
    Post count: 5658

    Because you are re-creating new DataColumns, the DisplayMemberBindings originally created for the DataTable are not working anymore (they are based on the custom property descriptor the DataView injects for the DataColumns ).

    Either set the DisplayMemberBinding manually on the columns or clear the DataGridControl.Columns collection before assigning the new items source.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 552 views)

    User (Old forums)
    Member
    Post count: 23064

    Thank you very much for your reply; as for the suggested solutions, I’d like to keep columns definitions in XAML as contrarily to this dummy sample they will never change. So, does this mean that when the DataTable object remains the same with no change in its schema simply changing its contents (rows) would be enough? if I change the code behind as below, the grid control is never updated to reflect new records. What’s the suggested way of refreshing a datagrid control when the underlying datatable rows change?

    Sample changes:

    1) set the data table as a member and create it only once:

    private DataTable _dt;

    private void LoadData()
    {
    DataGridCollectionView dgcv = new DataGridCollectionView(_dt.DefaultView);
    dgcv.SortDescriptions.Add(new SortDescription(“name”, ListSortDirection.Ascending));
    _dgSample.ClearValue(DataGridControl.ItemsSourceProperty);
    _dgSample.Items.Clear();
    _dgSample.ItemsSource = dgcv;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    _dt = GetData(); // ONCE for all
    LoadData();
    }

    2) when hitting refresh, clear rows and load new ones:

    private void _btnRefresh_Click(object sender, RoutedEventArgs e)
    {
    _dt.Rows.Clear();
    System.Data.DataRow row;
    for (int i = 0; i < 15; i++)
    {
    row = _dt.NewRow();
    row[“id”] = i + 1;
    row[“name”] = new string((char)(i + 65), 2);
    _dt.Rows.Add(row);
    } //efor
    LoadData();
    }

    Thanks again…

    Imported from legacy forums. Posted by Mathetes (had 743 views)

    User (Old forums)
    Member
    Post count: 23064

    Sorry, as for the sample above it effectively loads the new rows, but the vertical scrollbar of datagrid does not reflect it: it’s like it still had 10 rows, while they have grown to 15. If I resize the host window it refreshes the scrollbar, but this does not happen just after updating the data source.

    Imported from legacy forums. Posted by Mathetes (had 831 views)

    Xceed Support
    Member
    Post count: 5658

    If you want to preserve the same column definitions… you can do as said earlier and place a displaymemberbinding explicitelly on the column:

    <xcdg:Column FieldName=”MyDataColumnName” DisplayMemberBinding=”{Binding MyDataColumnName}” />

    and you can then preserve the code that replaces the data source as for your first post.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 558 views)

    User (Old forums)
    Member
    Post count: 23064

    I have encountered this same problem when using the 2.0 version of the DataGrid. Unfortunately, when I tried the recommended approach of explicitly setting the DisplayMemberBinding, it caused the behavior to occur on the first bind as well as all subsequent binds.

    The Xceed DataGrid documentation recommends using ItemProperties i/o explicitly defining columns, however, the ItemProperties does not allow a Width to be set, so this is not an effective work around.

    I tried several other things including changing the source of the DataGridCollectionViewSource w/o changing the binding, clearing the DisplayMemberBinding of all the columns before changing the binding, and repopulating the DataTable i/o changing the binding. None of these approaches solved the problem.

    The only solution that works is to recreate the columns whenever the binding changes, but that sort of defeats the whole point of using XAML in the first place. Please let me know when this defect is resolved.

    Regards,
    Bryn

    Imported from legacy forums. Posted by Bryn (had 524 views)

    Xceed Support
    Member
    Post count: 5658

    Can you provide any additional information on your setup and the issue encountered?

    XAML used, name of the DataColumns, content of the output pane, …

    Imported from legacy forums. Posted by Marcus [Xceed] (had 520 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello, thanks for the reply.

    It’s the same scenario as is described in the first posting. I have a simple DataGridControl created in XAML with the columns explicitly defined. I’m using a DataGridCollectionViewSource over a DataTable, and when I try to refresh the DataTable by replacing it, repopulating it or rebinding it, the columns don’t display data (though the correct number of rows are displayed).

    Each time it I try the rebind, binding errors are written to the output window stating that <FieldName> is not a property of a DataRow.

    And just as in the example above it works fine the first time, and I can get it to work every time if I recreate the columns every time I recreate the DataTable.

    Regards,
    Bryn

    Imported from legacy forums. Posted by Bryn (had 388 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Bryn,

    I’d really like to see your code (XAML and C#) because based on your description, there is nothing that should prevent you from seeing data.

    Seeing your code would definitely help narrow down the issue.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 6531 views)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.