2012-02-28 59 views
0

我在數據庫中兩個表如下:如何通過從DropDownList中選擇一個值來更新/插入GridView中的狀態(一列)?

建議表:ID,標題,描述,StatusID ...等

SuggestionsStatus表:ID,狀態

我使用的GridView顯示建議,並在最後一列我把一個DropDownList,爲每個建議選擇狀態。現在,當我試圖通過從DropDownList中選擇狀態來更新其中一個提交建議的狀態時,該值已被選中,但未被插入到數據庫中(這意味着仍然在StatusID列中有NULL值建議表),我不知道爲什麼。

我的ASP.NET代碼:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
         AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
         width="950px" CssClass="mGrid" 
         AlternatingRowStyle-CssClass="alt" 
         RowStyle-HorizontalAlign="Center" 
         DataSourceID="SqlDataSource1" 
         OnRowDataBound="GridView1_RowDataBound" > 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
      <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
      <Columns> 
       <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
        ReadOnly="True" SortExpression="ID" /> 
       <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 
       <asp:BoundField DataField="Description" HeaderText="Description" 
        SortExpression="Description" /> 
       <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
       <asp:BoundField DataField="Username" HeaderText="Username" 
        SortExpression="Username" /> 
       <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
        SortExpression="DivisionShortcut" /> 
       <asp:TemplateField HeaderText="Status"> 
        <ItemTemplate> 
         <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2" 
              Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" 
              DataTextField="Status" DataValueField="ID" AutoPostBack="true" 
              OnDataBound="DropDownList_DataBound"> 
         </asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
      SelectCommand="SELECT  dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username, 
         dbo.Divisions.DivisionShortcut 
FROM   dbo.employee INNER JOIN 
         dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN 
         dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode" 
         FilterExpression="[DivisionShortcut] like '{0}%'"> 

         <FilterParameters> 
         <asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut" 
               PropertyName="SelectedValue" Type="String" /> 
        </FilterParameters> 
     </asp:SqlDataSource> 

     <%--For the DropDownList--%> 
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
     SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource> 

我的後臺代碼:

protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)sender; 
    int suggestionStatus = int.Parse(ddl.SelectedValue); 
    //For inserting the status in the database 
    string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True"; 
    string updateCommand = "UPDATE SafetySuggestionsLog SET [StatusID] = @StatusID"; 
    using (SqlConnection conn = new SqlConnection(connString)) 
    { 
     conn.Open(); 
     using (SqlCommand cmd = new SqlCommand(updateCommand, conn)) 
     { 
      cmd.Parameters.Clear(); 
      cmd.Parameters.AddWithValue("@StatusID", suggestionStatus); 
      cmd.ExecuteNonQuery(); 
     } 
     conn.Close(); 
    } 
} 

它的工作原理,但它會更新所有提交建議的狀態。

那麼如何解決這個問題,以便能夠更新數據庫中(GridView)提交的建議之一的狀態?

回答

0

您需要更改的更新語句,包括備案有點像ID:

UPDATE SafetySuggestionsLog SET [StatusID] = @StatusID WHERE SafetySuggestionId = @SafetySuggestionId 

要獲取更新的ID,你需要做的這個帖子有點像讓行索引,然後用它來查找ID:get selected row index of dynamic dropdown list selection

問候 安迪

+0

感謝您的幫助,但該職位是很難理解,所以你可以請我提供的代碼片段? – 2012-02-28 09:44:24

+0

這個想法是,你是下拉下拉列表這是發件人,並獲得父母。然後,你繼續得到父母的父母,直到你到達你需要的元素,這將是我認爲你的情況下的DataRow。一旦你有了數據行,你就可以到達包含你需要更新的ID的DataCell。我相信你會很快開始工作,祝你好運 – 2012-02-28 13:21:30

相關問題