GridView Data Control of ASP.Net 2.0 supports Edit mode and Data Update command. GridView Control consists of TemplateField column that can show the multiple data items. TemplateField of GridView control provides ItemTemplate and EditItemTemplate that enables you to change the GridView mode to edit or read only mode. RowEditing Event hides the ItemTemplate and shows the EditItemTemplate. You can place the DataBinder code in the ItemTemplate of TemplateField to display the read only view of GridView Data retrieved from the Database. To display the GridView Edit mode, you can place the textboxes inside the EditItemTemplate and assign the value to Text property of textboxes using DataBinder method to display the old database value in edit mode.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="2" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" Width="500px" DataKeyNames="categoryId" BorderStyle="None" ShowHeader="False" CellSpacing="2">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="outer">
<div class="inner">
<div class="innerTitle">
<b>Category:</b>
</div>
<div class="innerContent">
<div class="clear"></div>
<b>Description:</b>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCategoryName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"categoryName") %>' ></asp:TextBox>
<asp:TextBox ID="txtDescription" runat="server" Width="300px" Text='<%# DataBinder.Eval(Container.DataItem,"description") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True">
<HeaderStyle HorizontalAlign="Left" />
</asp:CommandField>
</asp:GridView>
In the next article you can learn C# code to handle the ASP.Net GridView edit and cancel events.
Currently rated 3.8 by 6 people
Tags: asp.net 2.0, c#, asp.net gridview, asp.net gridview edit mode, asp.net gridview commandfield, asp.net gridview templatefield, asp.net gridview itemtemplate, asp.net gridview edititemtemplate, c# gridview, asp.net gridview rowediting, asp.net gridview rowcancelingedit, asp.net gridview rowupdating, c# code
8/23/2008 8:49:38 PM