2011-12-16 130 views
3

當我使用AutoGenerateColumns屬性爲AutoGenerateColumns =「true」時,在設置gridview的寬度時出現問題。而gridview是代碼後面的數據綁定。如果我使用gridview1.columns(0).width它會引發錯誤。當AutoGenerateColumns =「true」時動態設置gridview列的寬度

而GridView1.Columns.Count始終爲零,因爲網格視圖是數據綁定。

在的.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

提供全面`RowDataBound`方法體。你用'If`子句檢查了什麼? – 2011-12-16 08:53:49

+0

@YuriyRozhovetskiy對不起,它被錯誤地添加。謝謝。 – 2011-12-16 09:18:01

回答

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呈現一個表。所以細胞將自己調整到最長的內容。

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的列寬呈現在瀏覽器中,我們設置每列的寬度。我在兩個項目中使用了兩個方法 - 一個是通過將綁定字段與AutoGenerateColumns =「false」並且另一個通過設置AutoGenerateColumns =「true」 - 分別在兩個項目中,然後當頁面在瀏覽器中呈現時,我使用「View Source 「瀏覽器的功能,然後意識到兩種類型的主要區別是什麼。所不同的是: -

style="table-layout:fixed;" 

我還增加了以下線在我的.aspx頁面中的GridView的標籤: -

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> 

在這這種情況下只需設置單元格的寬度,以特定的值,並設置細胞包裹爲False

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 
相關問題