如何在运行时更改网格视图行的颜色

本文关键字:视图 颜色 网格 运行时 | 更新日期: 2023-09-27 17:58:45

我使用的是网格视图,其中使用的是下拉列表

我的网格视图

<asp:GridView ID="grdUser" runat="server" AllowPaging="True" AllowSorting="True" CaptionAlign="Left" OnPageIndexChanging="grdUser_PageIndexChanging" OnSorting="grdUser_Sorting" PageSize="5" CssClass="table table-hover table-striped table-responsive" DataKeyNames="Email">
        <AlternatingRowStyle BackColor="#CCCCFF" />
        <HeaderStyle BackColor="#009933" Font-Bold="True" Font-Size="Medium" HorizontalAlign="Center" VerticalAlign="Middle" />
        <PagerSettings FirstPageText="First" Mode="NumericFirstLast" LastPageText="Last" PageButtonCount="4" NextPageText="" />
        <PagerStyle BackColor="#CC66FF" HorizontalAlign="Right" VerticalAlign="Middle" ForeColor="Black" />
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="ddlAct" runat="server" OnSelectedIndexChanged="ddlAct_SelectedIndexChanged" AutoPostBack="true">
                        <asp:ListItem Value="Action" Text="imtiaz" >Action</asp:ListItem>
                        <asp:ListItem Value="Activate" Text="imtiaz">Activate</asp:ListItem>
                        <asp:ListItem Value="Block" Text="imtiaz">Block</asp:ListItem>
                        <asp:ListItem Value="Delete" Text="imtiaz">Delete</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

现在有一个条件,当我点击激活时,所选的行颜色应该改变

我的代码是

protected void ddlAct_SelectedIndexChanged(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(CS))
    {
        DropDownList ddlAct = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddlAct.Parent.Parent;
        int idx = row.RowIndex;
        string Email = grdUser.DataKeys[idx]["Email"].ToString();
        //string Email = ((Label)row.Cells[0].FindControl("Email")).Text;
        DropDownList ddl = (DropDownList)row.Cells[0].FindControl("ddlAct");
        var ddlvalue = "";
        if (ddl.Text.ToString() == "Activate")
        {
            ddlvalue = "Approved";
        }
        else if (ddl.Text.ToString() == "Block")
        {
            ddlvalue = "Blocked";
        }
        else
        {
            ddlvalue = ddl.Text.ToString();
        }
        string query = "Update Users set Status ='" + ddlvalue + "' where E_Mail='" + Email + "'";

        SqlCommand cmd = new SqlCommand(query, con);
        con.Open();
        cmd.ExecuteNonQuery();
        GetDataTable();
        lblMsg.Visible = true;
        lblMsg.ForeColor = Color.Red;
        lblMsg.Text = "Status is :  Account is " + ddl.Text.ToString();
    }
}

我必须在选定的索引内更改颜色。请帮助

如何在运行时更改网格视图行的颜色

我已经为多个项目做了这件事

 Public Sub MyGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim checkCell As String = DirectCast(e.Row.FindControl("Label11"), Label).Text
        If checkCell = 0 Then
            e.Row.Cells(11).BackColor = Drawing.Color.Green
            e.Row.Cells(11).ForeColor = Drawing.Color.White
        ElseIf checkCell > 0 And checkCell < 11 Then
            e.Row.Cells(11).BackColor = Drawing.Color.Yellow
        ElseIf checkCell > 10 Then
            e.Row.Cells(11).BackColor = Drawing.Color.Red
            e.Row.Cells(11).ForeColor = Drawing.Color.White
        End If
    End If
End Sub

FindControl("Label11")指的是我在aspx页面中的数据绑定对象。e.row.cells(11)是我想要突出显示的列编号。可以更改这些值以选择任何列或查找控件。这甚至在databind上被调用,所以databind可能是您需要更新行颜色的东西。