Click here to Skip to main content
14,159,205 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
When i press Enter Key on row which is in editmode in gridview it has to save and it has to go to next row and that row has to display in editmode.To update the data easier.Please help me

What I have tried:

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{

int rowNum = e.NewEditIndex;
GridViewRow row = gv.Rows[Convert.ToInt32(e.CommandArgument)];
gv.EditIndex = Convert.ToInt32(row);
DataTable dt = (DataTable)Session["dt"];
gv.DataSource = dt;
gv.DataBind();
}
Posted
Updated 13-Feb-17 7:05am
Comments
Richard Deeming 17-Feb-17 11:13am
   

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Here's a solution using only code begind and no JavaScript:

1. To achieve this, make use of the form.defaultbutton property.
2. Ensure that edit controls .e.g text boxes are manually defined in TemplateFields with Id property set.
3. Ensure the Edit, Update buttons are also defined manually inside TemplateFields with Ids set, this is how the enter button will allow you to update, and move on to next row.
4. SetFocus() - tailor this to your own requirement to aid user experience.

Markup:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EnterKeyEditGrid.aspx.cs" Inherits="WebFormsGridViewCommand.EnterKeyEditGrid" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView runat="server" ID="gv1"
                AutoGenerateColumns="false"
                OnRowEditing="gv1_RowEditing"
                OnRowCancelingEdit="gv1_RowCancelingEdit"
                OnRowUpdating="gv1_RowUpdating">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <%#Eval("Item") %>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox runat="server" ID="ItemTextBox" Text='<%#Eval("Item") %>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="Price" DataField="Price" DataFormatString="{0:c}" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button runat="server" ID="editButton" Text="Edit" CommandName="edit" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Button runat="server" ID="updateButton" Text="Update" CommandName="update" ClientIDMode="AutoID" />
                            <asp:Button runat="server" ID="cancelButton" Text="Cancel" CommandName="cancel" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>


Code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormsGridViewCommand
{
    public partial class EnterKeyEditGrid : System.Web.UI.Page
    {
        private const string SESSION_GV_DATA = "Data";
        private DataTable _Data
        {
            get
            {
                if (Session[SESSION_GV_DATA] == null)
                {
                    Session[SESSION_GV_DATA] = GetData();
                }
                return (DataTable)Session[SESSION_GV_DATA];
            }
            set { Session[SESSION_GV_DATA] = value; }
        }

        public void SetUpdateButtonId()
        {
            if (gv1.EditIndex != -1)
            {
                Button updateButton = (Button)gv1.Rows[gv1.EditIndex].FindControl("UpdateButton");
                if (updateButton != null)
                {
                    Form.DefaultButton = updateButton.UniqueID;
                }
            }
            else
            {
                Form.DefaultButton = null;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadGridView();
            }
        }

        protected void LoadGridView()
        {
            _Data = GetData();

            gv1.DataSource = _Data;
            gv1.DataBind();
        }

        private DataTable GetData()
        {
            DataTable dt = new DataTable("data1");
            dt.Columns.AddRange(
                new DataColumn[]
                {
                    new DataColumn("Item",typeof(string)),
                    new DataColumn("Price", typeof(decimal))
                });
            List<object[]> dataItems = new List<object[]>
            {
                new object[] { "Apple", 0.30m },
                new object[] { "Orange", 0.20m },
                new object[] { "Pear", 0.40m }
            };

            dataItems.ForEach(x => dt.Rows.Add(x));

            return dt;
        }

        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            LoadGridView();
            SetFocus();
        }

        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv1.EditIndex = -1;
            LoadGridView();
        }

        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Implement your data update code here...

            SetEditRow();
            LoadGridView();
            SetUpdateButtonId();
            SetFocus();
        }

        private void SetEditRow()
        {
            if (gv1.EditIndex == -1) return;

            if (gv1.EditIndex + 1 < gv1.Rows.Count)
            {
                if (gv1.Rows[gv1.EditIndex + 1].RowType == DataControlRowType.DataRow)
                {
                    gv1.EditIndex++;
                }
            }
            else
            {
                gv1.EditIndex = -1;
            }
        }

        private void SetFocus()
        {
            if (gv1.EditIndex == -1) return;
            gv1.Rows[gv1.EditIndex].Cells[0].FindControl("ItemTextBox").Focus();
        }
    }
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 13 Feb 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100