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

Set Cellstyle based on Datagrid row and column index

2 Answers 1009 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 1
Stefan asked on 07 Dec 2017, 03:06 PM

Hey guys,

since i dont get it to work since 3 hours now i registered here in the hope you have a solution.

I have a GridView in my View:

<tel:RadGridView ItemsSource="{Binding CompareTablesResultView, Mode=OneWay}" ColumnWidth="Auto" x:Name="grdComparedData"
                         IsFilteringAllowed="False" CanUserReorderColumns="False" CanUserFreezeColumns="False"
                         CanUserSortColumns="False" CanUserDeleteRows="False" CanUserInsertRows="False" ShowGroupPanel="False"
                         tel:StyleManager.Theme="Windows7" DataLoaded="grdComparedData_DataLoaded">
        </tel:RadGridView>
        <tel:RadDataPager x:Name="DataPager" Source="{Binding Items, ElementName=grdComparedData}"
                          PageSize="30"  Grid.Row="1" tel:StyleManager.Theme="Windows7"/>

The GridView Itemssource is bound to a DataView Property in my ViewModel.

The DataView is simply the .DefaultView of the Datatable i want to display.

With the data in my datatable i did some validation. In my validation i receive information which cells are invalid (rownumber and columnnumber/columnname).

Now my target is to set the style for the cells that are invalid (foreground color to red). But i cant get it working to get the GridViewCell based on the infos if have.

In some forum posts i saw code like MyGridView.Rows(index).Cells(anotherIndex) but the RadGridView dont have an accessable Row property for me.

I also tried some other things to get the cell like to get access the .Items Property or some attempts with the .ChildrenOfType<> method but nothing worked for me.

Is there any way to set the Cell-Style for a specific cell when bound to a DataTable or DataView? I cant use a cellstyle selector cause its not the value of the cell itself that is invalid, its only invalid in the context of row and column. The same value can be valid in another row or in another column.

You guys have any ideas?

Could i mark the DataView Cell in any way that i could react from a cellstyle selector on it or something like that?

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 12 Dec 2017, 10:27 AM
Hello Stefan,

I believe you should be able to achieve the desired result by using a cell style selector. You can get both the column and the row of the current cell by casting the container in the SelectStyle method to GridViewCell. Here's what I have in mind:

public override Style SelectStyle(object item, DependencyObject container)
{
    var row = item as DataRowView;
    var cell = container as GridViewCell;
    if (cell.Column.DisplayIndex == 0 && cell.Column.DataControl.Items.IndexOf(item) == 2) // you can change this with your custom logic
    {
        return this.StyleOne;
    }
 
    return base.SelectStyle(item, container);
}

Please let me know whether such an approach would work for you. If that is not the case, please share some more details on your exact requirement so that I can better assist you.

Regards,
Dilyan Traykov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Stefan
Top achievements
Rank 1
answered on 18 Dec 2017, 11:29 AM
That was exacly that what i was looking for. Thank you very much!
Tags
GridView
Asked by
Stefan
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Stefan
Top achievements
Rank 1
Share this question
or