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

Add new row and set focus to second cell on gridview keyboard focus

4 Answers 332 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 21 Mar 2016, 06:08 PM

Hello,

I have the following requirements:

  • When the user enters from outside the grid into the empty grid a new row should be created and the second cell should be selected in edit mode.
  • When the user enters from outside the grid into a grid with existing rows, the second cell should be selected in edit mode.

I am using the enter-key to move to the next field in a particular viewmodel, instead of the tab-key.

How can I accomplish this, I tried searching a solution but couldnt find anything that accomplish this when entering the grid from outside?

 

Any help would be greatly appreciated.

Marcel

4 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 24 Mar 2016, 02:48 PM
Hello Marcel,

I will try to address both scenarios you have described.

First of all, you should check if the respective GridView has any items.

1) If the GridView is empty, you can call RadGridView's BeginInsert() method and then set the CurrentCell to the cell in the second column:

if (this.RadGridView.Items.Count == 0)
{
    this.RadGridView.BeginInsert();
    this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
}

2) If there are items in the GridView, you can select the second cell of the last item, for example, and call RadGridView's BeginEdit() method:

else
{
    RadGridView.Items.MoveCurrentToLast();
    this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
    this.RadGridView.BeginEdit();
}

It is up to you to pick the event on which you will trigger the respective operation. If you're navigating with the Enter key, for example, you can handle the window's PreviewKeyUp event like so:

public MainWindow()
{
    InitializeComponent();
    this.PreviewKeyUp += MainWindow_PreviewKeyUp;
}
 
void MainWindow_PreviewKeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter && FocusManager.GetFocusedElement(this) == this.RadGridView)
    {
        HandleNavigation();
    }
}
 
void HandleNavigation()
{
    if (this.RadGridView.Items.Count == 0)
    {
        this.RadGridView.BeginInsert();
        this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
    }
    else
    {
        RadGridView.Items.MoveCurrentToLast();
        this.RadGridView.CurrentCellInfo = new GridViewCellInfo(RadGridView.CurrentItem, RadGridView.Columns[1]);
 
        if (this.RadGridView.CurrentCell.IsInEditMode)
        {
            this.RadGridView.CommitEdit();
        }
 
        this.RadGridView.BeginEdit();
    }
}

I'm attaching a sample project to better illustrate this. Please let me know if this approach would work for you.

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Marcel
Top achievements
Rank 1
answered on 22 Apr 2016, 12:20 PM

Hello Dilyan,

Thanks for the support, had to alter the example code a little bit but now its working!

Regards,

Marcel

 

 

0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 15 Jul 2019, 12:15 PM

hii i have added a radmulticolumcombobox as gridviewdatacolum cell template in RadGridView...but when i am trying to focus cell using Tab or enter key it does not focus directly on that multicombobox column.....how to solve this...could you please give me a solution for this ?

 

Regards,

  <telerik:GridViewDataColumn Width="300"  IsReadOnly="True"  IsFilterable="False" IsGroupable="False" DataMemberBinding="{Binding ourItemCode, Mode=TwoWay}" CellStyle="{StaticResource GridViewCellStyle}" UniqueName="ItemCode" Header="Item Code">
 
    <telerik:GridViewDataColumn.CellTemplate>
 
      <telerik:RadMultiColumnComboBox telerik:StyleManager.Theme="Summer"  BorderThickness="0" Width="Auto"  Height="Auto"  AutoCompleteMode="SuggestAppend" Tag="{Binding product_Id,Source={StaticResource StatusList}}"  
    SelectedValuePath="product_Id"  DropDownWidth="400" DisplayMemberPath="product_code"
   SelectedValue="{Binding TempItemId,Mode=TwoWay}"   CloseDropDownAfterSelectionInput="True"
      OpenDropDownOnFocus="True" SelectionChanged="RadMultiColumnComboBox_SelectionChanged" KeyDown="RadMultiColumnComboBox_KeyDown" GotFocus="RadMultiColumnComboBox_GotFocus" LostFocus="RadMultiColumnComboBox_LostFocus_1">
<telerik:RadMultiColumnComboBox.ItemsSourceProvider>
    <telerik:GridViewItemsSourceProvider ItemsSource="{StaticResource ItemCodeList}">
 
        </telerik:GridViewItemsSourceProvider>
 
                                            
     </telerik:RadMultiColumnComboBox.ItemsSourceProvider>
                                         
    </telerik:RadMultiColumnComboBox>
                                        
 </DataTemplate>
                                   
  </telerik:GridViewDataColumn.CellTemplate>
                                
 </telerik:GridViewDataColumn>

Ranees Ras

0
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 15 Jul 2019, 12:18 PM

hii i have added a radmulticolumcombobox as gridviewdatacolum cell template in RadGridView...but when i am trying to focus cell using Tab or enter key it does not focus directly on that multicombobox column.....how to solve this...could you please give me a solution for this ?

Regards,

Ranees Ras

 

<telerik:GridViewDataColumn Width="300"  IsReadOnly="True"  IsFilterable="False" IsGroupable="False" DataMemberBinding="{Binding ourItemCode, Mode=TwoWay}" CellStyle="{StaticResource GridViewCellStyle}" UniqueName="ItemCode" Header="Item Code">
  
    <telerik:GridViewDataColumn.CellTemplate>
  
      <telerik:RadMultiColumnComboBox telerik:StyleManager.Theme="Summer"  BorderThickness="0" Width="Auto"  Height="Auto"  AutoCompleteMode="SuggestAppend" Tag="{Binding product_Id,Source={StaticResource StatusList}}" 
    SelectedValuePath="product_Id"  DropDownWidth="400" DisplayMemberPath="product_code"
   SelectedValue="{Binding TempItemId,Mode=TwoWay}"   CloseDropDownAfterSelectionInput="True"
      OpenDropDownOnFocus="True" SelectionChanged="RadMultiColumnComboBox_SelectionChanged" KeyDown="RadMultiColumnComboBox_KeyDown" GotFocus="RadMultiColumnComboBox_GotFocus" LostFocus="RadMultiColumnComboBox_LostFocus_1">
<telerik:RadMultiColumnComboBox.ItemsSourceProvider>
    <telerik:GridViewItemsSourceProvider ItemsSource="{StaticResource ItemCodeList}">
  
        </telerik:GridViewItemsSourceProvider>
  
                                             
     </telerik:RadMultiColumnComboBox.ItemsSourceProvider>
                                          
    </telerik:RadMultiColumnComboBox>
                                         
 </DataTemplate>
                                    
  </telerik:GridViewDataColumn.CellTemplate>
                                 
 </telerik:GridViewDataColumn>
Tags
GridView
Asked by
Marcel
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Marcel
Top achievements
Rank 1
Ras Ran
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or