C
cj
I was looking over some of my 2003 code today (see below) that loads a
foxpro table via oledb connection.
I used a sub "autosizecolumns" I found on the web but I never quite
understood why they said the style name had to be the same as the table
name. Perhaps the are refering to ts1.mappingname must be the same as
the table name (datagrid1.datamember) because I've read that that is how
the datagrid associates a style with what it's displaying. Is that it?
Also, I can't figure out how I pulled off using
system.drawing.color.fromname("info"). I think that was something I
came up with but it makes no sense now. Can someone enlighten me?
Lastly all this is probably a mute point now as I see we have a new
control in 2005 (datagridview). So I started typing in this code from
scratch in 2005 to see how it works. I got to datagrid1.setdatabinding
only to find it doesn't exist. How do I attach the datatable in the
dataset to the datagrid in 2005?
mySQLCommand.CommandText = fields & from & where & order
mySQLCommand.Connection = myFoxProConnection
myDataAdapter.SelectCommand = mySQLCommand
myDataAdapter.Fill(ds, tableName)
DataGrid1.SetDataBinding(ds, tableName)
Dim ts1 As New DataGridTableStyle
ts1.ReadOnly = True
ts1.RowHeadersVisible = False
ts1.ColumnHeadersVisible = False
ts1.AlternatingBackColor = System.Drawing.Color.FromName("info")
ts1.MappingName = DataGrid1.DataMember
DataGrid1.TableStyles.Add(ts1)
AutoSizeColumns(DataGrid1)
' **********************************************************
' Sizes all columns of a datagrid optimally.
'
' Parameters:
' TempDG - The datagrid that owns the column
' Notes:
' Ensure that a style exists for the table you are
' viewing and that the style name is the same as the table name.
' **********************************************************
Public Sub AutoSizeColumns(ByRef TempDG As DataGrid)
Dim iCount As Integer
For iCount = 0 To (TempDG.VisibleColumnCount() - 1)
AutoSizeCol(iCount, TempDG)
Next
End Sub
' **********************************************************
' Sizes a column of a single datagrid column optimally.
'
' Parameters:
' iCol - The column to resize
' TempDG - The datagrid that owns the column
' Notes:
' Make sure the column is a valid one. Also, ensure
' that a style exists for the table you are viewing and that
' the style name is the same as the table name.
' found on experts-exchange.com
' **********************************************************
Public Sub AutoSizeCol(ByVal iCol As Integer, ByRef TempDG As DataGrid)
Dim iMaxWidth, iTemp As Integer
Dim iNumRows As Integer = TempDG.BindingContext(TempDG.DataSource,
TempDG.DataMember).Count
Dim G As Graphics
Dim sf As StringFormat = New
StringFormat(StringFormat.GenericTypographic)
Dim sTemp, sTableName As String
Dim Size As SizeF
' Get a graphics handle
G = Me.CreateGraphics
' Loop thru all rows of this column. Get length of longest string
If iNumRows > 0 Then
For iTemp = 0 To (iNumRows - 1)
sTemp = TempDG(iTemp, iCol).ToString
Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
Size.Width += 10
If Size.Width > iMaxWidth Then iMaxWidth = Size.Width
Next iTemp
End If
sTableName = TempDG.DataMember
' Lastly, consider length of column header name
sTemp =
TempDG.TableStyles(sTableName).GridColumnStyles(iCol).HeaderText()
Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
Size.Width += 10
If Size.Width > iMaxWidth Then iMaxWidth = Size.Width
' Modify the width of the grid column style
TempDG.TableStyles(sTableName).GridColumnStyles(iCol).Width = iMaxWidth
MsgBox("Width of Column " & iCol & " is " & iMaxWidth,
MsgBoxStyle.Information, "FYI")
G.Dispose()
End Sub
foxpro table via oledb connection.
I used a sub "autosizecolumns" I found on the web but I never quite
understood why they said the style name had to be the same as the table
name. Perhaps the are refering to ts1.mappingname must be the same as
the table name (datagrid1.datamember) because I've read that that is how
the datagrid associates a style with what it's displaying. Is that it?
Also, I can't figure out how I pulled off using
system.drawing.color.fromname("info"). I think that was something I
came up with but it makes no sense now. Can someone enlighten me?
Lastly all this is probably a mute point now as I see we have a new
control in 2005 (datagridview). So I started typing in this code from
scratch in 2005 to see how it works. I got to datagrid1.setdatabinding
only to find it doesn't exist. How do I attach the datatable in the
dataset to the datagrid in 2005?
mySQLCommand.CommandText = fields & from & where & order
mySQLCommand.Connection = myFoxProConnection
myDataAdapter.SelectCommand = mySQLCommand
myDataAdapter.Fill(ds, tableName)
DataGrid1.SetDataBinding(ds, tableName)
Dim ts1 As New DataGridTableStyle
ts1.ReadOnly = True
ts1.RowHeadersVisible = False
ts1.ColumnHeadersVisible = False
ts1.AlternatingBackColor = System.Drawing.Color.FromName("info")
ts1.MappingName = DataGrid1.DataMember
DataGrid1.TableStyles.Add(ts1)
AutoSizeColumns(DataGrid1)
' **********************************************************
' Sizes all columns of a datagrid optimally.
'
' Parameters:
' TempDG - The datagrid that owns the column
' Notes:
' Ensure that a style exists for the table you are
' viewing and that the style name is the same as the table name.
' **********************************************************
Public Sub AutoSizeColumns(ByRef TempDG As DataGrid)
Dim iCount As Integer
For iCount = 0 To (TempDG.VisibleColumnCount() - 1)
AutoSizeCol(iCount, TempDG)
Next
End Sub
' **********************************************************
' Sizes a column of a single datagrid column optimally.
'
' Parameters:
' iCol - The column to resize
' TempDG - The datagrid that owns the column
' Notes:
' Make sure the column is a valid one. Also, ensure
' that a style exists for the table you are viewing and that
' the style name is the same as the table name.
' found on experts-exchange.com
' **********************************************************
Public Sub AutoSizeCol(ByVal iCol As Integer, ByRef TempDG As DataGrid)
Dim iMaxWidth, iTemp As Integer
Dim iNumRows As Integer = TempDG.BindingContext(TempDG.DataSource,
TempDG.DataMember).Count
Dim G As Graphics
Dim sf As StringFormat = New
StringFormat(StringFormat.GenericTypographic)
Dim sTemp, sTableName As String
Dim Size As SizeF
' Get a graphics handle
G = Me.CreateGraphics
' Loop thru all rows of this column. Get length of longest string
If iNumRows > 0 Then
For iTemp = 0 To (iNumRows - 1)
sTemp = TempDG(iTemp, iCol).ToString
Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
Size.Width += 10
If Size.Width > iMaxWidth Then iMaxWidth = Size.Width
Next iTemp
End If
sTableName = TempDG.DataMember
' Lastly, consider length of column header name
sTemp =
TempDG.TableStyles(sTableName).GridColumnStyles(iCol).HeaderText()
Size = G.MeasureString(sTemp, TempDG.Font, 500, sf)
Size.Width += 10
If Size.Width > iMaxWidth Then iMaxWidth = Size.Width
' Modify the width of the grid column style
TempDG.TableStyles(sTableName).GridColumnStyles(iCol).Width = iMaxWidth
MsgBox("Width of Column " & iCol & " is " & iMaxWidth,
MsgBoxStyle.Information, "FYI")
G.Dispose()
End Sub