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

How to set a DataTemplate for a column that is bound to System.Double Property?

2 Answers 49 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kornelije
Top achievements
Rank 1
Kornelije asked on 14 Jun 2012, 04:55 PM
I have a very specific problem, so please try to understand the issues it relates to.

I am logging activities for users accross dates.
An activity can be authorized and has a related value.

Take a look at this table:

        Mark        Jack        Kirk
1/1/2012    12 (A)      11 (A)      
1/2/2012    18          15          4 (A)
1/3/2012    8 (A)       6 (A)       4
That means that on the the 3rd of January, Mark was active with a value of 8 and authorized, while Kirk was active with a value of 4, not authorized. On Jan 1, Kirk was not active at all. I hope the table makes sense.

The underlying data looks like this.

public class Activity  
{
    public String User { get; set; }
      
    public DateTime Time { get; set; } 
    public Double? Value { get; set; } 
        
    public Boolean IsAuthorized { get; set; } 
}

Therefore, there is an instance of the Activity for each Time-User pair.


Now, I am trying to show this data in a RadGridView with three specific and mandatory requirements:

1) The number of users must remain variable. In other words, I don't know exactly how many columns will I need at compile time.

2) I must be able to set a different background color for cells that are not authorized (IsAuthorized == false). I don't want to display (A) in results, that's only indicative for this example. I want the background color changed appropriately instead.

3) Data must be sortable by all columns, both by time and by any of the user column.sortable

I've tried to create a class that represents a Grid row, that has a dynamic number of values, something like this:

public class ActivitiesDataRow
{
    public DateTime TimeStamp { get; set; }
  
    public List<string> ColumnHeaders { get; set; }
    public List<double?> Values { get; set; }
}

Then I create rows from all the activities. I turn off the auto-generation of columns, and make them manually, setting them like this:

List<Activity> activities = new List<Activity>();
List<ActivitiesDataRow> activityRows = new List<ActivitiesDataRow>();
  
// ...
// ...
  
List<String> users = activities.Select(a => a.User).Distinct().ToList();
  
for (int i = 0; i < numberOfUsers; i++)
{
    GridViewDataColumn col = new GridViewDataColumn();
      
    // Get the names of the users, they are in the same order as 
    // their values in ActivitiesDataRow
    col.Header = userNames[i]; 
      
    col.DataType = typeof(ActivitiesDataRow);
    Binding bnd = new Binding("Values");
    bnd.Converter = new ActivityDataRowCollectionConverter();
    bnd.ConverterParameter = i;
    col.DataMemberBinding = bnd;
    gridView.Columns.Add(col);
}
  
public class ActivityDataRowCollectionConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Since "Values" property is bound, the value is the List<Double?>
        return (value as List<Double?>)[(int)parameter];
    }
}


How can I make it so that background color is set depending on the IsAuthorized value?


If it can't be done by using this approach (List<Double?> + ValueConverter), what changes would I have to do to be able to satisfy all three requirements?

(I've seen the Telerik's video example that shows how to style the cell by using a DataTemplate and StyleSelectors, but it was binding to a known double property in DataTemplate. I don't have a known property that represents the value, but a list of them.)

2 Answers, 1 is accepted

Sort by
0
Kornelije
Top achievements
Rank 1
answered on 14 Jun 2012, 04:57 PM

I have change my question during writing but forgot to update the title, so it may be misleading. Unfortunately, I am unable to edit the post to change the title. My apologies.

0
Nedyalko Nikolov
Telerik team
answered on 18 Jun 2012, 02:33 PM
Hi,

Am I missing something? You want to change the background of a double value cell depending on "IsAuthorized" property not the double property. So I think that you can do that with a StyleSelector. Keep in mind that you have a reference to the whole object not only value of the cell, so you can use any property of your data object. You can take a look at this online help topic as well.
Let me know if this does not help.

Regards,
Nedyalko Nikolov
the Telerik team

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

Tags
GridView
Asked by
Kornelije
Top achievements
Rank 1
Answers by
Kornelije
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or