public async void Created()
{
await Grid.EditCell(0, nameof(Order.CustomerID));
} |
public async void KeyDownHandler(KeyboardEventArgs args, int? OrderID, string Field)
{
if (Grid.IsEdit)
{
if (args.Key == "Enter") {
var rowIndex = await Grid.GetRowIndexByPrimaryKey(OrderID);
if (Field == "OrderID" && rowIndex == CurrentViewRecordCount - 1)
{
await Grid.AddRecord();
}
else {
await Grid.EditCell(rowIndex, Field);
}
}
}
} |
public async void KeyUpHandler(KeyboardEventArgs args, int? OrderID, string Field)
{
if (Grid.IsEdit && (args.Key == "ArrowDown" || args.Key == "ArrowUp"))
{
var cellIndex = await Grid.GetColumnIndexByField(Field);
var rowIndex = await Grid.GetRowIndexByPrimaryKey(OrderID);
rowIndex = args.Key == "ArrowDown" ? rowIndex + 1 : (args.Key == "ArrowUp" ? rowIndex -1 : rowIndex);
await Grid.EditCell(rowIndex, Field);
}
} |
<GridKeySettings MoveDownCell="ArrowDown"></GridKeySettings>
<GridEvents Created="Created" DataBound="DataBound" TValue="Order"></GridEvents>
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Batch" NewRowPosition="NewRowPosition.Bottom"></GridEditSettings>
..
..
..
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120">
<EditTemplate>
@{
var con = context as Order;
}
<SfNumericTextBox ID="OrderID" @bind-Value="con.OrderID"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, "CustomerID"))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,"OrderID"))">
</SfNumericTextBox>
</EditTemplate>
</GridColumn>
<EditTemplate>
@{
var con = context as Order;
}
<SfTextBox ID="CustomerID" @bind-Value="con.CustomerID"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, (e.Key == "Enter" && !e.ShiftKey ? "ShipCountry": "OrderID")))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,"CustomerID"))">
</SfTextBox>
</EditTemplate>
</GridColumn>
<EditTemplate>
@{
var con = context as Order;
}
<SfTextBox ID="ShipCountry" @bind-Value="con.ShipCountry"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, (e.Key == "Enter" && !e.ShiftKey ? "ShipAddress": "CustomerID")))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,"ShipCountry"))">
</SfTextBox>
</EditTemplate>
</GridColumn>
<EditTemplate>
@{
var con = context as Order;
}
<SfTextBox ID="ShipAddress" @bind-Value="con.ShipAddress"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, (e.Key == "Enter" && !e.ShiftKey ? "OrderID": "ShipCountry")))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,"ShipAddress"))">
</SfTextBox>
</EditTemplate>
</GridColumn>
|
sorry for late reply..
the below points is not available in example code
" b.When I press Enter key on last column of current row,the new row should be created (NewRowPosition = bottom )"
Point 1:-
Yes
Point 2:-
1.I want to add new row when I press enter key in last row of last column
2.If I I use enter key on last column in any row,then the cursor should go to next row.
Hope it's ckear
<SfGrid>
<GridColumns>
<GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120">
<EditTemplate>
@{
var con = context as Order;
}
<SfNumericTextBox ID="OrderID" @bind-Value="con.OrderID"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, "CustomerID"))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID, (e.Key == "ArrowRight" ? "CustomerID" : (e.Key == "ArrowLeft" ? "null" : "OrderID"))))">
</SfNumericTextBox>
</EditTemplate>
</GridColumn>
<EditTemplate>
@{
var con = context as Order;
}
<SfTextBox ID="CustomerID" @bind-Value="con.CustomerID"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, (e.Key == "Enter" && !e.ShiftKey ? "ShipCountry": "OrderID")))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,(e.Key == "ArrowRight" ? "ShipCountry" : (e.Key == "ArrowLeft" ? "OrderID" : "CustomerID"))))">
</SfTextBox>
</EditTemplate>
</GridColumn>
<EditTemplate>
@{
var con = context as Order;
}
<SfTextBox ID="ShipCountry" @bind-Value="con.ShipCountry"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, (e.Key == "Enter" && !e.ShiftKey ? "ShipAddress": "CustomerID")))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,(e.Key == "ArrowRight" ? "ShipAddress" : (e.Key == "ArrowLeft" ? "CustomerID" : "ShipCountry"))))">
</SfTextBox>
</EditTemplate>
</GridColumn>
<EditTemplate>
@{
var con = context as Order;
}
<SfTextBox ID="ShipAddress" @bind-Value="con.ShipAddress"
@onkeydown="@(e=>KeyDownHandler(e,con.OrderID, (e.Key == "Enter" && !e.ShiftKey ? "CustomerID": "ShipCountry")))"
@onkeyup="@(e=>KeyUpHandler(e, con.OrderID,(e.Key == "ArrowRight" ? "null" : (e.Key == "ArrowLeft" ? "ShipCountry" : "ShipAddress"))))">
</SfTextBox>
</EditTemplate>
</GridColumn>
</GridColumns>
</SfGrid>
@code{
public async void KeyUpHandler(KeyboardEventArgs args, int? OrderID, string Field)
{
if (Grid.IsEdit && Field != "null")
{
if ((args.Key == "ArrowDown" || args.Key == "ArrowUp"))
{
var cellIndex = await Grid.GetColumnIndexByField(Field);
var rowIndex = await Grid.GetRowIndexByPrimaryKey(OrderID);
rowIndex = args.Key == "ArrowDown" ? rowIndex + 1 : (args.Key == "ArrowUp" ? rowIndex - 1 : rowIndex);
await Grid.EditCell(rowIndex, Field);
}
else if ((args.Key == "ArrowLeft" || args.Key == "ArrowRight"))
{
var rowIndex = await Grid.GetRowIndexByPrimaryKey(OrderID);
await Grid.EditCell(rowIndex, Field);
}
}
}
public async void KeyDownHandler(KeyboardEventArgs args, int? OrderID, string Field)
{
if (Grid.IsEdit)
{
if (args.Key == "Enter") {
var rowIndex = await Grid.GetRowIndexByPrimaryKey(OrderID);
if (rowIndex == CurrentViewRecordCount - 1)
{
//The above if block is handled only for the last row
if (!args.ShiftKey)
{
if (Field == "CustomerID")
{
//To add a new row when pressing Enter key from the Last Column of Last Row
await Grid.AddRecord();
}
else {
//To edit the next cells when pressing Enter key
await Grid.EditCell(rowIndex, Field);
}
}
else
{
//To edit the previous cell when pressing Shift+Enter key
await Grid.EditCell(rowIndex, Field);
}
}
else {
if (Field == "CustomerID" && !args.ShiftKey)
{
//To edit the next row when pressing Enter Key from the Last Column of Current Row
await Grid.EditCell(rowIndex + 1, Field);
}
else
{
await Grid.EditCell(rowIndex, Field);
}
}
}
}
}
|
else {
if (rowIndex == -1)
{
//This block is handled to edit the cells by using Enter key in the newly added row(row which is not yet saved in datasource)
rowIndex = await Grid.GetRowIndexByPrimaryKey(null);
if (Field == "CustomerID" && !args.ShiftKey) {
rowIndex = rowIndex - 1;
}
}
if (Field == "CustomerID" && !args.ShiftKey)
{
//To edit the next row when pressing Enter Key from the Last Column of Current Row
await Grid.EditCell(rowIndex + 1, Field);
}
else
{
await Grid.EditCell(rowIndex, Field);
}
} |