问题描述
我在设置在GridView的宽度时,我使用的物业的AutoGenerateColumns到的AutoGenerateColumns =真的问题。而GridView控件是数据绑定在后面的code。如果我使用gridview1.columns(0).WIDTH它提升的错误。
I have a problem in setting the width of the gridview when i used the property AutoGenerateColumns to AutoGenerateColumns="true". And the gridview is databind in code behind. If i am using gridview1.columns(0).width it raise error.
而GridView1.Columns.Count始终为零,因为网格视图是数据绑定。
And the GridView1.Columns.Count is always zero because the grid view is databind.
在的.aspx: -
In .aspx: -
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
在code后面
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因为它们随在我的情况添加。
Hence myTableName has more columns and i dont like to add them through BoundFiled because they vary in my case.
在GridView1_RowDataBound我用: -
In GridView1_RowDataBound i used : -
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
但它不能为我工作。请帮助我!
But it could not work for me. Please help me!!
感谢所有!
推荐答案
我知道了。
下面是.aspx页: -
Below is the .aspx page: -
<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>
这背后是code: -
And this is the Code behind: -
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 =假,另一个通过设置的AutoGenerateColumns =真 - 两个项目分别然后当页面在浏览器中得到了呈现,我用查看源文件浏览器的功能,然后意识到,是在这两种类型的主要区别。所不同的是如: -
Actually when we use boundfields then gridview columns width renders in browser as we set the widths of each and every columns. I used two methods in two projects - that is one by taking bound fields with AutoGenerateColumns="false" and another by setting the AutoGenerateColumns = "true" - individually in two project and then when page got rendered in browser, i used "View Source" functionality of browser and then realized that what is the main difference in both types. The difference is as: -
style="table-layout:fixed;"
我还添加了以下线在我的.aspx页面的GridView标签: -
I also added the below lines in my .aspx page at gridview tag: -
style="table-layout:fixed;" Width="1000px"
而现在它的正常工作。
And now it's working fine.
感谢所有!!
这篇关于设置gridview的列宽动态时的AutoGenerateColumns =&QUOT;真&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!