2017-02-16 42 views
0

這是我的問題。我有這個網格:如何限制這個GridView只有一個單元格具有相同的值?

<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,ID用戶,名稱,描述

階段表:idstage,idtask,idstate //連接表

狀態表:idstate,名

好了,所以在狀態表我只有3個:inWaiting,inProgress,則成品

正如你可以在網格的狀態列中看到填充有其各自的狀態每個任務。 。th當按鈕被點擊時,e行被着色爲紅色,並且一旦狀態變爲進入狀態,就隱藏該按鈕

以下是代碼隱藏的樣子以及我所嘗試過的,我只設法給行隱藏按鈕着色並將狀態更新爲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 [email protected] 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和按鈕新任務正在進行中 你能幫忙嗎? 我試圖盡我所能解釋它..英語不是我的主要語言。如果您有任何疑問,請詢問。謝謝

回答

0

我能理解的是,你一次只需要完成一項任務就可以「進行中」。

您可以更新這是「正在進行中」到「等待」到「進展」更改其他任務的狀態,當任務的狀態。

試試看看這個代碼。

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 [email protected] 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(); 
    } 

} 
+0

OMG是的!非常感謝!它的工作..我只是有一個堵塞,無法弄清楚我應該如何更新他們:) –

相關問題