基于sql查询输出在网格视图中填充颜色

时间:2014-05-20 07:51:04

标签: c# sql gridview colors

我有一张桌子,我们将每天使用(一次)。我正在使用查询是否已经输入。如果今天有条目,我应该标记为绿色。如果不在那里,我会将标记变成红色。以下编码不适合我。任何人都可以帮我解决这个问题。提前谢谢。

SqlCommand cmd = new SqlCommand("select * from Handover where Facility='Facility'", con);
try
{
      cmd.ExecuteNonQuery();
      GridView1.Rows[1].Cells[0].Text = "Chennai";
      GridView1.Rows[1].Cells[1].Text = "Annanagar";
      GridView1.Rows[1].Cells[2].ControlStyle.BackColor = Color.Green;
}
catch
{
      GridView1.Rows[1].Cells[0].Text = "Chennai";
      GridView1.Rows[1].Cells[1].Text = "Annanagar";
      GridView1.Rows[1].Cells[2].ControlStyle.BackColor = Color.Red;
}

错误:

  

指数超出范围。必须是非负数且小于   集合。参数名称:index

1 个答案:

答案 0 :(得分:1)

 protected void GetGrid()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("Select * from UserDetails", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        if (GridView1.Rows[i].Cells[1].Text == "k" && GridView1.Rows[i].Cells[2].Text == "j")
            GridView1.Rows[i].Cells[2].ControlStyle.BackColor = Color.Green;
        else
            GridView1.Rows[i].Cells[2].ControlStyle.BackColor = Color.Red;
    }
}