2011-01-19 65 views
0

我有一個sqldatasource,從我的服務器加載數據並將其放入數據網格中。根據行中列的值更改行的顏色

我有一個名爲列clnumber有,數字1,2,3

我想的是,每一行都有不同的顏色取決於哪個號碼是在DataRow中列

THIS IS守則基於怎樣的數量我用

$(document).ready(function() { 

    $("#<%=GridView1.UniqueID%> tr").each(function() { 

     var number = $(this).children('td:eq(6)').text(); 

     if (number == 'OK') { 
      $(this).children('td').css({ "background-color": "lightgreen" }); 

     } 
    }) 
}); 
+0

一個明顯的使用jQuery的那個片斷的問題是,「Gridview1」實際上並沒有參考任何你能重構它。現在,'#Gridview1'或'.Gridview1'實際上可能工作。看到我的新帖子下面用這個問題的jQuery解決方案。 – Didaxis 2011-01-19 18:00:12

回答

2

就算你給你的GridView稱爲「myGridView」 CSS類,你可以做到以下幾點:

$(document).ready(function() { 

    $('.myGridView tr').each(function() { 

     var number = $(this).children('td:eq(1)').text(); 

     if (number == '1') { 
      $(this).children('td').css('background', 'red'); 
     } 
    }) 
}); 

其中'td:eq(1)'表示連續的第二個單元格。這當然取決於你的行中的單元格包含這個幻數。

我敢肯定它不是最優雅的使用jQuery的,但如你所願

1


第一行白色,第二個灰色?

if(rownumber%2 == 0) //white 
else //grey 

或實際上週圍的其他方法。

+0

這將自動爲網格中的每一行做到這一點? – MyHeadHurts 2011-01-19 17:16:50

+0

把它放在GridView_RowDataBound事件中,否則如果你的GridView有Paging,它將只應用於代碼運行時出現的任何行。 – 2011-01-19 17:19:48

+0

是的。任何被兩等分的數字將是白色的,其他的將是灰色的。 – 2011-01-19 17:23:18

1

如果指定應使用哪種顏色的數字實際上是在HTML輸出中生成的,爲什麼不使用javascript?

1

您可以使用類似於此:

/// <summary> 
    /// Updates the row fore colour to show the line type 
    /// </summary> 
    /// <param name="sender">object</param> 
    /// <param name="e">args</param> 
    protected void gvLineValues_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     try 
     { 
      //Format the row 
      this.FormatRow(e); 
     } 
     catch (Exception ex) 
     { 
      ErrorLogging.LogError(ex); 
     } 
    } 

    /// <summary> 
     /// Formats a gridview row 
     /// </summary> 
     /// <param name="e">Gridview event args</param> 
     private void FormatRow(GridViewRowEventArgs e) 
     { 
      try 
      { 
       //Change the forecolor of the row 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        OrderLine oOrderLine = e.Row.DataItem as OrderLine; 

        if (oOrderLine != null) 
        { 
         e.Row.ForeColor = oOrderLine.ForeColour; 


           //Check the line is over budget 
           if (oOrderLine.OverBudget) 
           { 
            e.Row.BackColor = ColourManager.OverBudgetItemBackColour; 
            e.Row.ToolTip = String.Format("Item over {0} and awaiting your approval", GlobalizationManager.Budget); 
           } 
           else 
           { 
            e.Row.BackColor = ColourManager.WithinBudgetItemBackColour; 
            e.Row.ToolTip = "Item awaiting your approval"; 
           } 
          } 
         } 

         //Change the back colour of the row if its a deleted row 
         if (oOrderLine.Deleted) 
         { 
          e.Row.Font.Strikeout = true; 
          e.Row.ToolTip = "This line has been deleted"; 
         } 
        } 
       } 
      } 
      catch (Exception) 
      { 
       throw; 
      } 
     } 

你可以做類似

switch (DataItem.ColorNumber) 
{ 
case 1: 

e.row.backcolor = Color.blue; 

break; 

case 2: 

e.row.backcolor = Color.red; 

break; 

case 3: 

e.row.backcolor = Color.white; 

break; 

}