Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / September 2005

Tip: Looking for answers? Try searching our database.

DataGrid Column Headers and Size

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lliang27@yahoo.com - 30 Sep 2005 14:55 GMT
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.
AMDRIT - 30 Sep 2005 17:01 GMT
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

> Hi List,
>
[quoted text clipped - 9 lines]
>
> Thank you for any help.
lliang27@yahoo.com - 30 Sep 2005 18:08 GMT
Thank you so much.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.