I have a Grid with a number of read only columns and two editable columns and would like the ENTER key and TAB key to behave the same when editing data..
When I am editing a value in a cell and press TAB, focus skips over any read only columns in the row and moves to the next editable column. When I TAB off that, focus moves to the first editable column of the next row. This is desired behaviour.
When I am editing a cell and press ENTER though, focus moves to the next column whether it is read only or not. I have set EnterKeyMode to NextCell.
Is there any way to simulate the tab behaviour when the Enter Key is pressed?
Thanks,
Mark.
14 Answers, 1 is accepted
Thank you for writing.
To achieve the desired functionality, you should create a custom grid behavior in the following way:
class
MyBehavior : BaseGridBehavior
{
public
override
bool
ProcessKeyDown(KeyEventArgs keys)
{
if
(keys.KeyData == Keys.Enter &&
this
.GridControl.IsInEditMode)
{
this
.GridControl.GridNavigator.SelectNextColumn();
}
return
true
;
}
}
Then, simply assign this custom behavior to your grid:
radGridView1.GridBehavior =
new
MyBehavior();
I hope this helps.
Greetings,
Stefan
the Telerik team
Great stuff! Works a treat.
Many thanks,
Mark.
I tried the following code :
Private Sub RadGridView1_PreviewKeyDown(sender As System.Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles RadGridView1.PreviewKeyDown
If e.KeyData = Keys.Enter Then
Me.RadGridView1.GridNavigator.SelectNextRow(1)
End If
End Sub
but after editing the cell, I need to Press Enter twice (2 times) in order to move to the second row.
there's a solution ?
And lets say I have I have 3 columns, and just second column is editable, how to make it by default cell selective, in another term, wherever user click in any cell, or press enter in any cell, just cell of column 2 be selected.
Thanks
Thank you for writing.
You have to press enter key twice because the first time you do it, the grid will go out of edit mode and the next one will move the selection. The solution provided with the previous post is suitable for both of your requirements. Feel free to use it.
If need be, you can use our free online C# to VB converter: http://converter.telerik.com/.
I hope that you find this information useful.
Regards,
Stefan
Telerik
and another solution can be:
Private Sub RadGridView1_PreviewKeyDown(sender As System.Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles RadGridView1.PreviewKeyDown
If e.KeyData = Keys.Enter Then
If RadGridView1.CurrentRow.Index + 1 = RadGridView1.Rows.Count Then
Me.RadGridView1.GridNavigator.SelectFirstRow()
Me.RadGridView1.GridNavigator.SelectFirstColumn()
Else
Me.RadGridView1.GridNavigator.SelectNextRow(1)
Me.RadGridView1.GridNavigator.SelectFirstColumn()
End If
End If
End Sub
Private Sub RadGridView1_CellEndEdit(sender As System.Object, e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridView1.CellEndEdit
If RadGridView1.CurrentRow.Index + 1 = RadGridView1.Rows.Count Then
Me.RadGridView1.GridNavigator.SelectFirstRow()
Me.RadGridView1.GridNavigator.SelectFirstColumn()
Else
Me.RadGridView1.GridNavigator.SelectNextRow(1)
Me.RadGridView1.GridNavigator.SelectFirstColumn()
End If
End Sub
Hi Stefan,
I want to customize RadGridView cell focus(Tab Key) on Enter Key based on RadGridView.Column index(I developed a Settings for RadGridView in which customer can change column index,readonly,visible,etc).So RadGridView.Column index is not fixed,it will change.
Hope you got my question.If you need more clarification,please let me know.
Regards
ABDUL HAFEEL
This can be achieved by setting the EnterKeyMode property:
radGridView1.EnterKeyMode = RadGridViewEnterKeyMode.EnterMovesToNextCell;
I hope this will be useful.
Regards,
Dimitar
Progress Telerik
Hi Dimitar,
Thank you for your quick reply
In my case,I want to implement this,when pressing an Enter Key moves to a particular cell not the next cell.
How can i set TabIndex for each cell in RadGridView?
Regards
ABDUL HAFEEL
This can be achieved by using custom row behavior which allows you to handle the enter key. You can use the Tag property to store the tab indexes. Here is a sample implementation:
public
partial
class
RadForm1 : Telerik.WinControls.UI.RadForm
{
public
RadForm1()
{
InitializeComponent();
radGridView1.DataSource = GetTable();
BaseGridBehavior gridBehavior = radGridView1.GridBehavior
as
BaseGridBehavior;
gridBehavior.UnregisterBehavior(
typeof
(GridViewDataRowInfo));
gridBehavior.RegisterBehavior(
typeof
(GridViewDataRowInfo),
new
CustomGridDataRowBehavior());
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
int
count = 100;
foreach
(var row
in
radGridView1.Rows)
{
foreach
(GridViewCellInfo cell
in
row.Cells)
{
cell.Tag = count--;
}
}
}
static
DataTable GetTable()
{
DataTable table =
new
DataTable();
table.Columns.Add(
"Dosage"
,
typeof
(
int
));
table.Columns.Add(
"Drug"
,
typeof
(
string
));
table.Columns.Add(
"Name"
,
typeof
(
string
));
table.Columns.Add(
"Date"
,
typeof
(DateTime));
table.Rows.Add(25,
"Indocin"
,
"David"
, DateTime.Now);
table.Rows.Add(50,
"Enebrel"
,
"Sam"
, DateTime.Now);
table.Rows.Add(10,
"Hydralazine"
,
"Christoff"
, DateTime.Now);
table.Rows.Add(21,
"Combivent"
,
"Janet"
, DateTime.Now);
table.Rows.Add(100,
"Dilantin"
,
"Melanie"
, DateTime.Now);
return
table;
}
}
public
class
CustomGridDataRowBehavior : GridDataRowBehavior
{
protected
override
bool
ProcessEnterKey(KeyEventArgs keys)
{
int
currentIndex = (
int
)
this
.GridControl.CurrentRow.Cells[
this
.GridControl.CurrentColumn.Name].Tag;
foreach
(var row
in
GridControl.Rows)
{
foreach
(GridViewCellInfo cell
in
row.Cells)
{
if
((
int
)cell.Tag == currentIndex + 1)
{
this
.GridControl.CurrentRow = row;
this
.GridControl.CurrentColumn = cell.ColumnInfo;
break
;
}
}
}
return
true
;
}
}
I hope this will be useful.
Regards,
Dimitar
Progress Telerik
Hi Stefan,
Is there any reason ProcessKeyDown can not be called ? Because i do same steps but it can not call to custom behavior .
I am not sure why this is not working on your side. I would suggest you open a support ticket and attach your project. This will allow us to properly investigate this case and provide you with a solution.
Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Progress Telerik
I am trying to solve the exact thing the original poster posted about. I have tried the solution that was given to him (the ProcessKeyDown) method. It does go to the next editable cell however when the last cell is edited and the enter key is pressed, it just stays on that row. I need it to go to the first editable cell on the next row (in my current case a new row).
I just updated to 2018 q 1 this morning.
I have tested this and it appears to work on my side. I have attached my test project. Could you please check it and let me know what is different on your side?
I am looking forward to your reply.
Regards,
Dimitar
Progress Telerik