GridView編輯指定列的方法有多種,可以通過以下幾種方式實(shí)現(xiàn):
示例代碼如下:
<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;
// 進(jìn)行數(shù)據(jù)操作
// ...
}
示例代碼如下:
<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;
// 進(jìn)行數(shù)據(jù)操作
// ...
}
以上是兩種常見的方法,根據(jù)實(shí)際情況選擇適合的方式進(jìn)行編輯指定列的處理。