2011-12-16 2 views
3

AutoGenerateColumns = "true"로 AutoGenerateColumns 속성을 사용할 때 gridview의 너비를 설정하는 데 문제가 있습니다. 그리고 gridview는 코드 뒤에 databind입니다. gridview1.columns (0) .width를 사용하고 있으면 오류가 발생합니다.AutoGenerateColumns = "true"일 때 gridview 열의 너비를 동적으로 설정하십시오.

격자보기가 databind이기 때문에 GridView1.Columns.Count는 항상 0입니다. .ASPX에서

: -

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"> 
</asp:GridView> 

코드에서

Dim strCon As New SqlConnection("Data Source=myDataSource;Initial Catalog=myDataBaseName;Persist Security Info=True;User ID=GKRANJAN;Password=abcdef") 
     Dim da As New SqlDataAdapter("Select * from myTableName", strCon) 
     Dim ds As New DataSet 
     da.Fill(ds) 
     GridView1.DataSource = ds 
     GridView1.DataBind() 

뒤에은 따라서 myTableName는 더 많은 열을 가지고 있으며, 나는 그들이 내 경우에는 차이가 있기 때문에 BoundFiled 통해를 추가 해달라고.

는 GridView1_RowDataBound에서 내가 사용 : -

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
Dim cell As TableCell = e.Row.Cells(0) 
      cell.Width = New Unit("200px") 
    End Sub 

그러나 그것은 나를 위해 작동하지 않을 수 있습니다. 도와주세요 !!

감사합니다.

+0

에 특정 값과 설정 셀 랩에 셀 폭을 설정합니다. 'If' 조항으로 무엇을 체크인합니까? –

+0

@YuriyRozhovetskiy 죄송합니다. 실수로 추가되었습니다. 감사. –

답변

2

의 단지 오타 (또는 당신이 그것을 생략)하지만 RowDataBound 부분에 코드가 IF 부분을 누락 된 경우 내가 아는 해달라고 ..

그러나 개봉 바른 길에. 나, 이것과 같은 것을 사용하고 항상 작동합니다.

Protected Sub gvData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvData.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
      e.Row.Cells(0).Width = New Unit("200px") 
      e.Row.Cells(1).Width = New Unit("500px") 
    End If 
End Sub 

그러나 gridview는 테이블로 렌더링됩니다. 따라서 셀은 가장 긴 내용으로 자체 크기가 조정됩니다.

+0

나도 마찬가지였다. 테이블 크기가 조정되고 수행 된 모든 작업이 쓸모 없게됩니다. @ noisyass2 –

+0

당신은 언제나 테이블의 너비를 고정시키기 위해 CSS를 사용할 수 있습니다. 또는 약간의 js 마술 .. – noisyass2

3

알겠습니다. 다음은

는 .aspx 페이지입니다 : -

<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" 

     style="table-layout:fixed;" Width="1000px">   

     <!-- Mind the above two lines to make this trick effective you must have to use both properties as is; --> 

     </asp:GridView> 
    </div> 
    </form> 
</body> 

그리고 이것은 뒤에 코드입니다 : - 사실

Imports System.Data.SqlClient 
Partial Public Class _Default 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Dim strCon As New SqlConnection("Data Source=myDataSource;Initial Catalog=myDataBaseName;Persist Security Info=True;User ID=GKRANJAN;Password=abcdef") 
     Dim da As New SqlDataAdapter("Select * from myTableName", strCon) 
     Dim ds As New DataSet 
     da.Fill(ds) 
     GridView1.DataSource = ds 
     GridView1.DataBind() 
    End Sub 

    Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
     If e.Row.RowType = DataControlRowType.Header Then 

      'For first column set to 200 px 
      Dim cell As TableCell = e.Row.Cells(0) 
      cell.Width = New Unit("200px") 

      'For others set to 50 px 
      'You can set all the width individually 

      For i = 1 To e.Row.Cells.Count - 1 
       'Mind that i used i=1 not 0 because the width of cells(0) has already been set 
       Dim cell2 As TableCell = e.Row.Cells(i) 
       cell2.Width = New Unit("10px") 
      Next 
     End If 
    End Sub 
End Class 

우리는 우리가 설정으로 폭이 브라우저에서 렌더링 다음의 gridview 열을 boundfields를 사용할 때 각 열의 너비. 두 프로젝트에서 두 가지 방법을 사용했습니다. 하나는 AutoGenerateColumns = "false"로 바인딩 된 필드를 취하고 다른 하나는 AutoGenerateColumns = "true"로 설정하는 것입니다. 두 프로젝트에서 개별적으로 수행 한 다음 페이지를 브라우저에서 렌더링 할 때 "View Source "브라우저의 기능을 이해하고 두 가지 유형의 주된 차이점은 무엇인지 깨닫게되었습니다. 나는 또한있는 gridview 태그에 내 .aspx 페이지에 아래 라인을 추가

style="table-layout:fixed;" 

: - - :

style="table-layout:fixed;" Width="1000px" 

그리고 지금은 잘 작동하고 같은 차이입니다.

모두에게 감사합니다 !!

2

그리드를 고정 모드로 만들려고하지 않는 경우 (예 : 많은 수의 열이 있기 때문에 오버플로 동작이 예상 됨) 위의 해결 방법은 style = "table-layout : fixed;"입니다. 적절한 것이 아닙니다.

아래의 시나리오 참조 :이 같은 경우

<div style="overflow:auto;"> 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView> 
</div> 

은 전체`RowDataBound` 방법 본문을 제공 거짓

Protected Sub gvData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvData.RowDataBound 
    If e.Row.RowType = DataControlRowType.DataRow Then 
     e.Row.Cells(0).Width = New Unit("200px") 
     e.Row.Cells(0).Wrap = false 
    End If 
End Sub 
관련 문제