这是我的问题。我有这个网格:

<asp:GridView runat="server" ID="gvTest" AutoGenerateColumns="false" DataKeyNames="IdTask" AllowPaging="true" PageSize="4"
                OnPageIndexChanging="gvTest_PageIndexChanging" PagerStyle-CssClass="pagerGrid" CssClass="gvTest" HeaderStyle-CssClass="headerGrid"
                RowStyle-CssClass="rowsGrid" OnRowCommand="gvTest_RowCommand" OnRowDataBound="gvTest_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="IdTask" HeaderText="ID" />
                    <asp:BoundField DataField="Name" HeaderText="Task" />
                    <asp:BoundField DataField="Description" HeaderText="Description" />
                    <asp:BoundField DataField="State" HeaderText="State" />
                    <asp:BoundField DataField="User" HeaderText="User" />
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:Button ID="btnLoad" runat="server" Text="Bugs" CssClass="btn" data-task-id='<%# Eval("IdTask") %>'></asp:Button>
                       </ItemTemplate>
                   </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnProgress" runat="server" Text="TakeInProgress" CommandName="progress" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'></asp:Button>
                        </ItemTemplate>
                    </asp:TemplateField>
                   <asp:TemplateField>
                       <ItemTemplate>
                           <asp:ImageButton ID="btnFiles" CssClass="btnFiles" runat="server" ImageUrl="img/files.png.png"/>
                       </ItemTemplate>
                   </asp:TemplateField>
                </Columns>
            </asp:GridView>


所有值均来自mysql数据库。在数据库中,我有多个表,但是与此问题相关的表是:

任务表:idtask,iduser,名称,说明

阶段表:idstage,idtask,idstate //连接表

状态表:idstate,名称

好的,所以在状态表中我只有3个:inWaiting,inProgress,Finished

如您所见,在网格的状态列中,每个任务都以其各自的状态填充..单击按钮时,该行被涂成红色,当状态变为inProgress时,隐藏该按钮

这是后面的代码看起来和我尝试过的内容,我只设法给行隐藏按钮上色并将状态更新为inProgress:

protected void gvTest_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "progress")
        {
            int index = Convert.ToInt32(e.CommandArgument); //converts the row index stored in command argument property to an integer
            GridViewRow row = gvTest.Rows[index]; //retrieve the row that contains the button clicked by the user from the rows collection
            string state = (row.Cells[3].Text); //gets the value of the cell
            int id = Convert.ToInt32(gvTest.DataKeys[row.RowIndex].Value.ToString()); //gets the primary key value

            string sqlUpdateState = "UPDATE states SET IdState=@IdState WHERE IdTask = @IdTask";
            MySqlCommand cmd = new MySqlCommand(sqlUpdateState, conn);
            cmd.Parameters.AddWithValue("@IdTask", id);
            cmd.Parameters.AddWithValue("@IdState", 1);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            LoadGridData();
        }

    }

    protected void gvTest_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string state = (e.Row.Cells[3].Text);
            foreach (TableCell cell in e.Row.Cells)
            {
                if (state == "inProgress")
                {
                    cell.BackColor = Color.Red;
                    e.Row.Cells[5].Controls.Clear();
                }
            }
        }
    }


我现在需要的是一次只能有一个任务处于inProgress状态..就像我单击另一行上的按钮之前的状态(处于inProgress状态)将其状态更改为inWaiting并且新任务变为inProgress
你能帮我吗?
我尽力解释了这件事..英语不是我的主要语言。如果您有任何问题,请询问。谢谢

最佳答案

我能理解的是,您希望一次完成一项任务。

将其他任务的状态更改为“进行中”时,可以将“正在进行中”的任务状态更新为“正在等待”。

试试这个代码。

protected void gvTest_RowCommand (object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "progress")
    {
        int index = Convert.ToInt32(e.CommandArgument); //converts the row index stored in command argument property to an integer
        GridViewRow row = gvTest.Rows[index]; //retrieve the row that contains the button clicked by the user from the rows collection
        string state = (row.Cells[3].Text); //gets the value of the cell
        int id = Convert.ToInt32(gvTest.DataKeys[row.RowIndex].Value.ToString()); //gets the primary key value
        string sqlChangeStateToWaiting = "UPDATE task set IDState = @IdState where IDState = @IdStateInProgress";

        MySqlCommand cmdChangeStateToWaiting = new MySqlCommand(sqlUpdateState, conn);
        cmdChangeStateToWaiting.Parameters.AddWithValue("@IdState ", "id for in waiting");
        cmdChangeStateToWaiting.Parameters.AddWithValue("@IdStateInProgress", "id for in progress");

        string sqlUpdateState = "UPDATE task SET IdState=@IdState WHERE IdTask = @IdTask";
        MySqlCommand cmd = new MySqlCommand(sqlUpdateState, conn);
        cmd.Parameters.AddWithValue("@IdTask", id);
        cmd.Parameters.AddWithValue("@IdState", 1);

        conn.Open();
        cmdChangeStateToWaiting.ExecuteNonQuery();
        cmd.ExecuteNonQuery();
        conn.Close();

        LoadGridData();
    }

}

09-26 16:26