Thursday, 4 September 2014

How to Edit GridView using BoundField Column in ASP.Net using C#/VB.NET

Hi friends,in this article I will explain about How to Edit GridView using BoundField Column in ASP.Net using C#/VB.NET.
I already explained in the previous articles about Code First Approach using Entity Framework 4.0 Sample exampleHow to create PDF document in ASP.NET with C#/VB.NET using iTextSharp and Telerik RadGrid Grouping -Drag and Drop a column header to group by that column in ASP.NET using C#/VB.NET

When the GridView Row enters Edit Mode, the BoundField column gets converted to a TextBox dynamically to allow user modify the values.

<html xmlns="">
<head runat="server">
    <title>How to Edit the GridView using BoundField Column in ASP.Net using C#/VB.NET</title>
    <form id="form1" runat="server">
    <h4 style="color:Green">Edit GridView using BoundField Column in ASP.Net using C#/VB.NET</h4>
        <asp:GridView ID="gvData" runat="server" BackColor="White" BorderColor="#CC9966" AutoGenerateColumns="false"
            BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Font-Names="Georgia" Font-Size="Small" OnRowEditing="OnRowEditing"
                <asp:BoundField DataField="EMP_ID" HeaderText="EMP ID" />
                <asp:BoundField DataField="EMP_Name" HeaderText="EMP Name" />
                <asp:TemplateField HeaderStyle-Width="100px">
                        <asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" CommandName="Edit" />
                        <asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" OnClick="OnUpdate" />
                        <asp:LinkButton ID="lnkCancel" Text="Cancel" runat="server" OnClick="OnCancel" />
            <FooterStyle BackColor="Tan" />
            <AlternatingRowStyle BackColor="#E6E6E1" />
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

using System.Data;

public partial class EditGridViewBoundField : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("EMP_ID"), new DataColumn("EMP_Name") });
            dt.Rows.Add("1", "Kishore");
            dt.Rows.Add("2", "SambaSiva");
            dt.Rows.Add("3", "Satyam");
            dt.Rows.Add("4", "Raghava");
            dt.Rows.Add("5", "Nag");
            ViewState["dt"] = dt;
    protected void BindGridData()

        gvData.DataSource = ViewState["dt"] as DataTable;
    protected void OnRowEditing(object sender, GridViewEditEventArgs e)
        gvData.EditIndex = e.NewEditIndex;
    protected void OnUpdate(object sender, EventArgs e)
        GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
        string name = (row.Cells[0].Controls[0] as TextBox).Text;
        string country = (row.Cells[1].Controls[0] as TextBox).Text;
        DataTable dt = ViewState["dt"] as DataTable;
        dt.Rows[row.RowIndex]["EMP_ID"] = name;
        dt.Rows[row.RowIndex]["EMP_Name"] = country;
        ViewState["dt"] = dt;
        gvData.EditIndex = -1;

    protected void OnCancel(object sender, EventArgs e)
        gvData.EditIndex = -1;

Imports System.Data

Partial Public Class EditGridViewBoundField
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(1) {New DataColumn("EMP_ID"), New DataColumn("EMP_Name")})
            dt.Rows.Add("1", "Kishore")
            dt.Rows.Add("2", "SambaSiva")
            dt.Rows.Add("3", "Satyam")
            dt.Rows.Add("4", "Raghava")
            dt.Rows.Add("5", "Nag")
            ViewState("dt") = dt
        End If
    End Sub
    Protected Sub BindGridData()

        gvData.DataSource = TryCast(ViewState("dt"), DataTable)
    End Sub
    Protected Sub OnRowEditing(sender As Object, e As GridViewEditEventArgs)
        gvData.EditIndex = e.NewEditIndex
    End Sub
    Protected Sub OnUpdate(sender As Object, e As EventArgs)
        Dim row As GridViewRow = TryCast(TryCast(sender, LinkButton).NamingContainer, GridViewRow)
        Dim name As String = TryCast(row.Cells(0).Controls(0), TextBox).Text
        Dim country As String = TryCast(row.Cells(1).Controls(0), TextBox).Text
        Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
        dt.Rows(row.RowIndex)("EMP_ID") = name
        dt.Rows(row.RowIndex)("EMP_Name") = country
        ViewState("dt") = dt
        gvData.EditIndex = -1
    End Sub

    Protected Sub OnCancel(sender As Object, e As EventArgs)
        gvData.EditIndex = -1
    End Sub
End Class

The output of the above code as shown in the below figure.

When we click on Edit button then
When we click on Update button then
You can download the code by clicking on the below Download image. 

No comments:

Post a Comment

© 2012-2018 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.