GridView编辑指定列的方法有多种,可以通过以下几种方式实现:
示例代码如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
// 获取指定列的编辑控件的值
TextBox txtName = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("txtName");
string newName = txtName.Text;
// 进行数据操作
// ...
}
示例代码如下:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView2_RowEditing">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="False" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
// 获取指定列的值
string name = GridView2.Rows[e.NewEditIndex].Cells[1].Text;
// 进行数据操作
// ...
}
以上是两种常见的方法,根据实际情况选择适合的方式进行编辑指定列的处理。