SqueakinSweep
Programmer
Having seen many questions about the datagrid, especially relating to column styles. After all every grid you design realistically needs some design touches adding and the MS Datagrid is not the easiest control in the world to work with on this front.
Because of this, and partly as an exercise to myself to figure out a little about Datagrid (I generally use C1 TrueDBGrid), I designed a little grid class, which hopefully other forum users will add to and build on. Eventually we could end up with TekTips Grid...
When I get time and a few revisions I will add this as an FAQ. Please feel free to add suggestions or add to the source.
The control is a component class, inheriting the Datagrid
Usage
Build and add to toolbar, then drop onto a new form
Sample usage code below
Sweep
...if it works dont mess with it
Because of this, and partly as an exercise to myself to figure out a little about Datagrid (I generally use C1 TrueDBGrid), I designed a little grid class, which hopefully other forum users will add to and build on. Eventually we could end up with TekTips Grid...
When I get time and a few revisions I will add this as an FAQ. Please feel free to add suggestions or add to the source.
The control is a component class, inheriting the Datagrid
Code:
Imports System.Drawing
Public Class comDataGrid
Inherits DataGrid
Dim sColumns As String
Dim sMapping As String
Dim sWidths() As String
Dim sHeaders() As String
Dim sAlignments() As String
Dim ts As New DataGridTableStyle
Dim rw As DataGridColumnStyle
Public Sub zSetDataSource(ByVal dt As DataTable)
Me.DataSource = dt
Me.sMapping = dt.TableName
'Assign Mapping name to TableStyle
ts.MappingName = dt.TableName
'Build a String of all columns.
For Each c As DataColumn In dt.Columns
Me.sColumns += c.ColumnName.Trim + ","
Next
Me.sColumns = Me.sColumns.TrimEnd(","c)
End Sub
Public Sub zSetColumnstoShow(ByVal s As String)
Me.sColumns = s
End Sub
Public Sub zSetColumnWidths(ByVal sColumnWidths As String)
Me.sWidths = sColumnWidths.Split(","c)
End Sub
Public Sub zSetColumnHeaders(ByVal sColumnHeaders As String)
Me.sHeaders = sColumnHeaders.Split(","c)
End Sub
Public Sub zSetColumnAlignMents(ByVal sAlignMents As String)
Me.sAlignments = sAlignMents.Split(","c)
End Sub
Public Sub zBuildGrid()
Dim dgcol As DataGridTextBoxColumn
Dim iloop As Integer = 0
For Each s As String In Me.sColumns.Split(","c)
'Define and apply the column settings
dgcol = New DataGridTextBoxColumn
dgcol.ReadOnly = True
dgcol.MappingName = s
'Column Header
If iloop < Me.sHeaders.Length Then
dgcol.HeaderText = CStr(Me.sHeaders(iloop))
End If
'Column Width
If iloop < Me.sWidths.Length Then
dgcol.Width = CInt(Me.sWidths(iloop))
End If
'Column AlignMent
If iloop < Me.sAlignments.Length Then
Dim sAlign As String = CStr(Me.sAlignments(iloop))
Select Case sAlign
Case "L"
dgcol.Alignment = HorizontalAlignment.Left
Case "C"
dgcol.Alignment = HorizontalAlignment.Center
Case "R"
dgcol.Alignment = HorizontalAlignment.Right
End Select
End If
'Add to TableStyle
ts.GridColumnStyles.Add(dgcol)
iloop += 1
Next
With Me
.TableStyles.Add(ts)
.TableStyles(Me.sMapping).ReadOnly = True
.TableStyles(Me.sMapping).AllowSorting = False
End With
Me.Select(0)
End Sub
Private Sub comDataGrid_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Dim pt As New Point(e.X, e.Y)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(pt)
If hti.Type = DataGrid.HitTestType.Cell Then
Me.CurrentCell = New DataGridCell(hti.Row, hti.Column)
End If
End Sub
Private Sub comDataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.CurrentCellChanged
Me.Select(Me.CurrentCell.RowNumber)
End Sub
End Class
Usage
Build and add to toolbar, then drop onto a new form
Sample usage code below
Code:
With xGrid
.zSetDataSource(dt) ' DataTable
.zSetColumnHeaders("FirstName,LastName,Salary")
.zSetColumnWidths("100,150,80")
.zSetColumnAlignMents("L,L,R")
.zBuildGrid()
End With
Sweep
...if it works dont mess with it