【发布时间】:2013-03-27 10:05:57
【问题描述】:
我正在 asp.net 中创建一个应用程序,它显示来自数据库的 gridview 中的值。在数据库中,我有一个名为 StatusId 的列,其值为 1 或 2 或 3。
我尝试通过它们的 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 的数据库中的颜色代码。我必须将这些颜色应用于这些状态。
【问题讨论】:
-
为什么不将状态颜色作为网格绑定的数据的一部分返回呢?您当前的方法将导致对每一行进行数据库调用。
-
是的,我认为这将是更改每行颜色的更好方法。
标签: asp.net gridview rowdatabound