2011-02-19 6 views

답변

41
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Attributes.Add("style", "cursor:help;"); 
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     {     
      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'"); 
      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'"); 
      e.Row.BackColor = Color.FromName("#E56E94");     
     }   
    } 
    else 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'"); 
      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'"); 
      e.Row.BackColor = Color.FromName("gray");     
     } 

     //e.Row.Cells[0].BackColor = Color.FromName("gray"); 
     //e.Row.Cells[1].BackColor = Color.FromName("gray"); 
     //e.Row.Cells[2].BackColor = Color.FromName("gray"); 
     //e.Row.Cells[3].BackColor = Color.FromName("gray"); 
     //e.Row.Cells[4].BackColor = Color.FromName("gray"); 
     //e.Row.BorderWidth = 2; 
     //e.Row.BorderColor = Color.FromName("#43C6DB"); 
    } 
} 
+2

'Row' 속성 등을 수정하는 대신 두 개의 CSS 클래스를 만들고 'e.Row.CssClass'만 첸지하게 만드는 것이 더 좋습니다. 더 간단한 코드로 인해 로직과 뷰가 더 잘 분리됩니다. –

8

GridView에 대해 GridView1_RowDataBound 이벤트를 만듭니다.

//Check if it is not header or footer row 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    //Check your condition here 
    If(Condition True) 
    { 
     e.Row.BackColor = Drawing.Color.Red // This will make row back color red 
    } 
} 
19
protected void DrugGridView_RowDataBound(object sender, GridViewRowEventArgs e) 

{ 
    // To check condition on integer value 
    if (Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Dosage")) == 50) 
    { 
     e.Row.BackColor = System.Drawing.Color.Cyan; 
    } 
} 
-4
\\loop throgh all rows of the grid view 

if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value1") 
{ 
    GridView1.Rows[i - 1].ForeColor = Color.Black; 
} 
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value2") 
{ 
    GridView1.Rows[i - 1].ForeColor = Color.Blue; 
} 
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value3") 
{ 
    GridView1.Rows[i - 1].ForeColor = Color.Red; 
} 
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value4") 
{ 
    GridView1.Rows[i - 1].ForeColor = Color.Green; 
} 
3

이 방법은 특정 문자열 ("TextToMatch")가 열 중 하나에서 발생 (어두운 빨강) 뒷면의 색상 (흰색에) 텍스트의 경우를 모두 수정 :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.Cells[8].Text.Equals("TextToMatch")) 
    { 
     e.Row.BackColor = System.Drawing.Color.DarkRed; 
     e.Row.ForeColor = System.Drawing.Color.White; 
    } 
} 

또는 다른 방법을 쓰기 :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.Cells[8].Text.Equals("TextToMatch")) 
    { 
     e.Row.Attributes.CssStyle.Value = "background-color: DarkRed; color: White"; 
    } 
} 
0

또는 행 DataItem을 클래스로 캐스팅 한 다음 클래스 속성을 기반으로 조건을 추가 할 수 있습니다. 다음은 행을 TimetableModel이라는 클래스/모델로 변환하는 예제입니다. if 문에서 모든 클래스 필드/속성에 액세스 할 수 있습니다.

protected void GridView_TimeTable_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        var tt = (TimetableModel)(e.Row.DataItem); 
        if (tt.Unpublsihed) 
         e.Row.BackColor = System.Drawing.Color.Red; 
        else 
         e.Row.BackColor = System.Drawing.Color.Green; 
       } 
      } 
     } 
관련 문제