Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADO DataGrid: Sizing and getting values of cells 3

Status
Not open for further replies.

perplexd

Programmer
May 9, 2002
154
0
0
US
These are simple problems of a beginner...

I have an ADO DataGrid, into which I have loaded a number of records from a database. The datagrid is effectively 'bound' to the recordset at run-time through specifying a new value for the dataMember parameter.

I have two questions:
1. How do I resize the columns at run-time
, so that they fit in the entire length of data or does this have to be done in advance? Do I resize the form in the same manner should that also need doing?

2. I need to get the value of Field1 for whichever record is selected when a cmdButton is pressed. (This has to work whichever field within the reocrd is selected when the button is pressed.) How do I go about this? I'm sure it must be possible as there are plenty of row/col functions - its just knowing what each one does and what type of value it returns!

Thank-you in advance!
 
To resize columns at runtime:

With MSHFlexGrid1
' set grid's column widths
.ColWidth(0) = -1 'default
.ColWidth(1) = 3000
.ColWidth(2) = 2000
.ColWidth(3) = 0
End With

For your question 2, try:

Private Sub MSHFlexGrid1_Click()

With MSHFlexGrid1
.Row = .MouseRow
.Col = 0 'or whichever column your Field1 is in
SelectedItem = .Text
End With

End Sub

Hope this helps.

Ralph
 
With resizing the column width at run-time is it possible to get the columns to automatically size to the data which is loaded in, instead of having to "manually" write in values?

For some reason the .MouseRow function is continuously returning zero, otherwise this code would work. Have you any idea what might be causing this? (The grid is not editable, if that has anything to do with it)

Thanks again
 
OK, you can simply leave out the .Row line, then it works! Thank you

Does anyone have any suggestions about resizing the column?
 
One approach that you might try to automatically set the width of each column to fit the data is as follows:
The following example sets all of the columns of the grid

Dim MaxWidth() as Long
Dim CellVal as String
Dim Col_Idx as Integer
Dim Row_Idx as Integer

' Set Up Array with an entry for each column
' Init Array Values to 0 (its default, but I do it anyway)
ReDim MaxWidth(grdGrid.Cols)
For Col_Idx = 0 to grdGrid.Cols - 1
MaxWidth(Col_Idx) = 0
Next Col_Idx

' Loop thru each col in each row, to find the max length
For Row_Idx = 0 to grdGrid.Rows - 1
For Col_Idx = 0 to grdGrid.Cols - 1
CellVal = grdGrid.TextMatrix(Row_Idx, Col_Idx)
If (Me.TextWidth(CellVal) > MaxWidth(Col_Idx)) Then
MaxWidth(Col_Idx) = Me.TextWidth(CellVal)
End If
Next Col_Idx
Next Row_Idx

' Now Set the Column Width (add a little extra)
For Col_Idx = 0 to grdGrid.Cols - 1
grdGrid.ColWidth(Col_Idx) = MaxWidth(Col_Idx) + 120
Next Col_Idx

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top