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.
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.