This is a migrated thread and some comments may be shown as answers.

Data Loaded Twice?

2 Answers 63 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rob
Top achievements
Rank 1
Rob asked on 30 May 2012, 03:18 PM
Hello,

I have some problems with my application and created a simple demo project to show the behavior.  As far as i can tell, this was behaving differently before we upgraded to "2012.1.326.1040".

A simple screen with a datagrid and three buttons - build columns, load data, and export data.  Add a breakpoint in the SearchResultItem converter, click Build Columns, and then LoadData... Notice that the converter runs twice - once with the proper converterparameter and once with null... 

Then try exporting the data and notice that the converter reruns multiple times again and obviously fails miserably because the values and parameters are all wrong.

Did something change?  Am I doing something incorrectly?  Why do the converters run twice on initial load? 
<UserControl x:Class="SilverlightApplication19.MainPage"
>
 
  <Grid x:Name="LayoutRoot" Background="White">
 
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
         
    <Button Grid.Row="0" Content="Build Columns" Click="BuildColumns" />
    <Button Grid.Row="1" Content="Load Data" Click="LoadData" />
    <Button Grid.Row="2" Content="Export" Click="ExportData" />
    <telerik:RadGridView x:Name="rgv" Grid.Row="3" AutoGenerateColumns="False" ItemsSource="{Binding list}" />
         
  </Grid>
     
</UserControl>




using System;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;
 
namespace SilverlightApplication19
{
  public partial class MainPage : UserControl
  {
 
    ViewModel vm = new ViewModel();
 
    public MainPage()
    {
      InitializeComponent();
    }
 
    private void BuildColumns(object sender, RoutedEventArgs e)
    {
             
      rgv.Columns.Clear();
 
      var gvdc1 = new GridViewDataColumn()
      {
        IsReadOnly = true,
        Header = "id column",
        Width = new GridViewLength(100),
        DataMemberBinding = new Binding()
        {
          Converter = new SearchResultItem(),
          ConverterParameter = "id",
          Mode = BindingMode.OneWay
        },
        DataType = typeof(string)
      };
      rgv.Columns.Add(gvdc1);
 
      var gvdc2 = new GridViewDataColumn()
      {
        IsReadOnly = true,
        Header = "name column",
        Width = new GridViewLength(100),
        DataMemberBinding = new Binding()
        {
          Converter = new SearchResultItem(),
          ConverterParameter = "name",
          Mode = BindingMode.OneWay
        },
        DataType = typeof(string)
      };
      rgv.Columns.Add(gvdc2);
 
    }
 
    private void LoadData(object sender, RoutedEventArgs e)
    {
             
      vm.list = new System.Collections.ObjectModel.ObservableCollection<DataObject>()
      {
        new DataObject() { id = 1, name = "a" },
      };
 
      this.DataContext = vm;
   
    }
 
    private void ExportData(object sender, RoutedEventArgs e)
    {
      SaveFileDialog dialog = new SaveFileDialog()
      {
        DefaultExt = "xls",
        Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "xls", "Excel"),
        FilterIndex = 1
      };
      if (dialog.ShowDialog() == true)
      {
        using (Stream stream = dialog.OpenFile())
        {
          rgv.Export(stream,
          new GridViewExportOptions() 
          {
            Format = ExportFormat.ExcelML,
            ShowColumnHeaders = true,
          });
        }
      }
    }
 
  }
 
 
  public class SearchResultItem : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
 
      if (parameter == null)
        return "parameter was null";
      else
        if (parameter.ToString() == "id")
          return (value as DataObject).id;
        else if (parameter.ToString() == "name")
          return (value as DataObject).name;
        else
          return "wtf";
      }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return DependencyProperty.UnsetValue;
    }
}
 
public class ViewModel : Notify
{
 
  private ObservableCollection<DataObject> _list = new ObservableCollection<DataObject>();
  public ObservableCollection<DataObject> list
  {
    get
    { return _list; }
    set
    { _list = value; RaisePropertyChanged("list"); }
  }
 
}
 
public class DataObject : Notify
{
  private int _id = 0;
  public int id
  {
    get
    { return _id; }
    set
    { _id = value; RaisePropertyChanged("id"); }
  }
 
  private string _name = null;
  public string name
  {
    get
    { return _name; }
    set
    { _name = value; RaisePropertyChanged("name"); }
  }
 
}
 
public class Notify : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
 
  protected void RaisePropertyChanged(string propertyName)
  {
    PropertyChangedEventHandler propertyChanged = PropertyChanged;
    if ((propertyChanged != null))
    {
      propertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }
 
}
 
}
asdasd

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 01 Jun 2012, 01:51 PM
Hi,

Thank you for the detailed explanation.

 If you specify a path for the Binding then the converter will be applied only once. Furthermore the data will be exported fine.
For example:

DataMemberBinding = new Binding("id")
       {
         Converter = new SearchResultItem(),
         ConverterParameter = "id",
         Mode = BindingMode.OneWay
       }

May I ask you what is the previous version that you had?

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Rob
Top achievements
Rank 1
answered on 06 Jun 2012, 12:22 PM
OK.  I'll see if I can fit the path into my scenario.  I think the previous version was 2011.3.1129.1040.
Tags
GridView
Asked by
Rob
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Rob
Top achievements
Rank 1
Share this question
or