2011-12-12 66 views
7

我正在使用VS2005 C# Server-side編碼。當條件滿足時突出顯示GridView行

我很想知道,在VS2005 version,是否有可能highlight GridView條件滿足時的行?例如。如果列風險存儲爲在該特定行的數據庫中,該行將爲highlighted in Red

可能嗎?


編輯:

當前代碼:

protected void GridView1_OnRowDataBound(Object sender, GridViewRowEventArgs e) 
{ 

if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    // do your stuffs here, for example if column risk is your third column: 
    if (e.Row.Cells[3].Text == "H") 
    { 
     e.Row.BackColor = Color.Red; 
    } 
} 
} 

我認爲列單元格從0開始,所以我的是在小區3但顏色依然不改。

任何人有任何想法?

+0

沒有,只OnDataBound得到射擊一次這是不是你想要的。當您嘗試OnRowDataBound時它顯示任何錯誤嗎? – S200

+0

確保您的'GridView_OnRowDataBound'方法設置爲'public'。 – S200

+1

@RUHAHAO檢查我的解決方案,我認爲'.Text'將更適用於DataBound事件而不是'RowDataBound',因爲該值實際上由控件包含,而不是單元,所以'DataBinder.Eval'應該適合您 – V4Vendetta

回答

10

是的,將OnRowDataBound="yourGridview_RowDataBound"添加到您的gridview。每個gridview行都會觸發此事件。

在後面的代碼中,有這樣的:

public void yourGridview_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // do your stuffs here, for example if column risk is your third column: 
     if (e.Row.Cells[2].Text == 'high') 
     { 
      e.Row.BackColor = Color.Red; 
     } 
    } 
} 

類似的東西。

+0

我試過你的方法,但顏色仍然沒有改變。 – gymcode

+0

檢查你的e.Row.Cells [3] .Text真的等於「高」(正確的情況下,沒有額外的空間等)。您可以使用Response.Write(「 - 」+ e.Row.Cells [3] .Text +「 - 」)來顯示值。 – S200

+0

這是正確的,在我的頁面頂部充滿了我的第三列中的文字。 – gymcode

1

你應該認購RowDataBound事件,有你的列提風險高,則該行的BackColor設爲您的高亮顏色選擇

If (e.Row.RowType == DataControlRowType.DataRow) 
{ 
     //DataBinder.Eval(e.Row.DataItem,"Risk")) 
     //if this is high then set the color 
     e.Row.BackColor = Drawing.Color.Yellow 
} 

MSDN Formatting the GridView Based on the Underlying Data

行的網格,手抓牢的
3

使用RowDataBound事件。在這種情況下,你會得到可能會同意你的條件

void GridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
    { 

    if(e.Row.RowType == DataControlRowType.DataRow) 
    { 
     // Logic for High 
     if(e.Row.Cells[1].Text > 100) 
     //set color 
     e.Row.Attributes.Add("style", "this.style.backgroundColor = '#FFFFFF';"); 

    } 

    } 
1

RowDataBound嘗試添加基於CSS:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    // searching through the rows 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     if(int.Parse(DataBinder.Eval(e.Row.DataItem,"Risk").ToString()) > 100) 
     { 
      e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row 
     } 
    } 
} 
+0

請參閱'http:// blog.jerryleelajohn.com/2009/08/rowdatabound-vs2005.html' –

-1
 if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.BackColor = Color.Yellow; 

      Label l1 = (Label)e.Row.FindControl("lblage"); 
      if(Convert.ToInt32(l1.Text)>=30) 
      { 
       e.Row.BackColor = Color.Tomato; 
      } 

     } 
+0

如果您添加了對您的想法的描述,而不是簡單的代碼,這將會很有幫助。 –

相關問題