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

Binding with array

5 Answers 722 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Simon
Top achievements
Rank 1
Simon asked on 20 Oct 2010, 04:53 PM
Hi,
I have an ObservableCollection of custom object I want to bind to a gridview.
The custom object is a list of Double values I want to display each value in each column of the gridview.

Unfortunately, I dont know how many double values are in the custom control at compile time: I'll know this information only at runtime, so the custom object is a List<double> object. Thus I have to bind an ObservableCollection<List<double>> to the grid view, and have each row display the respective array of double.

Is this possible with -telerik RadGridView component?
Could anyone provide example code, or instructions?
If I try to bind, I get one row for each double List, but only 2 colums, one showing the number of elements contained in the double List, the other shows another List-related information (not remember which one...).

Maybe I have to build a custom class which holds the double List, and implements some kind of interface to provide data to put into the columns??

Thanks for the help,
Simon

5 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 22 Oct 2010, 11:26 AM
Hi Simon,

I have prepare a very simple example for you that shows how to create an ObservableCollection<List<double>> and use it as RadGridView's ItemSource. The second issue occured because you should create your columns in code behind and this approach is shown also in the attached project.

Please see the sample and if you need any further assistance please let me know.

Greetings,
Vanya Pavlova
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Simon
Top achievements
Rank 1
answered on 25 Oct 2010, 12:47 PM
Hi Vanya , thanks for your example.
I modified the code, in order to bind to a List of custom classes, where each class has an array field of undefined length, to which elements I want to bind the columns, and I got it working using:

class MyCustomClass {
...
double[ ] MyClassArray;
...
}
...
List<MyCustomClass> MyListOfClasses;
...

NewCol = new GridViewDataColumn()
                    {
                        DataMemberBinding = new Binding("MyClassArray[" + i + "]"),
                        DataType = typeof(double),
                        DataFormatString = "{0:0.00}",                        
                    };

G.Columns.Add(NewCol);
MyGridView.ItemsSource = MyListOfClasses;
...

In addition, I need to bind the color, and/or any other visual property , of the cell to the value of MyClassArray[ i ].
I mean, if MyClassArray[ i ] < 0, then Cell-Font-Color = Red, otherwise Cell-Font-Color = Green

Is it possible to configure such a binding?
Also I can add an array to MyCustomClass that holds the respective colors and bind it to the visual propertry, but how to configure the binding?

Simon.
0
Vanya Pavlova
Telerik team
answered on 26 Oct 2010, 08:01 AM
Hi Simon,


You may use a StyleSelectors or a Converter that will return the approproate colour based on your custom logic , please see the following Conditional Styles and Templates with RadGridView for WPF and Silverlight and the following one forum thread regarding Converters and Arrays.

Please let me know if you need any additional information.

Kind regards,
Vanya Pavlova
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Simon
Top achievements
Rank 1
answered on 11 Nov 2010, 02:16 PM
Hi Vanya,
thanks for your suggestion.
I just found some spare time to test this functionality. Using CellStyleSelectors seems to work, but there is a problem when binding to a collection of collection. I mean: I bound the itemsource of the gridview to a collection of MyClass; MyClass has a field that is a collection itself (I'll know the length at runtime) and I want to bind each element of it to a column.

Here is some code:
...
public class SingleResult
{
   string Name;
   double Value;
   bool IsOK;
}

 public class ResultsGroup :
    {                         
        // ... some other properties
        public ObservableCollection<SingleResult> Results {  get;  set; }             
     }
...
 public ObservableCollection<ResultsGroup> ResultsList;

// Creating grid columns and bindings

foreach (string N in ColumnNames)
                {
                    TempCol = new GridViewDataColumn()
                    {                       
                        DataMemberBinding = new Binding("Results["+ i + "]"),

                        CellStyleSelector = new CellStyleSelector(),
                    };                    
                    TempCol.IsReadOnly = true;
                    G.Columns.Add(TempCol);
                    i++;
                }
                Grid.AutoGenerateColumns = false;                
                Grid.ItemsSource = ResultsList;
            }

Now each column is bound to a each ResultsGroup.Results element. I want that the cell element style is changed if the element has some specific value, so I created CellStyleSelector.

 public override Style SelectStyle(object item, DependencyObject container)
        {
            var obj = (SingleResult)item;

            var S = new Style(typeof(GridViewCell));
            if (obj.IsOK)
            {
                S.Setters.Add(
                    new Setter(GridViewCell.BackgroundProperty,
                        new SolidColorBrush(Colors.Green)));
            }
            else
            {
                S.Setters.Add(
                        new Setter(GridViewCell.BackgroundProperty,
                            new SolidColorBrush(Colors.Red)));
            }
            return S;
        }

The problem is that the SelectStyle of CellStyleSelector il called passing a ResultGroup object as Item, not a SingleResult object! So the conferter fails. If I do the cast on Item as a ResultsGroup object and make some decision for the background, the new style is applied to all the columns!

Really don't know how to proceed... :-(
0
Milan
Telerik team
answered on 17 Nov 2010, 09:45 PM
Hello Simon,

You can access the value of each cell by using the container parameter that is passed to the SelectStyle method:

public class CustomCellStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var cell = container as GridViewCell;
        var cellValue = cell.Value;
  
        return base.SelectStyle(item, container);
    }
}


All the best,
Milan
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
GridView
Asked by
Simon
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Simon
Top achievements
Rank 1
Milan
Telerik team
Share this question
or