locked
DataGrid issue RRS feed

  • Question

    • I am using Linq to Entities data model.
      Also using RIA services.
      I have two entities on is a question which in the model and sql server have a one to many relationship with the answer entity/table.
      my xaml is as follows

    < Grid x:Name="LayoutRoot">

    <ScrollViewer BorderThickness="0" VerticalScrollBarVisibility="Auto" Padding="12,0,12,0" Margin="-12">

    <StackPanel Orientation="Vertical" Margin="0,12,0,12">

    <TextBlock Text="Question Listing" HorizontalAlignment="Stretch" TextAlignment="Center"/>

    <StackPanel Margin="0" Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    <riacontrols:DomainDataSource x:Name="questionDataSource" LoadSize="20" QueryName="GetQuestionQuery" AutoLoad="True">

    <riacontrols:DomainDataSource.DomainContext>

    <ds:TeamsDomainContext/>

    </riacontrols:DomainDataSource.DomainContext>

    </riacontrols:DomainDataSource>

    <datagrid:DataGrid x:Name="questionGrid" AutoGenerateColumns="false"

    CanUserReorderColumns="True"

    IsReadOnly="True" SelectionMode="Single"

    GridLinesVisibility="Horizontal"

    ItemsSource="{Binding Data,ElementName=questionDataSource}"

    SelectionChanged="questionGrid_SelectionChanged">

    <datagrid:DataGrid.Columns>

    <datagrid:DataGridTextColumn Header="Question" Binding="{Binding Question1}"/>

    <datagrid:DataGridTextColumn Header="Question Id" Binding="{Binding QuestionID}" Visibility="Collapsed"/>

    <datagrid:DataGridCheckBoxColumn Header="Archived" Binding="{Binding Archive}"/>

    </datagrid:DataGrid.Columns>

    </datagrid:DataGrid>

    <datagrid:DataGrid Name="answerDataGrid"

    Margin="10">

    </datagrid:DataGrid>

    </StackPanel>

    </StackPanel>

    </ScrollViewer>

    </Grid>
    my code behind is as follows:

    using System.Data;

    using Teams40.Web.Services;

    using System.Windows.Ria.Data;

    using System.Collections;

    using Teams40.Web;

    using System.Data.Services.Client;

    namespace Teams40.Views

    {

    public partial class QuestionList : Page

    {

     

    TeamsDomainContext ctx = new TeamsDomainContext();

     

    public QuestionList()

    {

    InitializeComponent();

     

    }

     

    private void questionGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)

    {

     

    Question q = questionGrid.SelectedItem as Question;

     

    int qid = q.QuestionID;

     

    answerDataGrid.ItemsSource = ctx.QuestionAnswers;

     

    ctx.Load<

    QuestionAnswer>(ctx.GetQuestionAnswerByQidQuery(qid));

     

    }

     

    }

    }

    The answergrid loads the selected answer items but when i change the selected question it loads the answers for that question below the first set.
    This is not the look I am going for and would like to either delete the loaded answers and reload the answer for the selected question or display them in some other user friendly way.

    Any help or suggestions would be appreciated.
    Thanks
    Roby

    Wednesday, August 19, 2009 2:53 PM

Answers

  • If you only want to display the answers to the latest query, you can bind to the entities on the load operation.

      answerDataGrid.ItemsSource = ctx.Load<QuestionAnswer>(ctx.GetQuestionAnswerByQidQuery(qid)).Entities;

    Kyle

    Wednesday, August 19, 2009 9:04 PM

All replies

  • If you only want to display the answers to the latest query, you can bind to the entities on the load operation.

      answerDataGrid.ItemsSource = ctx.Load<QuestionAnswer>(ctx.GetQuestionAnswerByQidQuery(qid)).Entities;

    Kyle

    Wednesday, August 19, 2009 9:04 PM

  • answerDataGrid.ItemsSource = ctx.Load<QuestionAnswer>(ctx.GetQuestionAnswerByQidQuery(qid)).Entities;

    Hi Kyle, eventhough this syntax is very appealing, it still executes the asynchronous load operation. Do you know if there are any side effects when setting up a load operation like this? In the WinForms scene databinding in combination with manipulating the databound object meant you had to suspend databinding to avoid the controls reacting to events. In this asynchronous environment with declarative databinding you would guess things will be worce. There can be several asynchronous, parallel load operations against the same domain context. I haven't tried any of this, but is there a potential risk with such a scenario, should the ViewModel somehow be aware of the pending requests?

    Thursday, August 20, 2009 2:31 AM
  • Thanks guys that solved my issue and displayed the results exactly as I needed. On load it loads the answers to the first question as the selected record. I have noticed no side effects as of yet.

    Roby

    Thursday, August 20, 2009 9:44 AM
  • Theo, I doubt this syntax would be any less performant than binding to an EntityList on a DomainContext. Your concern about the performance of binding is valid, but I'd guess the point at which performance starts to degrade is application dependent. I'd hope that most people will find data binding more convenient than detrimental. However, it's certainly possible that a view model would provide more control. For instance, the scenario we're discussing might be optimized by performing a local search before loading new answers. Optionally, pending loads could be cancelled (op.Cancel) anytime a new load is started to minimize churn on the client.

    Kyle

    Thursday, August 20, 2009 11:41 AM
  • Theo, I doubt this syntax would be any less performant than binding to an EntityList on a DomainContext.

    I was more concerned about potential side effects in this scenario:

    1) grid is bound to entities of a domain context
    2) code triggers async load
    3) code triggers another async load
    4) first async load triggers callback and updates the entities bound to the grid
    5) second load triggers callback and updates the entities bouind to the grid

    The client doesn't queue server requests, so either 5) or 4) will happen first. My concern: is it possible that 5) will happen when 4) is working. But I guess the UI-thread is not reentrant and a new callback will only happen when the UI has gained control, so either when 4) or 5) completely finishes.

    Thursday, August 20, 2009 1:11 PM
  • Yeah, I got to that point to. Even if 5) happens before 4), the contents of the list will be correct. Also, the framework handles callbacks 1 at a time so 4) and 5) won't be happening in parallel. If you're worried about performance, the following pattern might be better.

    if (_currentLoadOperation.CanCancel)
    {
      _currentLoadOperation.Cancel();
    }
    _currentLoadOperation = context.Load(...);
    list.ItemsSource = _currentLoadOperation.Entities;
    Kyle
    Thursday, August 20, 2009 8:58 PM