Showing posts with label DevExpress. Show all posts
Showing posts with label DevExpress. Show all posts

WPF: AlternateRowColor style in devexpress GridControl

Here I am demonstrating how can we assign alternate Row color to a devexpress grid control. To do so we will have to use MultiDataTrigger. In the MultiDataTrigger, we will check that if the row is even and not selected then Even row color will be assigned, if the row is odd and not selected than odd row color will be applied. similarly if the row is selected than no color will be applied and default selection state will be applied.

XAML Code

Set selected row index in DevExpress Grid Control by C# code

In DevExpress grid control there is no direct property to set selected row index from C# code if you need to do so. To achieve this functionality one needs to define TableView for the DevExpress grid control. The TableView is defined under <dxg:GridControl.View></dxg:GridControl.View> tag.

To add DevExpress Grid you will require
xmlns:dxg='http://schemas.devexpress.com/winfx/2008/xaml/grid'

In the TableView there is a property called "FocusedRowHandle" which is used to set the selected row index in the GridControl

Consider Sample XAML Written Below
<StackPanel>
   <dxg:GridControl Height='300' Grid.Column='0' Grid.Row='1' x:Name='grdDetail' IsFilterEnabled='False' IsManipulationEnabled='False'> <dxg:GridControl.Columns> <dxg:GridColumn Header='SeqNo' AllowResizing='False' Width='30'  FieldName='[SeqNo]' HorizontalHeaderContentAlignment='Center' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                   <DataTemplate>                     <TextBlock HorizontalAlignment='Left' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[SeqNo]}'></TextBlock>                   </DataTemplate>               </dxg:GridColumn.CellTemplate>                             </dxg:GridColumn>          <dxg:GridColumn Header='RollNo' FieldName='[RollNo]' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                   <DataTemplate>                    <TextBlock HorizontalAlignment='Right' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[RollNo]}'></TextBlock>                   </DataTemplate>              </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>          <dxg:GridColumn Header='Name' FieldName='[Name]' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                   <DataTemplate>                    <TextBlock HorizontalAlignment='Center' VerticalAlignment='Center' Text='{Binding Path=RowData.Row.[Name]}'></TextBlock>                   </DataTemplate>               </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>          <dxg:GridColumn Header='Marks' FieldName='[Marks]' Width='50'  HorizontalHeaderContentAlignment='Center' AllowSorting='False'>                <dxg:GridColumn.CellTemplate>                  <DataTemplate>                    <TextBlock HorizontalAlignment='Right' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[Marks]}'></TextBlock>                   </DataTemplate>                </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>          <dxg:GridColumn Header='Rank' FieldName='[Rank]' Width='50'  HorizontalHeaderContentAlignment='Center' AllowSorting='False'>               <dxg:GridColumn.CellTemplate>                 <DataTemplate>                 <TextBlock HorizontalAlignment='Right' VerticalAlignment='Center'  Text='{Binding Path=RowData.Row.[Rank]}'></TextBlock>                   </DataTemplate>                </dxg:GridColumn.CellTemplate>          </dxg:GridColumn>        </dxg:GridControl.Columns>        <dxg:GridControl.View>        <dxg:TableView Name='tblViewDetail' AllowColumnFiltering='False'  AllowEditing='False' AllowGrouping='True' AllowResizing='True'  AllowSorting='True' AutoWidth='True' ShowGroupPanel='False'  ShowIndicator='False' ShowVerticalLines='False'>         </dxg:TableView>        </dxg:GridControl.View>    </dxg:GridControl>    <StackPanel Orientation="Horizontal">        <Button Content="Select 1st Row" Click="Button_Click" x:Name="Sel1"></Button>        <Button Content="Select 2nd Row" x:Name="Sel2" Click="Sel2_Click"></Button>        <Button Content="Select 3rd Row"  x:Name="Sel3" Click="Sel3_Click"></Button>   </StackPanel> </StackPanel>

Now In this XAML we will add ItemSources by C# Code as well as selected row will be set by c# code. The C# Code will be as followed