Click here to Skip to main content
Click here to Skip to main content

Editable Data View Example

, 16 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Amit M patel
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 Pinmembersanjay3024-Oct-08 21:25 
GeneralNice example. PinmemberRajib Ahmed16-Jun-08 9:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411028.1 | Last Updated 16 Jun 2008
Article Copyright 2008 by Amit M patel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid