如何在运行时更改gridview行颜色?

时间:2016-07-26 14:41:20

标签: c# asp.net gridview

我正在使用网格视图,并在其中使用下拉列表

我的gridview

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

我必须更改所选索引中的颜色仅更改。 请帮忙

1 个答案:

答案 0 :(得分:0)

我为多个项目做了这个

 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)是我想要突出显示的列号。可以更改这些值以选择任何列或查找控件。这甚至在数据绑定上调用,因此数据绑定可能是您需要更新行颜色所需的数据。