步骤一:先创建一个GridView 在编辑列中将此字段转换为TemplateField
<asp:GridView Width="100%" ID="GridView2" runat="server"
AutoGenerateColumns="False" DataKeyNames="id"
onrowcancelingedit="GridView2_RowCancelingEdit" ShowFooter="True"
onrowupdating="GridView2_RowUpdating" onrowediting="GridView2_RowEditing"
EmptyDataText="暂时没有数据" onrowdeleting="GridView2_RowDeleting"
onrowcommand="GridView2_RowCommand">
<EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Top" />
<Columns>
<asp:TemplateField >
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Width="50" Text='<%# Bind("difficultName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("difficultName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox4" runat="server" Width="50"></asp:TextBox>
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="300px" />
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Width="50" Text='<%# Bind("waitScore") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("waitScore") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TextBox5" runat="server" Width="50"></asp:TextBox>
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="300px" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="false" InsertVisible="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="编辑"></asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"
CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="True" CommandName="Insert"
Text="插入"></asp:LinkButton>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);
difficu.Delete(id);
Bind();
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
switch (e.CommandName)
{
case "Insert":
myWeb.Model.DifficultDegree d = new myWeb.Model.DifficultDegree();
d.fatherId = 4;
d.difficultName = ((TextBox)GridView2.FooterRow.FindControl("TextBox4")).Text;
d.waitScore = Convert.ToInt32(((TextBox)GridView2.FooterRow.FindControl("TextBox5")).Text);
difficu.Add(d);
Bind();
break;
}
}
catch (Exception ex)
{ }
}