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

CellStyleSelector with AutoGenerateColumns

1 Answer 41 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 09 Jun 2011, 08:56 AM
I am using your DataTable construct to service a dynamic structure requirement, basically a pivot table with variable columns. This works well until the client want a visual cue when there are zero values.

I have 2 problems, the first is obvious, where do I assign the CellStyleSelector when using auto generate columns?

The second is how to get the value of the cell in the style selector code. When I inspect the container object there is a value property but there is no obvious way to get at the value.

public class ZeroStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        Style oStyle = ZeroStyle;
        bool bOk;
        int iValue;
        object oValue = container.GetValue(); //<<<<<<<< this fails
        if (int.TryParse(oValue.ToString(), out iValue))
        {
            if (iValue != 0)
            {
                oStyle = NonZeroStyle;
            }
        }
         
        return oStyle;
    }
    public Style ZeroStyle { get; set; }
    public Style NonZeroStyle { get; set; }
}

1 Answer, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 09 Jun 2011, 12:05 PM
Hello Mark,

 You need to handle the AutoGeneratingColumn event of RadGridView, which gives you access to the auto-generated column, allowing you to set the CellStyleSelector property.

 The GetValue method belongs to the DependencyObject class and won't help you get the cell value. You'll need to cast the container to GridViewCell and use the GetValueForItem method from the cell's column like so:
object value = null;
var cell = container as GridViewCell;
if (cell.DataColumn != null)
{
    value = cell.DataColumn.GetValueForItem(item);
}


All the best,
Yavor Georgiev
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
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Share this question
or