根据表格内容,自动调整VB.NET中Datagrid控件单元格宽度

类别:.NET开发 点击:0 评论:0 推荐:

       用Datagrid控件显示表格数据,当表格内容过多时,Datagrid控件单元格宽度并不足以用来完全显示表格中的数据内容。而只有自己每次去拉动分界线,来进行调整。当出现表格字段比较多时,这样做就显得很麻烦。
       以下是用程序根据表格内容来自动调整Datagrid控件单元格宽度。
       先定义一个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

       只要使用以上两个Sub过程就可以达到你想要的结果了!

       一个完整的实例如下:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connString As String
        connString = "Provider=Microsoft.JET.OLEDB.4.0;data source=northwind.mdb"
        Dim sqlString As String
        sqlString = "SELECT * FROM customers"
        Dim dataAdapter As OleDbDataAdapter
        dataAdapter = Nothing
        Dim _dataSet As DataSet
        _dataSet = Nothing

        Try

            Dim connection As OleDbConnection
            connection = New OleDbConnection(connString)

            dataAdapter = New OleDbDataAdapter(sqlString, connection)

            _dataSet = New DataSet
            dataAdapter.Fill(_dataSet, "customers")
            connection.Close()
        Catch ex As Exception
            MessageBox.Show("出错!")
            Me.Close()
            Return
        End Try
        Dim tableStyle As DataGridTableStyle
        tableStyle = New DataGridTableStyle
        tableStyle.MappingName = "customers"

        dataGrid1.TableStyles.Clear()
        dataGrid1.TableStyles.Add(tableStyle)
        dataGrid1.DataSource = _dataSet.Tables("customers")
    End Sub

    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        AutoSizeTable()
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        AutoSizeCol(1)
    End Sub

效果如图示:
1窗体导入时


2单击[调整第一列宽]后


3单击[调整所有列宽]后


  

本文地址:http://com.8s8s.com/it/it41562.htm