CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Posts
    2

    Help Autosizing columns of the datagrid

    Hello All,
    I'm trying to autosize the columns of my datagrid using the following code, there seems to be a bug that I can't get rid of.


    Public Sub AutoSizeCol(ByVal dGrid As DataGrid, ByVal strTableName As String)

    Dim width As Decimal
    Dim sf As StringFormat
    Dim g As Graphics
    Dim numRows As Integer
    Dim numCols As Integer
    Dim size As SizeF
    Dim I, J As Integer



    numRows = CType(dGrid.DataSource.tables(strTableName), DataTable).Rows.Count
    numCols = CType(dGrid.DataSource.tables(strTableName), DataTable).Columns.Count


    myGridColumnStylesCollection = dGrid.TableStyles(0).GridColumnStyles

    g = Graphics.FromHwnd(dGrid.Handle)
    sf = New StringFormat(StringFormat.GenericTypographic)


    For I = 0 To numCols

    For J = 0 To numRows

    size = g.MeasureString(dGrid(J, I), dGrid.Font, 500, sf)
    If (size.Width > width) Then width = size.Width
    dGrid.TableStyles(strTableName).GridColumnStyles(I).Width = CType(width, Integer)
    Next
    Next

    g.Dispose()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    DataSet1.ReadXml("..\XML\ConditionCode.xml")
    dgrdConditionCode.DataMember = DataSet1.Tables(0).TableName
    AutoSizeCol(dgrdConditionCode, DataSet1.Tables(0).TableName)
    End Sub
    I get the following error from the following line:
    dGrid.TableStyles(strTableName).GridColumnStyles(I).Width = CType(width, Integer) --> object Reference Not set to instant of object


    Any help would be greatly appreciated.

  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            AutoSizeTable()'autosize all cols
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            AutoSizeCol(1)'autosize 1st col
        End Sub
    
    
    Public Sub AutoSizeTable()
            Dim numCols As Integer
            numCols = CType(dg.DataSource, DataTable).Columns.Count
            Dim i As Integer
            i = 0
            Do While (i < numCols)
                AutoSizeCol(i)
                i = (i + 1)
            Loop
    
        End Sub
    
        Public Sub AutoSizeCol(ByVal col As Integer)
    
            Dim width As Single
            width = 0
            Dim numRows As Integer
            numRows = CType(dg.DataSource, DataTable).Rows.Count
            Dim g As Graphics
            g = Graphics.FromHwnd(dg.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(dg(i, col).ToString, dg.Font, 500, sf)
                If (size.Width > width) Then
                    width = size.Width
                End If
                i = (i + 1)
    
            Loop
            g.Dispose()
            dg.TableStyles("customers").GridColumnStyles(col).Width = CType(width, Integer)
    
        End Sub
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  3. #3
    Join Date
    Mar 2003
    Posts
    2
    Thank you for the reply Iouri. However, I am using multiple datagrids in my program and would like to create a generic function for all datagrids.
    Could you help me fix my code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured