Setting datagrid column width sets width of toplevel!! 
Author Message
 Setting datagrid column width sets width of toplevel!!

Hello,

I have an application with a few tabs, two of which have one
datagrid each.  I have two varieties of a function to compute
and set the desired widths of the columns. I use only one of
them at any time, switching between then by renaming them.

One works perfectly, except the calculation is simplistic
and does not give the widths I want. the other has twice as
complex computations, but otherwise interact with the
surrounding system making the same function calls in the
same order, only with different values of the resulting
widths. But with this function the entire application window
comes up just approximately 100 pixels wide.

The design width of the application window is 700.

Yes, the second function has one interaction more with the
environment: the statement

   Dim space As Integer = grid.Width

at the top of the function. In addition to that there might
be some implicit calls to support systems but on the surface
it should all be integer math on register values and
load/store register variables from/to memory.

Here are the two functions, first the one that fails:

' Set the width of the datagrid columns
' according to the width of the data
Sub SetColumnWidths(ByVal grid As DataGrid, _
                    ByVal list As DataTable)
    Dim phics As Graphics = grid.CreateGraphics()
    Dim style As New DataGridTableStyle()

    style.MappingName = list.TableName
    grid.TableStyles.Clear()
    grid.TableStyles.Add(style)

    ' Minus one for good measure:
    Dim space As Integer = grid.Width - 1
    Dim total As Integer = 0
    Dim n As Integer = 0
    Dim widths(12) As Integer ' knowing there are
                              ' no more than 6 columns

    Dim col As DataColumn
    For Each col In list.Columns
        Dim colName As String = col.ColumnName()
        Dim maxwidth As Integer = 100
        ' Take width of one blank space and add to
 ' the new width of the Column.
        Dim offset As Integer = Convert.ToInt32( _
     Math.Ceiling(phics.MeasureString( _
     " ", grid.Font).Width))

        Dim row As DataRow
        For Each row In list.Rows
            Dim field As String = row(colName).ToString
            Dim width As Integer = Convert.ToInt32( _
         Math.Ceiling(phics.MeasureString( _
                field, grid.Font).Width))
            If width > maxwidth Then
                maxwidth = width
            End If
        Next

        width = offset + maxwidth
        space -= width
        total += width
        widths(n) = width
        n += 1
    Next

    n = 0
    For Each col In list.Columns
        Dim width As Integer = widths(n)
        Dim share As Integer = ((space * width) / total)
        total -= width
        space -= share
        style.GridColumnStyles(col.ColumnName).Width _
     = width + share
        n += 1
    Next
End Sub

'And the "good" one:

' Set the width of the datagrid columns
' according to the width of the data
Sub GOOD_SetColumnWidths(ByVal grid As DataGrid, _
    ByVal list As DataTable)
    Dim phics As Graphics = grid.CreateGraphics()
    Dim style As New DataGridTableStyle()

    style.MappingName = list.TableName
    grid.TableStyles.Clear()
    grid.TableStyles.Add(style)

    Dim width As Integer = grid.Width

    Dim col As DataColumn
    For Each col In list.Columns
        Dim colName As String = col.ColumnName()
        Dim maxwidth As Integer = 100
        ' Take width of one blank space and add to
 ' the new width of the Column.
        Dim offset As Integer = Convert.ToInt32( _
     Math.Ceiling(phics.MeasureString( _
     " ", grid.Font).Width))

        Dim row As DataRow
        Dim field As String
        For Each row In list.Rows
            field = row(colName).ToString
            width = Convert.ToInt32(Math.Ceiling( _
         phics.MeasureString( _
  field, grid.Font).Width))
            If width > maxwidth Then
                maxwidth = width
            End If
        Next

        style.GridColumnStyles(colName).Width _
     = offset + maxwidth
    Next
End Sub

----
The width of the grid is 640, the width of the columns adds
to at most 622 in the "good" case. The bad function tries
to distribute the remaining 18 pixels among the columns
in shares proportional to their first-round widths.

The algorithm has one flaw, it does not account for the
width of the nameless column of "row handles", which appears
to be approximately 28 pixels wide. However, I can manually
resize the columns and make them as wide as I please, with
no effects on the toplevel window. Just to be sure I have
changed the "- 1" to "- 37" but the only effect was to make
the columns narrower, and so also the main application
window which became roughly 10 pixels narrower.

I don't know what it takes to reproduce this, I could
mention that the grids live inside panels at negative 'y'
coordinates to hide the ugly dark band across the top of the
grids. (What is this band for, and what is the normal way of
getting rid of it, by the way?)

Any comments?

Regards, Enrique



Tue, 13 Dec 2005 09:30:40 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Set Grid width to sum of Column Widths

2. DataGrid Setting Width of Columns to Fit Autofit.

3. Set column width of a datagrid

4. How do I Set Datagrid Column widths?

5. how to set the Column Width in dataGrid?

6. Setting column width in DataGrid

7. setting width on one column in a datagrid

8. Setting form width/height = image width/height

9. Setting Usercontrol (ocx) width at runtime wider than Width property of usercontrol

10. Setting Font and Column Width in Excel from Access

11. Set column widths in code?

12. setting column width

 

 
Powered by phpBB® Forum Software