Framework
A framework comprises of all different elements that make the production of a software system smoother and less complicated.

mdWindowsUtility

mdWindowsUtility is a module where many procedures for windows formatting are stored.

Imports System.Windows.Forms
Imports Microsoft.Reporting.WinForms

Public Module mdWindowsUtility

Public Function GetForm(ByRef FormObj As Form, PrimaryKeyValue As Long) As Boolean
Dim b As Boolean = false
For Each objExistingForm As Form In Application.OpenForms
If FormObj.Name = objExistingForm.Name Then
If objExistingForm.Tag Is Nothing = false Then
If objExistingForm.Tag = PrimaryKeyValue Then
FormObj = objExistingForm
b = true
Exit For
End If
End If
End If
Next
Return b
End Function

Public Sub SetupGridReadonly(GridObj As DataGridView)
With GridObj
.Readonly = true
.AllowUserToAddRows = false
.AllowUserToDeleteRows = false
.RowHeadersWidth = 27
.MultiSelect = false
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
End Sub

Public Function GetValueFromGrid(GridObj As DataGridView, ColumnName As string, RowIndex As Integer, Optional UseSelectedRow As Boolean = false) As Long
Dim nValue As Long = 0
With GridObj
If UseSelectedRow = true Then
If .SelectedRows.Count > 0 Then
RowIndex = .SelectedRows(0).Index
End If
End If
If .Columns.Contains(ColumnName) = false Then
Throw New Exception("Grid does not contain column " & ColumnName)
ElseIf IsDBNull(.Item(ColumnName, RowIndex).Value) = true Then
nValue = 0
ElseIf IsNumeric(.Item(ColumnName, RowIndex).Value) = true Then
nValue = GridObj.Item(ColumnName, RowIndex).Value
End If
End With
Return nValue
End Function

Public Sub SelectGridRowBasedOnFieldValue(GridObj As DataGridView, FieldName As string, Value As Long)
With GridObj
For i As Integer = 0 To .Rows.Count - 1
If .Item(FieldName, i).Value = Value Then
.Rows(i).Selected = true
Exit For
End If
Next
End With
End Sub

Public Sub AddTextboxBinding(TextBoxControl As TextBox, TableObj As DataTable, FieldName As string)
If IsNumeric(FieldName) = true Then
Throw New CPUException("Cannot bind " & TextBoxControl.Name & " to numeric value.")
End If

TextBoxControl.DataBindings.Add("Text", TableObj, FieldName)
End Sub

Public Sub AddLabelBinding(LabelControl As Label, TableObj As DataTable, FieldName As string)
If IsNumeric(FieldName) = true Then
Throw New CPUException("Cannot bind " & LabelControl.Name & " to numeric value.")
End If

LabelControl.DataBindings.Add("Text", TableObj, FieldName)
End Sub

Public Sub AddBindingForGrid(ByVal GridObject As DataGridView, ByVal TableObject As DataTable, ByVal PrimaryKeyFieldName As string, ByVal ColumnList As string, Optional ByVal RemoveExistingCols As Boolean = true, Optional ByVal RefreshDataSource As Boolean = false, Optional ByVal AutoSort As Boolean = false, Optional ByVal BindToCopy As Boolean = false)
Dim bPrimaryKeyBound As Boolean = false
Dim sPrimaryKeyFieldName As string = PrimaryKeyFieldName.ToLower
Dim objTable As DataTable = TableObject
Dim bColumnsAdded As Boolean = false
If RemoveExistingCols = true Then
Try
GridObject.Columns.Clear()
Catch ex As Exception
End Try
End If
If Trim(ColumnList) <> "" Then
bColumnsAdded = true
GridObject.AutoGenerateColumns = false
Dim sColumns() As string = ColumnList.Split(",")
For Each sColumn As string In sColumns
Dim objColumn As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
Dim s() As string = sColumn.Split(":")
With objColumn
s(0) = Trim(s(0))
'Me.EnsureFieldExists(objTable, s(0))
.DataProperyName = s(0)
Select Case objTable.Columns(s(0)).DataType.Name
Case "Decimal", "Double"
With objColumn
With .DefaultCellStyle
.Alignment = DataGridViewContentAlignment.MiddleRight
If s.Length > 3 Then
If IsNumeric(s(3)) = true Then
.Format = NumericFormat(s(3))
Else
.Format = CurrencyFormat()
End If
Else
.Format = CurrencyFormat()
End If
End With
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
.SortMode = DataGridViewColumnSortMode.NotSortable
End With
Case "Date", "DateTime"
objColumn.DefaultCellStyle.Format = "d"
Case "string"
'200:wrap
If s.Length > 3 Then '4th segment
If s(3).ToLower = "wrap" Then
objColumn.DefaultCellStyle.WrapMode = DataGridViewTriState.true
With GridObject
.AutoResizeRows()
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
End With
End If
End If
End Select
If UBound(s) > 0 Then .HeaderText = s(1)
.Name = s(0)
If UBound(s) > 1 Then
If IsNumeric(s(2)) = true Then
.Width = s(2)
.Tag = s(2)
End If
End If
If sPrimaryKeyFieldName = LCase(s(0)) Then
objColumn.Readonly = true
bPrimaryKeyBound = true
End If
End With
If AutoSort = true Then
objColumn.SortMode = DataGridViewColumnSortMode.Automatic
Else
objColumn.SortMode = DataGridViewColumnSortMode.NotSortable
End If
'objColumn.HeaderCell.Style.WrapMode = DataGridViewTriState.false
GridObject.Columns.Add(objColumn)
Next
If bPrimaryKeyBound = false Then
Dim objColumn As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
With objColumn
.Name = sPrimaryKeyFieldName
.DataProperyName = sPrimaryKeyFieldName
.Visible = false
End With
GridObject.Columns.Insert(0, objColumn)
End If
End If
If BindToCopy = false Then
GridObject.DataSource = objTable
Else
GridObject.DataSource = objTable.Copy
End If
If bColumnsAdded = false And AutoSort = false Then
For Each objC As DataGridViewColumn In GridObject.Columns
objC.SortMode = DataGridViewColumnSortMode.NotSortable
Next
End If
End Sub

Public Sub FormatGridColumns(GridObj As DataGridView)
For Each objColumn As DataGridViewColumn In GridObj.Columns
Dim sFieldName As string = objColumn.DataProperyName
If mdUtility.IsFeildKey(sFieldName) = true Then
objColumn.Visible = false
Else
objColumn.HeaderText = mdUtility.GetFriEndlyColumnName(sFieldName)
End If
Next
End Sub

Public Function CurrencyFormat() As string
Dim sCurrencyFormat As string = ""
sCurrencyFormat = "n2"
Return sCurrencyFormat
End Function

Public Function NumericFormat(ByVal DecimalPlaces As Integer) As string
Dim sNumericFormat As string = ""
Dim Zerostring As string = New string("0", DecimalPlaces)
If sNumericFormat = "" Then
Dim sDecimal As string = Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
Dim sComma As string = Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyGroupSeparator
Dim sFormat As string = "#" & sComma & "0" & sDecimal & Zerostring
sNumericFormat = sFormat & ";(" & sFormat & ")"
End If
Return sNumericFormat
End Function

Public Sub LoadReportFromTable(ReportViewerObj As ReportViewer, ByVal ReportHeaderValue As string, ByVal TableObj As DataTable, Optional Bindingstring As string = "")
Dim ReportType As RdlGenerator.ReportTypeEnum
Dim ExcludeList As Collection = Nothing
Dim DefaultToLandscape As Boolean
Dim IncludeCriteria As Boolean

Dim objRDL As RdlGenerator = New RdlGenerator()
Dim sReportHeaderSource As string
Dim objExcludeListSource As Collection
Dim bDefaultToLandscapeSource As Boolean
Dim objAdapterSource As DataTable
Dim bIncludeCriteria As Boolean = false
Dim objReportViewer As ReportViewer = ReportViewerObj
Try
objReportViewer.Clear()
objReportViewer.Reset()
Catch ex As Exception
End Try
Dim sGroupField As string = ""
Dim sTotalCols As string = ""
Dim sGrandTotalCols As string = ""
Dim sCriteria As string = ""
Dim sData As string = ""
Dim sExtraHeader As string = ""
sReportHeaderSource = ReportHeaderValue
objExcludeListSource = ExcludeList
bDefaultToLandscapeSource = DefaultToLandscape
bIncludeCriteria = IncludeCriteria
objAdapterSource = TableObj

Dim TableObject As DataTable = Nothing
If TableObj Is Nothing = false Then
TableObject = TableObj
End If
Dim objMemoryStream As IO.MemoryStream
Dim objTable As DataTable
Dim objLocalReport As LocalReport = objReportViewer.LocalReport()
Dim objReportDS As ReportDataSource = New ReportDataSource
'Me.SetWindowTitle()
objReportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local

If TableObject Is Nothing = true Then
Throw New Exception("Datasource for report not specIfied")
End If

With TableObject
objTable = .Clone
For Each objRowView As DataRowView In .DefaultView
objTable.ImportRow(objRowView.Row)
Next
objTable.DefaultView.Sort = .DefaultView.Sort
End With
Dim sLocalCriteria As string = ""
'sCriteria = modGlobal.GetCriteriaTostring(tblCriteria, tblCriteria)

' If IncludeCriteria = true Then
' sLocalCriteria = sCriteria
' End If
objMemoryStream = objRDL.Run(sReportHeaderSource, objTable, Bindingstring, RdlGenerator.ReportTypeEnum.Preview, ExcludeList, sLocalCriteria, sGroupField, sExtraHeader, sTotalCols, sGrandTotalCols)
sData = objRDL.DataTostring


If TableObj Is Nothing = false Then
Dim enc As New System.Text.UTF8Encoding
Dim arrBytData() As Byte = New Byte(CType(objMemoryStream.Length, Long)) {}
Dim s As string = ""
objMemoryStream.Read(arrBytData, 1, objMemoryStream.Length)
s = enc.Getstring(arrBytData, 1, arrBytData.Length - 1)
Dim objMemoryStream2 As System.IO.MemoryStream

s = s.Replace("ReplaceExtraHeader", sExtraHeader)
objMemoryStream2 = New System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s))

objLocalReport.LoadReportDefinition(objMemoryStream2)
Else
'Dim enc As New System.Text.UTF8Encoding
'Dim arrBytData() As Byte = New Byte(CType(objMemoryStream.Length, Long)) {}
'Dim s As string = ""
'objMemoryStream.Read(arrBytData, 1, objMemoryStream.Length)
's = enc.Getstring(arrBytData, 1, arrBytData.Length - 1)
objLocalReport.LoadReportDefinition(objMemoryStream)
End If

With objReportDS
.Name = "DataSet1"
.Value = objTable
End With
objLocalReport.DataSources.Add(objReportDS)
objReportViewer.RefreshReport()
Select Case ReportType
'Case RdlGenerator.ReportTypeEnum.Preview
' Me.Show()
Case RdlGenerator.ReportTypeEnum.Excel
'ExportToExcel()
End Select
objMemoryStream.Close()
objMemoryStream = Nothing
objRDL = New RdlGenerator 'ensure that next time a new instance is used, we can't Set this at the bginning of the procedure because the instance may have properties Set by an event in bcGrid
End Sub

End Module