Re: Set Column Width for MSHFlexgrid

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



*** Repaired for top-down posting... see answer below ***

> > > I have an MSHFlexGrid, and i would like to set the width for one
> > > of the column to the size of the object(MSHFlexGrid).
> >
> > I'm not sure what you are trying to do here. Are you attempting to
make
> > one specified column be the same width at the "working" area of the
> > grid? In other words, are you trying to have a specific column fill
the
> > grid's display area completely? If so, what about if there are fixed
> > columns? Do you want the column to fill the non-fixed width of the
> > control?
>
> In my MSHflexgrid, i am using the method:
>
> set_colwidth (int, int)
> Let s say i have only one column and i want to set the column width to
the
> same width as the MSHFlexgrid itself
>
> in the second parameter, i have to add the col width i want but i
have some
> difficulty to set the column width because the measure are in pixel
>
> MyHSFlexgrid.width=640
> MyHSFlexgrid.set_colwidth(0,MyHSFlexgrid.width) => result: column is
smaller
>
> hope this is clear

No, it still isn't completely clear to me. You haven't defined if you
can have more than one column in your grid. Also, you didn't address the
FixedCol question. Anyway, I am feeling generous this morning, so I took
a stab at developing some code which I think will do what I think you
want. Copy the code below into the code window of the form on which your
FlexGrid is located. To use the Sub, simply pass it the column you want
to enlarge to the width of the grid. First, however, I need to explain a
couple of things. First, instead of adding extra code to test for, and
adjust around, the vertical scrollbar either being in the grid or not
(depending on how many rows and columns there are), I have chosen a
kludge type method to get around the problem. The GetClientRect obtains
the area of the grid that is not occupied by scrollbars. I simply expand
the specified column to the width of the grid and test this column width
against the client area after the expansion. If the column width is
greater than the now current client area, then a vertical scrollbar was
introduced and I run the adjustment calculation a second time to correct
for it. The Adjustment variable I introduced seems to be necessary to
make things look nice depending on whether the last column is selected
or not and what the BorderStyle is. Remember, this is a quickly
developed piece of code, so you should test it out to make sure I got
everything right. And one last thing... I don't do database programming,
so I don't use the Hierarchical FlexGrid (which your code samples
indicate you are using), so I haven't tested this code out against that
control. However, I have tested it against the normal FlexGrid and it
seems to work fine for that control. I am assuming it will also work
against your Hierarchical FlexGrid control too. If not, would you please
let me know. Finally, the name of the FlexGrid is hard-coded in the Sub
directly (it appears only once in the With statement), so you can change
it if needed.

Rick - MVP

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetClientRect _
Lib "user32" _
(ByVal hWnd As Long, _
lpRect As RECT) As Long

Private Sub SetColumnToFullWidth(ColNumber As Long)
Dim X As Long
Dim Difference As Long
Dim Adjustment As Long
Dim GridAreaWidth As Double
Dim NewColumnWidth As Double
Dim FixedColWidth As Double
Dim GridArea As RECT
With MyHSFlexgrid
If ColNumber > .FixedCols And ColNumber < .Cols Then
For X = 0 To .FixedCols - 1
FixedColWidth = FixedColWidth + .ColWidth(X)
Next
Call GetClientRect(.hWnd, GridArea)
GridAreaWidth = GridArea.Right - GridArea.Left '- 1
NewColumnWidth = Int((GridAreaWidth - ScaleX(FixedColWidth, _
vbTwips, vbPixels)))
Adjustment = 1 - .BorderStyle
.ColWidth(ColNumber) = ScaleX(NewColumnWidth + _
Adjustment, vbPixels, vbTwips)
Call GetClientRect(.hWnd, GridArea)
If GridArea.Right - GridArea.Left - 1 < GridAreaWidth Then
GridAreaWidth = GridArea.Right - GridArea.Left - 1
NewColumnWidth = Int((GridAreaWidth - ScaleX(FixedColWidth, _
vbTwips, vbPixels)))
End If
.ColWidth(ColNumber) = ScaleX(NewColumnWidth + _
Adjustment, vbPixels, vbTwips)
.LeftCol = ColNumber
Else
' A bad column number was specified, handle error here
MsgBox "An invalid ColNumber argument was specified!", _
vbExclamation, "Invalid ColNumber Argument"
End If
End With
End Sub

.



Relevant Pages

  • Re: Set Column Width for MSHFlexgrid
    ... >> In my MSHflexgrid, ... > can have more than one column in your grid. ... > so I don't use the Hierarchical FlexGrid (which your code samples ... > Dim Difference As Long ...
    (microsoft.public.vb.controls)
  • Re: MSHFlexGrid
    ... This grid is only for displaying data. ... I don't use the hierarchial form of the Flexgrid (MSHFlexgrid), ... Dim SaveRow As Long ... ' Set the rest of the range to highlight ...
    (microsoft.public.vb.general.discussion)
  • Re: DataGrid problem thats very difficult. A Challenge for someone
    ... rebind the data. ... I move the CurrentRowIndex off the row that I want to delete by ... when the grid is redrawn it finds the currentrowindex is ... Dim iwkNoOfRowsSelected As Integer ...
    (microsoft.public.dotnet.framework)
  • Re: Allow user to select a Row in MSHFlexGrid control
    ... Dim rsPatient As ADODB.Recordset ... Dim rsRecCount As Integer ' For counting records in a record set ... Private Sub MSHFlexGrid1_Click ... select a row from the grid and based on that selection, ...
    (microsoft.public.vb.controls)
  • Re: How to use one datagrid to update/insert into another
    ... dgMassUpdate binds the query that holds all the fields that are ... RobinS wrote: ... Is dbMassUpdate your data grid that you are inserting their ... Dim cmdParam As SqlParameter ...
    (microsoft.public.dotnet.languages.vb)