2012-05-31 5 views
0

I이 들어 그 안에서asp.net의 gridview에

Protected Sub OnRowCreated(sender As Object, e As GridViewRowEventArgs) 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     For Each cell As TableCell In e.Row.Cells 
      cell.Font.Size = 10 
      cell.HorizontalAlign = HorizontalAlign.Center 
     Next 
    End If 

End Sub 

과 같은 코드를 사용하여 모든 셀에 적용 할 때 나는 일부 서식을 추가 할 수 있어요 cumpnl, 손익, 세 개의 열 날짜를 가지고있는 gridview .. 다음 루프 어떻게하면 pnl 및 cumpnl 열의 셀만 참조 할 수 있습니까? 어쨌든 columnheader 이름이나 인덱스를 참조하는 셀이 표시되지 않습니다.

업데이트 : 이제 열의 값의 형식이지만 값이 실수를 범 유지하는 세포에 따라 데 ForeColor의 설정 재설정 할 수있는 RowDataBound 이벤트를 사용하여

("의 형식이 잘못 입력 문자열") . 또한 열 인덱스를 하드 코딩하고 있습니다. 동적으로 각 루프의 열 헤더 이름

Protected Sub gvDataRetrieval_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDataRetrieval.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     For i As Integer = 1 To 2 
      e.Row.Cells(i).Text = FormatCurrency(e.Row.Cells(i).Text, 2).ToString() 
      If Double.Parse(e.Row.Cells(i).Text) < 0 Then e.Row.Cells(i).ForeColor = Drawing.Color.Red 
     Next 
End If 
End Sub 

답변

0

외부에 따라 열 인덱스를 얻을 수있는 방법이 필요합니다 :

e.Row.Cells(index) 

예를. 헤더 툴팁을 추가 :

Protected Sub Grid1_RowCreated(ByVal sender As Object, ByVal e As GridRowEventArgs) 
     //setting row cells 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     For i As Integer = 1 To e.Row.Cells.Count 
     If i = 1 Then 
      e.Row.Cells(i).Font.Size= 8 
     ELSE 
      e.Row.Cells(i).Font.Size= 12 
     END IF 
     NEXT 
    End If 
    End Sub 
+0

C#에서 카필의 코드 그것의 인덱스) 및 해당 열 서식을 적절하게 – dinotom

+0

당신은 그리드 및 할 수있는 템플릿 열을 추가 할 수 있습니다 : e.Row.FindControl ("IDofyourcontrol") –

+0

아마 내가 명확하지 않다, 각 셀을 반복 할 수있게하려면 datarow에서, 그리고 오직 열의 형식에 따라 셀을 변경해야합니다. 그렇게하려면 Tes에 대한 방법이 필요합니다. t 반복 동안 셀이있는 열. something cell.column ("pnl") 다음 ForeColor = Color.Red – dinotom

0

내가 열심히 동적 열 머리글을 테스트 할 수 있도록하려면하지만, 그 컬럼에 대한 참조를 코딩 (및 검색 할 수 있습니다 물론

 private void DataGrid1_RowCreated(Object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      for (int i = 1; i < e.Row.Cells.Count; i++) 
      { 
       if (i == 1) { e.Row.Cells[i-1].Font.Bold = true; } else { e.Row.Cells[i-1].Font.Bold = false; } 
      } 
     } 
    }