2013-04-05 93 views
0

我在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數據庫中的顏色代碼。我必須將這些顏色應用於這些狀態。

+2

爲什麼不直接返回狀態顏色作爲網格綁定的數據的一部分?您目前的做法會導致數據庫調用*每行*。 – 2013-04-05 05:11:12

+0

是的,我認爲這將是更好的方式來改變每一行的顏色。 – 2013-04-05 05:12:44

回答

0

你有statusId有值1,2,3和要傳遞到Color.FromName這和1,2,3-不是顏色的名稱,你可以使用switch分配基於statusId不同的顏色。

Color rowColor = Color.Red;  
switch(statusId) 
{ 
    case 1: 
     rowColor = Color.Green; 
     break; 

    case 2: 
     rowColor = Color.White; 
     break; 
    case 3: 
     rowColor = Color.Blue; 
     break; 
} 
GridView1.RowStyle.BackColor = rowColor ; 
+0

檢查更新的問題朋友.... – 2013-04-05 05:15:58

+0

statusId會有什麼值,它們仍然是1,2,3問題? – Adil 2013-04-05 05:23:47

+0

是的,而不是進入編碼,我們可以更改數據庫中的顏色值。 – 2013-04-05 05:25:01

0

你這樣做是錯誤的。你必須在網頁加載或自定義事件(比如點擊一個按鈕)上數據綁定你的gridview &不是rowDataBound事件。當你的行與數據&綁定,你想改變每行的一些屬性時(如你的情況),就會發生這個事件。

您可以使用GridView的DataBound事件以下列方式

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Fetch status Id for current row 
     int statusId= Convert.ToInt32(DataBinder.Eval(e.Row.DataItem,"UnitsInStock")); 

     switch (statusId) 
     { 
     case (1): 
        e.Row.BackColor= System.Drawing.Color.Green; 
        break; 

     // other cases follow the same 
     } 

    } 
} 

把數據綁定代碼在任一頁面加載或按鈕的Click事件指定顏色。

0
protected void grdMyQ_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      for (int i = 0; i < grdMyQ.Rows.Count; i++) 
      { 
       if (grdMyQ.Rows[i].Cells[13].Text.ToUpper() == "DISCHARGED_PROCESS") 
       { 
        grdMyQ.Rows[i].BackColor = Color.Red; 

       } 
      } 
     }