我在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