Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET
Article

Editable Data View Example

Rate me:
Please Sign up or sign in to vote.
1.60/5 (4 votes)
16 Jun 2008CPOL 30K   350   11   2
Editable Data View Example

Introduction

This is the code for create editable grid with view and edit functionality

Background

Basic Idea of this article to how to user the DataView functionally specially in Edit mode

Using the code

In this code you need to specify below code in aspx page, where you mainly use ItemTemplate which is for View purpose and EditItemTemplate which is for edit purpose..as Template

you can also Commnad="Update" and find its event as given below code and same case for Delete also.

        <asp:TemplateField HeaderText="ID">
            <EditItemTemplate>
                   <asp:TextBox ID="txtID" runat="server" Text='<%# Bind("DetailsId") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("DetailsId")%>'></asp:Label>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Edit">
                                <EditItemTemplate>
                                    <asp:Button ID="btnOk" Text ="OK" CommandName="Update" runat="server" />
                                    <asp:Button ID="btnCancle" Text ="Cancle"  runat="server" />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" Text ="btnEdit"  runat="server" CommandName="Edit" />
                                    <asp:Button ID="btnDelete" Text="btnDelete" runat="server" CommandName="Delete" />
                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Center" />
                            </asp:TemplateField> 
 protected void grdContact_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox lblID = (TextBox)grdView.Rows[e.RowIndex].FindControl("txtID");
        TextBox txtBN = (TextBox)grdView.Rows[e.RowIndex].FindControl("txtBookName");
        TextBox txtR = (TextBox)grdView.Rows[e.RowIndex].FindControl("txtRate");
        TextBox txtQ = (TextBox)grdView.Rows[e.RowIndex].FindControl("txtQty");
        TextBox txtT= (TextBox)grdView.Rows[e.RowIndex].FindControl("txtTotal");

        DataTable dtGrid = (DataTable)ViewState["Table"];
        DataRow[] tempDr = dtGrid.Select("DetailsID=" + lblID.Text);
        tempDr[0]["BookName"] = txtBN.Text;
        tempDr[0]["Rate"] = txtR.Text;
        tempDr[0]["Qty"] = txtQ.Text;
        tempDr[0]["Total"] = txtT.Text;

        dtGrid.AcceptChanges();
        ViewState["Table"] = dtGrid;

        grdView.EditIndex = -1;
         this.BindGrid();

        
    } 
protected void grdView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblID = (Label)grdView.Rows[e.RowIndex].FindControl("lblID");

        DataTable dtGrid = (DataTable)ViewState["Table"];
        DataRow[] tempDr = dtGrid.Select("DetailsID=" + lblID.Text);
        dtGrid.Rows.Remove(tempDr[0]);
        dtGrid.AcceptChanges();
        ViewState["Table"] = dtGrid;
        this.BindGrid();
    }


Points of Interest

This code is mainly use for initial level understanding development for Dataview.

License

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


Written By
Technical Lead Accenture Services
India India
I have total more then 6.6 years of IT Industry experience....

I have comlated my Diploma in Computern Engineering in 2003...

I have also cleared Microsoft Certified Professional Developer Specailist Exams for enterprise edition.

I have also cleared Microsoft Certified Technologies Specailist Exams for web,window and distribute application

I have write couple of article regarding .net,asp.net,SSRS, Documentum, COM,DCOM,web services,Indesign Server SDK which is published in many web sites.....

I have worked on differnet patterns like Factory, Decorative, facade, MVP.

Comments and Discussions

 
GeneralI exit problem is i use asp.net 2.0 and SqlExpress Pin
sanjay3024-Oct-08 20:25
sanjay3024-Oct-08 20:25 
Respected sir ,

I exit problem is i use asp.net 2.0 and SqlExpress ,

Unless i exit Problem in the method .
TextBox txtR = (TextBox)grdView.Rows[e.RowIndex].FindControl("txtRate");

when i click edit button gridview go edit mode right, after edit value of tamplet column we press update button that event above method is return privious value plese tell How it solve.


Plese replay me ...
GeneralNice example. Pin
Rajib Ahmed16-Jun-08 8:00
Rajib Ahmed16-Jun-08 8:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.