Is it possible for the user NOT to enter a new row when hitting the Arrow down- key? I want the new row to be added only when hitting Enter.
Regards, Jill-Connie Lorentsen
5 Answers, 1 is accepted
You can create a custom edit manager where you can change the default behavior:
public
class
CustomRadGridViewEditManager : GridViewEditManager
{
public
CustomRadGridViewEditManager(RadGridViewElement gridView)
:
base
(gridView)
{
}
protected
override
void
OnPositionChanging(PositionChangingEventArgs args)
{
GridViewRowInfo row =
this
.GridViewElement.Template.CurrentRow;
if
(row
is
GridViewNewRowInfo && args.Row != row)
{
this
.CloseEditor();
args.Cancel =
true
;
}
base
.OnPositionChanging(args);
args.Cancel =
false
;
}
}
Then you should replace the default one in the following way:
this
.radGridView1.GridViewElement.EditorManager =
new
CustomRadGridViewEditManager(
this
.radGridView1.GridViewElement);
I hope this helps.
Greetings,Svett
the Telerik team
Thanks for your answer, but it doesn't solve my problem completely. Maybe I didn't explain my scenario well enough.
I have a grid with ComboBox-columns and TextBox-columns. The ComboBox-columns are filled with items from the database. A typical scenario when the user adds a new row is that he starts at the first column, and uses TAB to move to the next one. In a ComboBox column he uses the ArrowDown to chose one of the items, and then TAB to move on. In the Textbox columns he often presses ArrowDown by mistake, and then the new row is added (or, it won't be, as some values are missing).
From what I see from what you suggest is that all the values that are entered are removed, and the user has to start all over again. I simply want the user to be able to go back to entering the rest of the values in the new row, and then adding it by pressing ENTER when done.
I hope you understand what I mean.
In that case you should create a custom grid behavior which will prevent adding new row in the particular cases:
public
class
RadGridBehavior : BaseGridBehavior
{
public
override
bool
ProcessKeyDown(KeyEventArgs keys)
{
if
(
this
.GridViewElement.IsInEditMode && keys.KeyCode != Keys.Enter &&
this
.GridViewElement.CurrentRow
is
GridViewNewRowInfo)
{
return
false
;
}
return
base
.ProcessKeyDown(keys);
}
}
Then you should replace the default one in the following way:
this
.radGridView1.GridBehavior =
new
RadGridBehavior();
I hope this helps.
Regards,Svett
the Telerik team
The first piece of code is OK for clicking on another line of the table. Using the the Leave event + CancelAddNewRow() on the new row is OK for clicking outside the grid view.
private
void
ontGridViewLeave(
object
sender, EventArgs e)
{
// Do not add new row when clicking outside the grid view.
var newRow = _mappingTableListGridView.TableElement.GetRowElement(_mappingTableListGridView.MasterView.TableAddNewRow)
as
GridNewRowElement;
if
(newRow !=
null
)
{
((GridViewNewRowInfo)newRow.RowInfo).CancelAddNewRow();
}
}
However, I did not manage to deal with the case when the user clicks in the grid view but under the last row (in the blank space). How to prevent the new row to be added in this case ?
Thanks,
Amand.
Thank you for writing.
Do handle this case, you should create a GridViewBehavior and override the OnMouseDown method:
public
class
RadGridBehavior : BaseGridBehavior
{
public
override
bool
OnMouseDown(MouseEventArgs e)
{
if
(
this
.GridViewElement.IsInEditMode &&
this
.GridViewElement.CurrentRow
is
GridViewNewRowInfo)
{
return
false
;
}
return
base
.OnMouseDown(e);
}
}
Here is how to put this behavior in action:
this
.radGridView1.GridBehavior =
new
RadGridBehavior();
Also, here is an easier way to get the GridViewNewRowInfo:
void
radGridView1_Leave(
object
sender, EventArgs e)
{
radGridView1.MasterView.TableAddNewRow.CancelAddNewRow();
}
I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.
Regards,
Stefan
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.