我在asp.net中制作一个应用程序,它显示gridview中数据库的值。在数据库中,我有一个名为StatusId的colmn,其值为1或2或3通过数据库更改gridview的行颜色值
我试图通过它们的statusId值显示不同颜色的网格视图行。但它从来没有工作。我怎么能在asp.net中做到这一点。
这里是我的代码
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
Connection.Open();
SqlCommand Command1 = Connection.CreateCommand();
Command1.CommandText = "Select statusColor from status where statusId=(Select statusId from fileInfo where userId=(Select userId from userInfo where email='" + Session["email"].ToString() + "'))";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
using (SqlDataReader reader = Command1.ExecuteReader())
{
while (reader.Read())
{
statusId = reader["statusColor"].ToString();
}
GridView1.RowStyle.BackColor = Color.FromName(statusId);
}
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.Green;
}
SqlCommand com = new SqlCommand("gridcolor", Connection);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@statusId", statusId);
com.Parameters.Add("@statusColor", SqlDbType.NVarChar, 30);
com.Parameters["@statusColor"].Direction = ParameterDirection.Output;
com.ExecuteNonQuery();
string msg = (string)com.Parameters["@statusColor"].Value;
Connection.Close();
}
什么就是我在这里做什么错误?
编辑
我有存储在名为statusColor数据库中的颜色代码。我必须将这些颜色应用于这些状态。
为什么不直接返回状态颜色作为网格绑定的数据的一部分?您目前的做法会导致数据库调用*每行*。 – 2013-04-05 05:11:12
是的,我认为这将是更好的方式来改变每一行的颜色。 – 2013-04-05 05:12:44