DataGrid Column Headers and Size

  • Thread starter Thread starter lliang27
  • Start date Start date
L

lliang27

Hi List,

I use the folloiwng code to fill up a DataGrid with one table's data
from SQL:

fillGrid.SetDataBinding(m_DataSet, tableName)

My questions are:
1. How can i change the column headers instead of using the names from
the database columns?
2. How can I autofit each column?

Thank you for any help.
 
Using table styles, you can affect many changes to the column headers and
the column headers and column data.

refer to DataGridTableStyle class

As for "autofit", a site that I find most helpful is:
http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx#44

From that site, I have come up with a helper class that does all the work
for me.


Public Class dbgHelper

Public Shared Sub DataGrid_SizeChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

DataGrid_SizeChanged_Equal(sender, e)

End Sub

Public Shared Sub DataGrid_SizeChanged_Equal(ByVal sender As Object, ByVal
e As System.EventArgs)

Dim dbg As DataGrid
Dim iWidth As Decimal

dbg = CType(sender, DataGrid)

If dbg.TableStyles.Count > 0 Then

SizeEqually(dbg.TableStyles(0))

End If

End Sub

Public Shared Sub DataGrid_SizeChanged_Fill(ByVal sender As Object, ByVal
e As System.EventArgs)
Dim dbg As DataGrid
Dim iWidth As Decimal

Try
dbg = CType(sender, DataGrid)

If dbg.TableStyles.Count > 0 Then

For col As Integer = 0 To
dbg.TableStyles(0).GridColumnStyles.Count - 1

If col = dbg.TableStyles(0).GridColumnStyles.Count - 1 Then
'Last Column, at least fill the width

If IsScrollBarVisible(dbg) Then
iWidth -= SystemInformation.VerticalScrollBarWidth
End If

dbg.TableStyles(0).GridColumnStyles(col).Width =
dbg.Size.Width - iWidth

Else

AutoSizeCol(dbg, col)

iWidth = iWidth + dbg.TableStyles(0).GridColumnStyles(col).Width
End If

Next

End If

dbg.Invalidate()

Catch ex As Exception

End Try
End Sub

Public Shared Sub AutoSizeCol(ByVal dbg As DataGrid, ByVal col As Integer)

Dim width As Decimal = 0
Dim numRows As Integer
Dim ColSize As Decimal
Dim RowColsize As SizeF
Dim g As Graphics
Dim sf As StringFormat
Dim colStyles As GridColumnStylesCollection

Try
numRows = CType(dbg.DataSource, DataTable).Rows.Count
Catch ex As Exception

Try
numRows = dbg.DataSource.Count
Catch ex1 As Exception
Return
End Try

End Try

colStyles = dbg.TableStyles(0).GridColumnStyles

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

'ColSize = g.MeasureString( _
' dbg.TableStyles(0).GridColumnStyles(col).HeaderText.ToString, _
' dbg.Font, _
' 500, _
' sf)

width = dbg.GetCellBounds(0, col).X
ColSize = width

For i As Integer = 0 To numRows - 1

RowColsize = g.MeasureString(dbg(i, col).ToString, dbg.Font, 500, sf)

If (RowColsize.Width > width) AndAlso (RowColsize.Width > ColSize)
Then
width = RowColsize.Width
End If

Next

g.Dispose()

colStyles(col).Width = width '+ 8 '; // 8 is for leading and trailing
padding

End Sub

Private Shared Sub SizeEqually(ByVal ts As DataGridTableStyle)

Dim headerWidth As Integer
Dim nCols As Integer

Try

If ts Is Nothing OrElse ts.DataGrid Is Nothing Then
Return
End If

'get a reference to the GridColumnStyles
Dim colStyles As GridColumnStylesCollection = ts.GridColumnStyles

nCols = ts.GridColumnStyles.Count

Try
headerWidth = ts.DataGrid.GetCellBounds(0, 0).X
Catch ex As Exception
headerWidth = 20
End Try

'get the target width
Dim width As Integer = ts.DataGrid.Size.Width - headerWidth - 4 ' the
4 handles the borders

If IsScrollBarVisible(ts.DataGrid) Then
width -= SystemInformation.VerticalScrollBarWidth
End If

Dim lastColIndex As Integer = nCols - 1

'set to zero so horizontal scroll does not show as your size
'frees up room in case leading cols grod during resize and try to
flash teh HScroll
colStyles(lastColIndex).Width = 0

Dim i As Integer
Dim colWidth As Integer = width / nCols
For i = 0 To lastColIndex - 1
colStyles(i).Width = colWidth
Next

'make last col fit whatever is left (handles pixels lost to int
division
colStyles(lastColIndex).Width = width - (nCols - 1) * colWidth

Catch ex As Exception

End Try


End Sub

Private Shared Function IsScrollBarVisible(ByVal aControl As Control) As
Boolean
Dim c As Control
For Each c In aControl.Controls
If c.GetType() Is GetType(VScrollBar) Then
If c.Visible Then
Return True
Else
Return False
End If
End If
Next
Return False
End Function

End Class
 
Back
Top