C# 基于sql查询输出的网格视图填充颜色
我有一个表,我们将每天使用(一次)。我使用一个查询来确定它是否已在今天输入。如果今天有参赛作品,我会打绿色的。如果没有,我就用红色标记。下面给出的代码不适合我。谁能帮我解决这个问题。提前谢谢C# 基于sql查询输出的网格视图填充颜色,c#,sql,gridview,colors,C#,Sql,Gridview,Colors,我有一个表,我们将每天使用(一次)。我使用一个查询来确定它是否已在今天输入。如果今天有参赛作品,我会打绿色的。如果没有,我就用红色标记。下面给出的代码不适合我。谁能帮我解决这个问题。提前谢谢 SqlCommand cmd = new SqlCommand("select * from Handover where Facility='Facility'", con); try { cmd.ExecuteNonQuery(); GridView1.Rows[1].Cells
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;
}
错误:
索引超出范围。必须为非负数且小于
收藏。参数名称:索引
受保护的void GetGrid()
{
con.Open();
SqlCommand cmd=newsqlcommand(“从UserDetails中选择*”,con);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
da.填充(ds);
con.Close();
如果(ds.Tables[0].Rows.Count>0)
{
GridView1.DataSource=ds;
GridView1.DataBind();
}
对于(int i=0;i
GridView有多少列?为什么要将ExecuteNonQuery
与SELECT
语句一起使用?我看没有必要用它。它只是执行你的查询,什么。。?它甚至没有得到任何值。@user..gridview的数据绑定逻辑在哪里???@user..我认为人们不会对select语句使用EXCETENTONQUERY()。您正在尝试访问一个不存在的单元格。确保有足够的列。我猜想细胞不存在,也可以考虑使用参数而不是写“设施”。并使用var reader=cmd.ExecuteReader();我有3列,我确实想将数据从sql表绑定到gridview。如果查询有任何值,那么它将在单元格中显示为绿色。如果查询没有任何值,则单元格中的查询将为红色。
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;
}
}