使用编辑按钮更新网格视图中的特定单元格

本文关键字:单元格 视图 网格 编辑 编辑按 按钮 更新 | 更新日期: 2023-09-27 18:24:12

单击编辑按钮时,我想在网格视图中的特定单元格中编辑TaskName和DueDate。任何人都可以提供一些我可以放在if(e.CommandName=="Edit")中的代码?

这是我在Checklist.aspx中的代码:

protected void gvAddTask_RowCommand(object sender, GridViewCommandEventArgs e)
{    
    int i = int.Parse(e.CommandArgument.ToString());    
    string CheckListId = gvAddTask.Rows[i].Cells[0].Text;
    if(e.CommandName == "Edit")
    {
    }
    if (e.CommandName == "Del")
    {
        BlChecklist blChecklist = new BlChecklist();
        blChecklist.DeleteChecklist(int.Parse(gvAddTask.Rows[i].Cells[0].Text));
        gvAddTask.DataBind();
    }
}

这是我的网格视图代码:

<asp:GridView ID="gvAddTask" runat="server" AutoGenerateColumns="False" Width="100%"
EmptyDataText="There are no tasks!" GridLines="Horizontal" 
OnRowCommand="gvAddTask_RowCommand" DataSourceID="ObjectDataSource1" 
CellPadding="4" BackColor="White" BorderColor="#336666" 
BorderStyle="Double" BorderWidth="3px" style="margin-top: 0px">
<Columns>
<asp:BoundField DataField="CheckListId" HeaderText="CheckListId" 
HeaderStyle-HorizontalAlign="Left" ReadOnly="True">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="TaskName" HeaderText="TaskName" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:BoundField DataField="DueDate" HeaderText="DueDate" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:ButtonField  ButtonType="Button" CommandName="Edit" Text="Edit"/>
<asp:ButtonField ButtonType="Button" CommandName="Del" Text="Delete" />
</Columns>
</asp:GridView>

使用编辑按钮更新网格视图中的特定单元格

您必须使用asp:TemplateField作为TaskName、DueDate和按钮。然后,在Rowcommand事件中,u可以使用findcontrol属性来更改值。

另请查看此链接:http://forums.asp.net/t/1528797.aspx/1

在Rowcommad事件中,您可以使用以下代码。

if (e.CommandName == "Edit")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = gviewRetailers.Rows[index];
        string CheckListId =  row.Cells[0].Text;
        string sql="update tblname set TaskName ="u want" ,DueDate ="U wnat" where id = CheckListId "; // for example only use can use your update query
       // Update value and Bind grid again
    }

谢谢,Hitesh