Imports System.Runtime.CompilerServices
Imports System.Windows.Forms
<Extension()> _
Public Sub ColorCells(ByVal Expression As DataGridView, ByVal ColorSettings As List(Of CellColorSetting))
Dim L As New List(Of DataGridViewRow)
For Each R In Expression.Rows
L.Add(R)
Next
For Each ColorSetting In ColorSettings
For Each PropertyName In ColorSetting.ColumnNames.Split(",")
If Not Expression.Columns.Contains(PropertyName) Then
Throw New Exception("Error Applying Color Style: The Column named '" & PropertyName & "' does not exist in " & Expression.Name)
End If
Next
Dim Matches = L.FindAll(ColorSetting.ConditionToMatch)
For Each Match In Matches
Match.ApplyStyleToCell(ColorSetting.ColumnNames, ColorSetting.Style)
Next
Next
End Sub
<Extension()> _
Public Sub ColorRows(ByVal Expression As DataGridView, ByVal ColorSettings As List(Of CellColorSetting))
Dim L As New List(Of DataGridViewRow)
For Each R In Expression.Rows
L.Add(R)
Next
For Each ColorSetting In ColorSettings
Dim Matches = L.FindAll(ColorSetting.ConditionToMatch)
For Each Match In Matches
Match.ApplyStyleToRow(ColorSetting.Style)
Next
Next
End Sub
<Extension()> _
Public Function ToCSV(ByVal Expression As DataGridView, Optional ByVal SelectedRowsOnly As Boolean = True, Optional ByVal Separator As String = ",") As String
Dim SB As New Text.StringBuilder
For Each column As DataGridViewColumn In Expression.Columns
'SB.Append("""")
SB.Append(Replace(column.HeaderText, vbCrLf, ""))
'SB.Append("""")
If Not column.Index = Expression.Columns.Count - 1 Then
SB.Append(Separator)
End If
Next
SB.AppendLine()
For Each Row As DataGridViewRow In IIf(SelectedRowsOnly, Expression.SelectedRows, Expression.Rows)
For Each Col As DataGridViewColumn In Expression.Columns
'SB.Append("""")
If Row.Cells(Col.Index).Value.GetType Is GetType(Date) Then
SB.Append(Row.Cells(Col.Index).Value.toshortdatestring)
Else
SB.Append(Row.Cells(Col.Index).Value)
End If
'SB.Append("""")
If Not Col.Index = Expression.Columns.Count - 1 Then
SB.Append(Separator)
End If
Next
SB.AppendLine()
Next
Return SB.ToString
End Function
<Extension()> _
Public Function ToHTML(ByVal Expression As DataGridView, Optional ByVal SelectedRowsOnly As Boolean = True, Optional ByVal IncludeHTMLandBodyTags As Boolean = True) As String
Dim SB As New System.Text.StringBuilder
If IncludeHTMLandBodyTags Then
SB.AppendLine("<HTML>")
SB.AppendLine("<BODY>")
End If
'Write Table
SB.AppendLine("<TABLE border=1 bgcolor=#FFFFFF>")
SB.AppendLine("<TR bgcolor=#D5D5D8>")
For Each Column As DataGridViewColumn In Expression.Columns
SB.Append("<TD>")
SB.Append("<SPAN style=""font-family: arial, helvetica, sans-serif; font-size: 12px"">")
SB.Append("<STRONG>")
SB.Append(Trim(Column.HeaderText))
SB.Append("</STRONG>")
SB.Append("</SPAN>")
SB.Append("</TD>")
Next
SB.AppendLine("</TR>")
For Each Row As DataGridViewRow In IIf(SelectedRowsOnly, Expression.SelectedRows, Expression.Rows)
SB.AppendLine("<TR>")
For Each Column As DataGridViewColumn In Expression.Columns
SB.Append("<TD bgcolor=")
SB.Append(IIf(Row.Index Mod 2, "#F2EAB5", "#E3EEFB"))
SB.Append(">")
Dim Value = Trim(Row.Cells(Column.Index).FormattedValue.ToString)
Select Case Row.Cells(Column.Index).Value.GetType.ToString
Case GetType(Date).ToString, GetType(Decimal).ToString 'Align Numbers and dates to the right
SB.Append("<p style=""text-align: right"">")
SB.Append("<SPAN style=""font-family: courier new, helvetica, sans-serif; font-size: 12px"">")
SB.Append(Value)
SB.Append("</SPAN>")
SB.Append("</p>")
Case GetType(Boolean).ToString
SB.Append("<p>")
SB.Append("<input checked=""")
SB.Append(IIf(Value, "checked", "unchecked"))
SB.Append(""" name=""CheckBox")
SB.Append(Column.Index)
SB.Append(" type=""checkbox"" value=""")
SB.Append(Value.ToString)
SB.Append("/>")
SB.Append("</p>")
Case Else
SB.Append("<SPAN style=""font-family: courier new, helvetica, sans-serif; font-size: 12px"">")
SB.Append(Value)
SB.Append("</SPAN>")
End Select
SB.AppendLine("</TD>")
Next
SB.AppendLine("</TR>")
Next
SB.AppendLine("</TABLE>")
If IncludeHTMLandBodyTags Then
SB.AppendLine("</HTML>")
SB.AppendLine("</BODY>")
End If
Return SB.ToString
End Function
<Extension()> _
Public Function ConverTextToXML(ByVal Expression As String) As String
Dim Value = Expression
Value = Replace(Value, "&", "&")
Value = Replace(Value, ">", ">")
Value = Replace(Value, "<", "<")
Value = Replace(Value, "'", "'")
Value = Replace(Value, """", """)
Return Value
End Function
<Extension()> _
Public Function ToXML(ByVal Expression As DataGridView, Optional ByVal SelectedRowsOnly As Boolean = True, Optional ByVal Title As String = "Export") As String
Dim SB As New System.Text.StringBuilder
SB.AppendLine("<?xml version=""1.0""?>")
'Write Table
SB.Append(vbTab)
SB.AppendLine("<TABLE>")
For Each Row As DataGridViewRow In IIf(SelectedRowsOnly, Expression.SelectedRows, Expression.Rows)
SB.Append(vbTab)
SB.Append(vbTab)
SB.AppendLine("<ROW>")
For Each Column As DataGridViewColumn In Expression.Columns
If Column.CellType Is GetType(DataGridViewImageCell) Then
GoTo NextColumn
End If
Dim ColHeader As String = Column.HeaderText.ConverTextToXML
ColHeader = Replace(ColHeader, ")", "")
ColHeader = Replace(ColHeader, "(", "")
ColHeader = Replace(ColHeader, "/", "")
ColHeader = Replace(ColHeader, "\", "")
ColHeader = Replace(ColHeader, "<", "")
ColHeader = Replace(ColHeader, ">", "")
ColHeader = Replace(ColHeader, "*", "_")
ColHeader = Replace(ColHeader, "#", "No")
ColHeader = Replace(ColHeader, " ", "__")
SB.Append(vbTab)
SB.Append(vbTab)
SB.Append(vbTab)
SB.Append("<")
SB.Append(ColHeader)
SB.Append(">")
Dim Value = Trim(Row.Cells(Column.Index).FormattedValue.ToString).ConverTextToXML
'Value = Replace(Value, "&", "&")
'Value = Replace(Value, ">", ">")
'Value = Replace(Value, "<", "<")
'Value = Replace(Value, "'", "'")
'Value = Replace(Value, """", """)
Select Case Row.Cells(Column.Index).ValueType.ToString
Case GetType(Date).ToString
SB.Append(Value)
Case GetType(Decimal).ToString()
SB.Append(Value)
Case GetType(Boolean).ToString
SB.Append(Value.ToString)
Case Else
SB.Append(Value)
End Select
SB.Append("</")
SB.Append(ColHeader)
SB.Append(">")
SB.AppendLine()
NextColumn:
Next
SB.Append(vbTab)
SB.Append(vbTab)
SB.AppendLine("</ROW>")
Next
SB.Append(vbTab)
SB.AppendLine("</TABLE>")
Return SB.ToString
End Function
<Extension()> _
Public Sub QuickSaveXML(ByVal Expression As DataGridView, ByVal Prefix As String, ByVal OpenWithExcel As Boolean)
Dim XML = Expression.ToXML(False)
Dim XML_ExportDir = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Exports\"
CreateDir:
If Not My.Computer.FileSystem.DirectoryExists(XML_ExportDir) Then
Try
My.Computer.FileSystem.CreateDirectory(XML_ExportDir)
Catch ex As Exception
If MsgBox(ex.Message, MsgBoxStyle.RetryCancel, "Error creating export directory") = MsgBoxResult.Retry Then
GoTo CreateDir
Else
Exit Sub
End If
End Try
End If
Dim ExportFile = XML_ExportDir & Prefix & "-" & Now.ToFileName & ".xml"
My.Computer.FileSystem.WriteAllText(ExportFile, XML, False)
If OpenWithExcel Then
Try
System.Diagnostics.Process.Start("excel", """" & ExportFile & """")
Catch ex As Exception
MsgBox(ex.Message & vbCrLf & "File was still exported to: MyDocuments\Exports", MsgBoxStyle.Exclamation, "Error Opening with excel, is it installed?")
End Try
End If
End Sub
End Module