1个回答
展开全部
思路:先获取列中字段最大宽度,再设置DataGrid当前列的宽度。
用循环一列一列的设置,
for i = 0 to datagrid1.colums.Count
for j = 0 to datagrid1.Rows.Count
……比较Len,找到最大,赋值
next
next
............................
先定义一个Sub过程AutoSizeCol,用来调整Datagrid中的列宽。
Public Sub AutoSizeCol(ByVal col As Integer) ’当前列
Dim width As Single
width = 0
Dim numRows As Integer
numRows = CType(dataGrid1.DataSource, DataTable).Rows.Count ‘当前列的行数
Dim g As Graphics
g = Graphics.FromHwnd(dataGrid1.Handle)
Dim sf As StringFormat
sf = New StringFormat(StringFormat.GenericTypographic)
Dim size As SizeF
Dim i As Integer
i = 0
Do While (i < numRows)
size = g.MeasureString(dataGrid1(i, col).ToString, dataGrid1.Font, 500, sf)
If (size.Width > width) Then
width = size.Width ’得到字符最多的行的字符数
End If
i = (i + 1)
Loop
g.Dispose()
dataGrid1.TableStyles("customers").GridColumnStyles(col).Width = CType(width, Integer)
'应用到列
End Sub
下面就可以表格内容来定义整张表所有列的宽度了。
Public Sub AutoSizeTable()
Dim numCols As Integer
numCols = CType(dataGrid1.DataSource, DataTable).Columns.Count
Dim i As Integer
i = 0
Do While (i < numCols) '循环每一列
AutoSizeCol(i) ‘调用上面的函数
i = (i + 1)
Loop
End Sub
(转并注释) <br>
用循环一列一列的设置,
for i = 0 to datagrid1.colums.Count
for j = 0 to datagrid1.Rows.Count
……比较Len,找到最大,赋值
next
next
............................
先定义一个Sub过程AutoSizeCol,用来调整Datagrid中的列宽。
Public Sub AutoSizeCol(ByVal col As Integer) ’当前列
Dim width As Single
width = 0
Dim numRows As Integer
numRows = CType(dataGrid1.DataSource, DataTable).Rows.Count ‘当前列的行数
Dim g As Graphics
g = Graphics.FromHwnd(dataGrid1.Handle)
Dim sf As StringFormat
sf = New StringFormat(StringFormat.GenericTypographic)
Dim size As SizeF
Dim i As Integer
i = 0
Do While (i < numRows)
size = g.MeasureString(dataGrid1(i, col).ToString, dataGrid1.Font, 500, sf)
If (size.Width > width) Then
width = size.Width ’得到字符最多的行的字符数
End If
i = (i + 1)
Loop
g.Dispose()
dataGrid1.TableStyles("customers").GridColumnStyles(col).Width = CType(width, Integer)
'应用到列
End Sub
下面就可以表格内容来定义整张表所有列的宽度了。
Public Sub AutoSizeTable()
Dim numCols As Integer
numCols = CType(dataGrid1.DataSource, DataTable).Columns.Count
Dim i As Integer
i = 0
Do While (i < numCols) '循环每一列
AutoSizeCol(i) ‘调用上面的函数
i = (i + 1)
Loop
End Sub
(转并注释) <br>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询