Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否使用编辑按钮更新gridview中的特定单元格?_C#_Datagridview - Fatal编程技术网

C# 是否使用编辑按钮更新gridview中的特定单元格?

C# 是否使用编辑按钮更新gridview中的特定单元格?,c#,datagridview,C#,Datagridview,单击“编辑”按钮时,我想在gridview中的特定单元格中编辑TaskName和DueDate。任何人都可以提供一些代码,我可以将其放入if(e.CommandName==“Edit”) 这是我在Checklist.aspx.cs中的代码: protected void gvAddTask_RowCommand(object sender, GridViewCommandEventArgs e) { int i = int.Parse(e.CommandArgument.ToSt

单击“编辑”按钮时,我想在gridview中的特定单元格中编辑TaskName和DueDate。任何人都可以提供一些代码,我可以将其放入if(e.CommandName==“Edit”)

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

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();
    }

}
这是我的gridview代码:

<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作为任务名、DueDate和按钮。 然后,在Rowcommand事件上,您可以使用findcontrol属性更改该值

另请查看此链接:

在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

对不起,你说的内联编辑是什么意思?我想直接在GridView中进行编辑,因此我需要为TaskName、DueDate和button创建boundfield和templatefield?查看链接后,我可以获得特定单元格的值。但是当我编辑这些值时,madeOk没有任何变化。现在使用方法中的上述值更新这些字段的数据库表值,并再次绑定网格。