如何在回发时保持gridview行颜色

时间:2014-02-24 11:48:02

标签: c# gridview colors

我有一个gridview,根据

我有一些coloumns
  

8个单元格文本

我想改变我已完成此部分的整行的颜色但我的问题是当我在该页面上进行一些搜索或任何回发发生在该页面时我的行颜色消失了

这是改变GridView颜色的代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int status = int.Parse(e.Row.Cells[8].Text);
        foreach (TableCell cell in e.Row.Cells)
        {
            if (status == 1)
            {
                cell.BackColor = System.Drawing.Color.Red;
            }
            if (status == 2)
            {
                cell.BackColor = System.Drawing.Color.Chocolate;
            }
            if (status == 3)
            {
                cell.BackColor = System.Drawing.Color.Green;
            }
        }
    }
}

编辑

页面加载

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.GetData();
        Logs.InsertLogs(Session["DomainID"].ToString(), "User at Report Page", "~/Report.aspx page");
    }
}
private void GetData()
{
    try
    {
        con.Open();
        ad = new SqlDataAdapter("Select * from Web_Server", con);
        ad.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        GridView1.Dispose();
        con.Close();
    }
    catch (Exception ex) // by using rowcreated i get error Input string was not in a correct format. 
    {
        Response.Write("<script>alert('Details not present..!!!');</script>");
    }
}

任何帮助都会很明显...在此先感谢!!!

1 个答案:

答案 0 :(得分:1)

尝试绑定到GridView.RowCreated事件而不是GridView.RowDataBound,我相信在回发时调用RowCreated,而RowDataBound仅在数据绑定时调用。

编辑:添加了示例代码

<asp:GridView runat="server" ID="GridView1" RowCreated="GridView1_RowCreated"> ...

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
  // Your code goes here.
}