Here is the markup you need to create an EditItemTemplate
within your TemplateField
:
<asp:GridView ID="GridView2" runat="server" Visible="False" AutoGenerateColumns="false"
ShowFooter="True"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Action Item">
<ItemTemplate>
<asp:TextBox ID="TextBox1" Width="500px" TextMode="MultiLine" runat="server"></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="EditActionItemTextBox" runat="server" Width="500px" TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" OnClick="ButtonAdd_Click" runat="server" Text="Add New Row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now in your code-behind where you are updating your data table, do this:
'Update the values.
Dim row = GridView2.Rows(e.RowIndex)
dt.Rows(row.DataItemIndex)("RowNumber") = (CType((row.Cells(1).Controls(0)), TextBox)).Text
dt.Rows(row.DataItemIndex)("Action Item") = (CType((row.FindControl("EditActionItemTextBox")), TextBox).Text)
Note: In your <ItemTemplate>
you may want to consider making the control read-only so that your users know they cannot change the value, usually when a user sees a text box they think they can edit the content, but it completely makes sense to have an editable text box in edit mode of your grid view. To change the the <ItemTemplate>
text box to read-only, just add ReadOnly="True"
, like this:
<ItemTemplate>
<asp:TextBox ID="TextBox1" Width="500px" TextMode="MultiLine"
runat="server" ReadOnly="True"></asp:TextBox>
</ItemTemplate>
Depending upon the browser, the control will have a slight gray tinge to it. The user will be able to click on the text, but will not be able to change the text.