2013-05-28 56 views
2

我在RowDataBound上有這個代碼,它交替單元格的顏色,並在單元格中添加一個onclick事件。數據是從存儲過程動態獲得的,所以gridview只有一個控件。 ButtonField。 我想爲內部沒有任何數據的單元格設置白色,我該如何實現?Gridview根據條件動態格式化

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 


    { 
     string back1 = "url('Images/GridBack1.jpg')"; 
     string back2 = "url('Images/GridBack2.jpg')"; 

     if (e.Row.RowType != DataControlRowType.DataRow) 
      return; 

     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      TableCell Cell = e.Row.Cells[i]; 


      // if both row and column are odd, color them black 
      // if both row and column are even, color them white 
      if (((e.Row.RowIndex % 2 == 1) && (i % 2 == 1)) || 
       ((e.Row.RowIndex % 2 == 0) && (i % 2 == 0))) 


      { 
       if (string.IsNullOrEmpty(e.Row.Cells[i].Text)) //Edit 
       { 
        e.Row.Cells[i].BackColor = Color.Blue; 
        e.Row.Cells[i].ForeColor = Color.White; 
       } 
       else 
       { 

        e.Row.Cells[i].Width = Unit.Pixel(450); 
        e.Row.Cells[i].Height = Unit.Pixel(67); 
        e.Row.Cells[i].Style.Add("background-image", back1); 
        //e.Row.Cells[i].Style.Add("width", "150px"); 
        //e.Row.Cells[i].Style.Add("word-wrap","break-word"); 
        e.Row.Cells[i].Attributes.Add("align", "center"); 
       } 



      } 
      else 
      { 

       if (string.IsNullOrEmpty(e.Row.Cells[i].Text)) //Edit 
       { 
        e.Row.Cells[i].BackColor = Color.Blue; 
        e.Row.Cells[i].ForeColor = Color.White; 
       } 
       else 
       { 
        e.Row.Cells[i].Width = Unit.Pixel(450); 
        e.Row.Cells[i].Height = Unit.Pixel(67); 
        e.Row.Cells[i].Style.Add("background-image", back2); 
        //e.Row.Cells[i].Style.Add("width", "150px"); 
        //e.Row.Cells[i].Style.Add("word-wrap", "break-word"); 
        e.Row.Cells[i].Attributes.Add("align", "center"); 
       } 


      } 
     string color = "#000000"; 
     e.Row.Cells[0].Attributes.Add("Style", "background-color: " + color + ";"); 
     e.Row.Cells[0].Width = Unit.Pixel(150); 
     e.Row.Cells[0].Height = Unit.Pixel(67); 


    } 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0]; 
      string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, ""); 
      // Add events to each editable cell 
      for (int columnIndex = 2; columnIndex < e.Row.Cells.Count; columnIndex++) 
      { 
       // Add the column index as the event argument parameter 
       string js = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString()); 
       // Add this javascript to the onclick Attribute of the cell 
       e.Row.Cells[columnIndex].Attributes["onclick"] = js; 
       // Add a cursor style to the cells 
       e.Row.Cells[columnIndex].Attributes["style"] += "cursor:pointer;cursor:hand;"; 
      } 
     } 
    } 


} 

回答

3

嘗試將白色分配給每個單元格,然後根據條件進行覆蓋。

1

您需要使用string.IsNullOrEmpty()

if(string.IsNullOrEmpty(e.Row.Cells[i].Text)) 
{ 
    e.Row.Cells[i].BackColor = Color.Blue; 
    e.Row.Cells[i].ForeColor = Color.White; 
} 

MSDN

希望工程。

+0

謝謝。你能告訴我什麼時候的背景嗎?我接受錯誤:名稱'當'不存在於當前的情況下 – focus

+0

oho對不起,我在想你正在使用'foreach'循環,只是使用'if condition'而不是'when'時,希望它能起作用。 – Rahul

+0

沒有改變。背景是一樣的。我也試圖改變你的條件背景圖像,但沒有運氣。 – focus