如何通过从DropDownList中选择一个值来更新/插入GridView中的状态(一列)?

时间:2012-02-28 08:03:10

标签: c# asp.net

我的数据库中有两个表如下:

建议表:ID,标题,描述,StatusID等等

SuggestionsStatus表:ID,状态

我正在使用GridView来显示建议,在最后一栏中我放了一个DropDownList来选择每个Suggestion的状态。现在,当我尝试通过从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)中提交的建议之一的状态?

1 个答案:

答案 0 :(得分:0)

您需要更改update语句以包含记录的ID,例如:

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

要获取更新的ID,您需要执行此帖子中的操作以获取行索引,然后使用该索引查找ID:get selected row index of dynamic dropdown list selection

此致 安迪