2011-12-16 136 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 
相关问题