繁体   English   中英

下拉列表和GridView颜色

[英]Drop down list and gridview color

好的,我在本周初得到了一些帮助,可以根据条件在GridView中突出显示一个单元格。

现在,我想将下拉列表添加到列状态(完成或未完成)。 我可以使用它,但是一旦使用新的用户输入更新了字段,我就不知道如何更改颜色。

我尝试使用下面的代码来做到这一点,但我想我需要调用DDL或模板吗?

关于如何做到这一点的任何想法?

我仍然没有运气。 看起来当状态字段通过dropdownlist更新时,它不解释新数据(autopostback设置为true。)基本上,它认为该值仍为“ null”。 有任何想法吗?

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

后面的代码:

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

如果我对您的理解正确,则需要处理DropDownList的SelectedIndexChanged事件,而不是GridView。 然后,您将使用与已有逻辑类似的逻辑,但是将基于已更改的DropDownList找到GridViewRow。

确保您的DropDownList将处理SelectedIndexChanged事件。 请记住,除非告知,否则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