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

WPF HyperLink Button using Style and Control Template




WPF: HyperLink Button using Style and Control Template

Using this post, you will be able to create a button, which will look like Hyperlink.
To achieve this you need to create a style for a button and in that style you need to modify the template of the button so that it looks like hyperlink.

Also, mostly we see that in hyperlink, the color changes whenever the mouse hovers on it. So using Control Template triggers, we will change the foreground color of the hyperlink.

Here is the Hyperlink button style which you need to define in the Resources section of XAML.


WPF: Image Button using Control Template



WPF: Image Button using Control Template

In WPF, for better UI representation we mostly use images in our application.
So most common place where we use images is in place of buttons.
Mostly we represent action buttons such as add,delete etc with some image which represents add or delete functionality.

Here we have taken an example of Add button and we have used Control Template for modifying the button to show as image instead of normal button.


WPF: Specify Combo Box ItemsSource from XAML

Use of x:Array in XAML
To define itemsource from XAML using x:Array, we need to add the following namespaces.
xmlns:sys="clr-namespace:System;assembly=mscorlib"

We can create an array of string, or any other simple data type in Resources section.
We have created an array of string in the following example:
<x:Array x:Key="StringArrayResource" Type="sys:String">
<sys:String>Combo Value1</sys:String>
<sys:String>Combo Value2</sys:String>
<sys:String>Combo Value3</sys:String>
</x:Array>


In the above example we can see that Type is specified as sys:String where 'sys' is the System NameSpace from mscorlib assembly.
Note we have given key "StringArrayResource" for the string array.
Now the main part, Binding Combo Box ItemsSource with the resource defined.
<ComboBox x:Name="comboBox" ItemsSource="{StaticResource StringArrayResource}" Height="30" Width="90">          
</ComboBox>


The Combo Box will look like this.


WPF: Clear SelectedValue of Combo box on delete button press and Handling Other keys and Cut, Copy, Paste Events

You can use following code to Delete the SelectedValue of combobox on delete  button press event with preventing other key inputs. You can also handle cut copy and paste event on the combobox by using below code.