简体   繁体   English

下拉列表和GridView颜色

[英]Drop down list and gridview color

OK, I got some help earlier this week in highlighting a cell in GridView based on conditions. 好的,我在本周初得到了一些帮助,可以根据条件在GridView中突出显示一个单元格。

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? 我尝试使用下面的代码来做到这一点,但我想我需要调用DDL或模板吗?

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". 看起来当状态字段通过dropdownlist更新时,它不解释新数据(autopostback设置为true。)基本上,它认为该值仍为“ 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. 如果我对您的理解正确,则需要处理DropDownList的SelectedIndexChanged事件,而不是GridView。 You will then use similar logic to what you already have, but find the GridViewRow based on the DropDownList that changed. 然后,您将使用与已有逻辑类似的逻辑,但是将基于已更改的DropDownList找到GridViewRow。

Make sure your DropDownList will handle the SelectedIndexChanged event. 确保您的DropDownList将处理SelectedIndexChanged事件。 Remember that DropDownLists do not automatically cause a post back unless told to. 请记住,除非告知,否则DropDownLists不会自动导致回发。

<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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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