Update GridView row on Button Click using C# and VB.Net in ASP.Net

indradeo
 
on Aug 11, 2021 04:59 AM
818 Views

Dear Sir,

how to make update function, while user click on edit button then update the value.

how to do this.

<div>
    <asp:GridView ID="gvEmployees" DataKeyNames="EmployeeId" runat="server" AutoGenerateColumns="false"
    OnRowEditing="OnEditEmployee" OnRowDataBound="OnRowDataBound" OnRowUpdating="gvEmployees_RowUpdating" OnRowCancelingEdit="OnCancelEdit">
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="Name" />
        <asp:TemplateField HeaderText="City">
            <ItemTemplate>
                <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlCities" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Birth Date">
            <ItemTemplate>
                <asp:Label ID="lblBirthDate" runat="server"
                    Text='<%#Convert.ToDateTime(Eval("BirthDate")).ToString("yyyy-MM-dd") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtBirthDate" runat="server"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
<script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('[id*=txtBirthDate]').datepicker({
            dateFormat: "yy-mm-dd",
            changeMonth: true,
            changeYear: true,
            yearRange: '1950:2050'
        });
    });
</script>
</div>

 

namespace Closer_contract
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
        SqlConnection con;
        SqlDataAdapter adapt;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindGrid();
            }
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && gvEmployees.EditIndex == e.Row.RowIndex)
            {
                DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities");
                string sql = "SELECT DISTINCT City FROM Employees";
                string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
                using (SqlConnection con = new SqlConnection(conString))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            ddlCities.DataSource = dt;
                            ddlCities.DataTextField = "City";
                            ddlCities.DataValueField = "City";
                            ddlCities.DataBind();
                            string selectedCity = DataBinder.Eval(e.Row.DataItem, "City").ToString();
                            if (ddlCities.Items.FindByValue(selectedCity) != null)
                            {
                                ddlCities.Items.FindByValue(selectedCity).Selected = true;
                            }
                        }
                    }
                }
            }
        }

        protected void OnEditEmployee(object sender, GridViewEditEventArgs e)
        {
            gvEmployees.EditIndex = e.NewEditIndex;
            this.BindGrid();
        }

        protected void OnCancelEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvEmployees.EditIndex = -1;
            this.BindGrid();
        }

        private void BindGrid()
        {
            string sql = "SELECT * FROM Employees";
            string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(conString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter(sql, con))
                {
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        gvEmployees.DataSource = dt;
                        gvEmployees.DataBind();
                    }
                }
            }
        }
        protected void gvEmployees_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
        {
           
        }
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 11, 2021 05:02 AM