简体   繁体   中英

Drop down list and gridview color

OK, I got some help earlier this week in highlighting a cell in GridView based on conditions.

Now, I want to add a drop down list to a column status (done or not done). I got it to work, but I don't know how to change the color once the field is updated with new user input.

I tried using the below code to do it, but I'm guessing I need to call the DDL or template?

Any ideas on how to accomplish this?

I still don't have any luck with this. It looks like when the status field is updated via dropdownlist, it doesn't interpret the new data (autopostback is set to true.) Basically, it thinks that the value is still "null". Any ideas?

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="SID" DataSourceID="SqlDataSource2" OnDataBound="GridView2_SelectedIndexChanged" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
   <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" ReadOnly="True" SortExpression="SID" />
        <asp:BoundField DataField="Servers" HeaderText="Servers" SortExpression="Servers" />
        <asp:TemplateField HeaderText="Status" SortExpression="Status">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Status") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem>done</asp:ListItem>
                    <asp:ListItem>not done</asp:ListItem>
                    <asp:ListItem></asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code behind:

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView2.Rows)
    {
        if (row.Cells[3].Text == "done")
        {
            row.BackColor = System.Drawing.Color.LimeGreen;
        }
        else if (row.Cells[3].Text == "not done")
        {
            row.BackColor = System.Drawing.Color.Red;
        }
    }
}

If I am understanding you correctly, you need to handle the SelectedIndexChanged event of the DropDownList, not the GridView. You will then use similar logic to what you already have, but find the GridViewRow based on the DropDownList that changed.

Make sure your DropDownList will handle the SelectedIndexChanged event. Remember that DropDownLists do not automatically cause a post back unless told to.

<asp:DropDownList ID="DropDownList1" runat="server"
    SelectedValue='<%# Bind("Status") %>' 
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" 
    AutoPostBack="True">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    GridViewRow row = (GridViewRow)ddl.NamingContainer;

    if(ddl.SelectedItem.Text == "done")
    {
        row.BackColor = System.Drawing.Color.LimeGreen;
    }
    else if(ddl.SelectedItem.Text == "not done")
    {
        row.BackColor = System.Drawing.Color.Red;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM