how can i make a new row for the radgridview by pressing the Tab key if the cursor is at the last cell of the grid(last row, last column) instead of pressing the Insert key?
i try to look at the document or the Search function in the forum but only see sample for the WinForm
thank you
9 Answers, 1 is accepted
You may handle the KeyDown event of the grid as follows:
private void playersGrid_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
var grid = sender as RadGridView;
Player currentItem = grid.CurrentItem as Player;
if ((currentItem != null))
{
grid.BeginInsert();
grid.CurrentColumn = grid.Columns[0];
}
else
{
e.Handled = true;
}
}
}
Sincerely yours,
Maya
the Telerik team
the sample code doesn't work well.
1. if my page only have one GridView(No Other UI controls such as TextBox), then pressing tab at the last cell still keeps the tab at that last cell. So no new row will appear
2. if i have other UI control and the GridView, then the tab works but the code becomes very buggy. The validation of the cell always fires whenever i press tab. And press Insert will make the new row appear at top , not last cell. Sometimes I will have the error which tells that the operation is in process during press tab at the last cell.
plz help
thank you
In order to provide you with an appropriate solution, I would need more details about your project and its settings. How exactly are you defining your grid in the first and second scenarios?
I am sending you the sample project illustrating the solution proposed above, so that you ca use it as a reference. You may feel free to change it in the way you want and share more information if there is any misunderstandings according to your requitements.
Maya
the Telerik team
I looked at your project and saw there weren't much different with mine excepting some more properties for the gridview. Yours is working but mine isn't. Not sure why.
this is the first case code. Pressing tab and it just stays at the last cells.(the second case, the code is more complex. If i know what wrong with the first case, i will try it with the second case before asking you again). Thank you for helping
<
Grid
x:Name
=
"LayoutRoot"
Background
=
"White"
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
=
"125*"
/>
<
RowDefinition
Height
=
"53*"
/>
</
Grid.RowDefinitions
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"254*"
/>
<
ColumnDefinition
Width
=
"64*"
/>
</
Grid.ColumnDefinitions
>
<
riaControls:DomainDataSource
AutoLoad
=
"True"
d:DesignData
=
"{d:DesignInstance my:Company, CreateList=true}"
Height
=
"0"
LoadedData
=
"companyDomainDataSource_LoadedData"
Name
=
"companyDomainDataSource"
QueryName
=
"GetCompaniesQuery"
Width
=
"0"
Margin
=
"0,0,254,125"
>
<
riaControls:DomainDataSource.DomainContext
>
<
my1:CompanyContext
/>
</
riaControls:DomainDataSource.DomainContext
>
</
riaControls:DomainDataSource
>
<
telerik:RadGridView
KeyDown
=
"companyRadGridView_KeyDown"
ItemsSource
=
"{Binding ElementName=companyDomainDataSource, Path=Data}"
Name
=
"companyRadGridView"
AutoGenerateColumns
=
"False"
ShowGroupPanel
=
"False"
DataLoadMode
=
"Asynchronous"
RowEditEnded
=
"companyRadGridView_RowEditEnded"
AddingNewDataItem
=
"companyRadGridView_AddingNewDataItem"
IsSynchronizedWithCurrentItem
=
"false"
Deleted
=
"companyRadGridView_Deleted"
Height
=
"125"
VerticalAlignment
=
"Top"
Grid.ColumnSpan
=
"2"
>
<
telerik:RadGridView.Columns
>
<
telerik:GridViewDataColumn
Header
=
"Company Code"
DataMemberBinding
=
"{Binding CompanyCode}"
/>
<
telerik:GridViewDataColumn
Header
=
"Company Name"
DataMemberBinding
=
"{Binding CompanyName}"
/>
</
telerik:RadGridView.Columns
>
</
telerik:RadGridView
>
<
telerik:RadButton
Content
=
"Setup"
Margin
=
"14,15,0,0"
Name
=
"setBtn"
Click
=
"ModalCompanySetup_Click"
Grid.Column
=
"1"
Height
=
"26"
VerticalAlignment
=
"Top"
HorizontalAlignment
=
"Left"
Width
=
"54"
Grid.Row
=
"1"
/>
</
Grid
>
This is my code behind.
public
partial
class
CompanyGridView : RadWindow
{
public
CompanyGridView()
{
InitializeComponent();
}
private
void
companyDomainDataSource_LoadedData(
object
sender, LoadedDataEventArgs e)
{
if
(e.HasError)
{
System.Windows.MessageBox.Show(e.Error.ToString(),
"Load Error"
, System.Windows.MessageBoxButton.OK);
e.MarkErrorAsHandled();
}
}
private
void
ModalCompanySetup_Click(
object
sender, RoutedEventArgs e)
{
CompanySetupWindow companySetupWindow =
new
CompanySetupWindow(
this
.companyRadGridView.SelectedItem, (
this
.companyDomainDataSource.DomainContext
as
CompanyContext));
companySetupWindow.WindowStartupLocation = Telerik.Windows.Controls.WindowStartupLocation.CenterOwner;
companySetupWindow.ShowDialog();
}
private
void
companyRadGridView_RowEditEnded(
object
sender, GridViewRowEditEndedEventArgs e)
{
this
.companyDomainDataSource.DomainContext.SubmitChanges();
}
private
void
companyRadGridView_Deleted(
object
sender, GridViewDeletedEventArgs e)
{
this
.companyDomainDataSource.DomainContext.SubmitChanges();
}
private
void
companyRadGridView_AddingNewDataItem(
object
sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
{
this
.companyRadGridView.CurrentColumn =
this
.companyRadGridView.Columns[0];
}
private
void
companyRadGridView_KeyDown(
object
sender, KeyEventArgs e)
{
if
(e.Key == Key.Tab)
{
var grid = sender
as
RadGridView;
Company currentItem = grid.CurrentItem
as
Company;
if
((currentItem !=
null
))
{
grid.BeginInsert();
grid.CurrentColumn = grid.Columns[0];
}
else
{
e.Handled =
true
;
}
}
}
Basically, In order to insert a new item in the grid, by using BeginInsert() method or by setting ShowInsertRow to "True", you need to have a default constructor in the definition of the class filling up the data of the grid.
Furthermore, as you are using RIA Services and DomainDataSource, the add/delete functionality may be not available depending on your data source you are using. You may take a look at this forum thread for more information on the way to overcome that and implement the desired features.
Maya
the Telerik team
I saw that thread a long time ago . However, the sample project of that post won't have the built-in feature "sorting, grouping,.." of the radgridview(if i click on one of the header column of the gridview, I will get an error) . I want to have those features. But not sure how to implement it to work with your radgridview so i decide to use the DomainDatasource with the radgridview which will take advantage of the built-in sorting, grouping,...
can you show me a sample which use domaindatasource + entity framework + enabling tab key in order to insert a new row?
thank you
You can apply the logic for handling the KeyDown event in the sample project attached in the forum thread mentioned above. The only thing you need to implement in this application is the ICollection CopyTo() method:
/// <
summary
>Removes all items from the <
see
cref
=
"T:System.Collections.Generic.ICollection`1"
/>.
/// </
summary
>
/// <
exception
cref
=
"T:System.NotSupportedException"
>The <
see
cref
=
"T:System.Collections.Generic.ICollection`1"
/>
/// is read-only. </
exception
>
void ICollection<
TEntity
>.CopyTo(TEntity[] array, int arrayIndex)
{
this.entities.ToArray().CopyTo(array, arrayIndex);
}
It is in the EntityCollectioViewBase<TEntity> class. Thus you will be able to add and remove items as well as to sort the grid.
All the best,
Maya
the Telerik team
EDIT: Actually I guess I need to use that CommandOverride ( I for get the name) because you take control of the tab key to move to the next cell.
Indeed, that would be the expected behavior for handling Tab key as RadGridView performs its own navigation logic for it. In case you want to predefine it, you can create one of your own by developing a custom keyboard command provider. Please refer to our online documentation and blog posts for a reference.
Is that the behavior you are looking for ?
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>