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



C# Code
public partial class MainWindow : Window
{
  DataTable dt;
  public MainWindow()
  {  
    InitializeComponent();
    dt = new DataTable();
LoadTable();
grdDetail.ItemsSource = dt;
  }


  public void LoadTable()
{
dt.Columns.Add("SeqNo");
dt.Columns.Add("RollNo");
dt.Columns.Add("Name");
dt.Columns.Add("Marks");
dt.Columns.Add("Rank");
     DataRow dr = dt.NewRow();
dr["SeqNo"] = "1";
    dr["RollNo"]= "789159";
dr["Name"] = "Adam";
dr["Marks"] = "785";
dr["Rank"] = "6";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["SeqNo"] = "2";
dr["RollNo"] = "789209";
dr["Name"] = "Shaun";
dr["Marks"] = "845";
dr["Rank"] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["SeqNo"] = "3";
dr["RollNo"] = "789205";
dr["Name"] = "Shane";
    dr["Marks"] = "844";
dr["Rank"] = "3";
dt.Rows.Add(dr);
  }
  private void Button_Click(object sender, RoutedEventArgs e)
    {
      tblViewDetail.FocusedRowHandle = 0; //Selecting 1st Row
    }
  private void Sel2_Click(object sender, RoutedEventArgs e)
    {
      tblViewDetail.FocusedRowHandle = 1; //Selecting 2nd Row
    }
  private void Sel3_Click(object sender, RoutedEventArgs e)
   {
      tblViewDetail.FocusedRowHandle= 2; //Selecting 3rd Row
  }
}

Output



1 comment:

  1. Hub Of Computer Tricks And Programming: Set Selected Row Index In Devexpress Grid Control By C Code >>>>> Download Now

    >>>>> Download Full

    Hub Of Computer Tricks And Programming: Set Selected Row Index In Devexpress Grid Control By C Code >>>>> Download LINK

    >>>>> Download Now

    Hub Of Computer Tricks And Programming: Set Selected Row Index In Devexpress Grid Control By C Code >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete