Imports RustemSoft.DataGridColumns
======================================= ' For more info: ' http://rustemsoft.com/DataGridColumns.htm ' The assembly online documentation: ' http://www.rustemsoft.com/DataGridColumnsDoc/ ' ------------------------------------------------------------------------------------- Public Class Form1 Inherits System.Windows.Forms.Form ' Identify path to application folder Private AppPath As String = Replace(Replace(Replace(Application.ExecutablePath, Application.ProductName + ".EXE", ""), Application.ProductName + ".exe", ""), Application.ProductName + ".exe", "") ' Initiate a new Data set Private ds As New DataSet() ' Initiate a new OleDb Data Adapter Private dbAdaptr As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter() ' Set Connection String to Samples.mdb Private conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppPath + "Samples.mdb" ' Define Connection to Samples.mdb Private cn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection(conStr) ' Define XPButtonColumn column style object of DataGridXPButtonColumn class Private WithEvents XPButtonColumn As DataGridXPButtonColumn #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid Friend WithEvents Button1 As XPButton Friend WithEvents btnOk As XPButton Friend WithEvents btnRS As XPButton <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1)) Me.Panel1 = New System.Windows.Forms.Panel Me.btnRS = New RustemSoft.DataGridColumns.XPButton Me.btnOk = New RustemSoft.DataGridColumns.XPButton Me.Button1 = New RustemSoft.DataGridColumns.XPButton Me.DataGrid1 = New System.Windows.Forms.DataGrid Me.Panel1.SuspendLayout() CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'Panel1 ' Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.Panel1.Controls.Add(Me.btnRS) Me.Panel1.Controls.Add(Me.btnOk) Me.Panel1.Controls.Add(Me.Button1) Me.Panel1.Controls.Add(Me.DataGrid1) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill Me.Panel1.Location = New System.Drawing.Point(0, 0) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(770, 325) Me.Panel1.TabIndex = 0 ' 'btnRS ' Me.btnRS.BackColor = System.Drawing.SystemColors.Window Me.btnRS.DialogResult = System.Windows.Forms.DialogResult.None Me.btnRS.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnRS.Image = Nothing Me.btnRS.ImageAlign = RustemSoft.DataGridColumns.XPButton.ImageAligns.Left Me.btnRS.Location = New System.Drawing.Point(336, 283) Me.btnRS.Name = "btnRS" Me.btnRS.Size = New System.Drawing.Size(91, 32) Me.btnRS.TabIndex = 3 Me.btnRS.Text = "RustemSoft" ' 'btnOk ' Me.btnOk.BackColor = System.Drawing.SystemColors.Window Me.btnOk.DialogResult = System.Windows.Forms.DialogResult.None Me.btnOk.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnOk.Image = Nothing Me.btnOk.ImageAlign = RustemSoft.DataGridColumns.XPButton.ImageAligns.Left Me.btnOk.Location = New System.Drawing.Point(186, 283) Me.btnOk.Name = "btnOk" Me.btnOk.Size = New System.Drawing.Size(91, 32) Me.btnOk.TabIndex = 2 Me.btnOk.Text = "Ok" ' 'Button1 ' Me.Button1.BackColor = System.Drawing.SystemColors.Window Me.Button1.DialogResult = System.Windows.Forms.DialogResult.None Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button1.Image = Nothing Me.Button1.ImageAlign = RustemSoft.DataGridColumns.XPButton.ImageAligns.Left Me.Button1.Location = New System.Drawing.Point(486, 283) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(91, 32) Me.Button1.TabIndex = 1 Me.Button1.Text = "Cancel" ' 'DataGrid1 ' Me.DataGrid1.DataMember = "" Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText Me.DataGrid1.Location = New System.Drawing.Point(0, 0) Me.DataGrid1.Name = "DataGrid1" Me.DataGrid1.Size = New System.Drawing.Size(766, 272) Me.DataGrid1.TabIndex = 0 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(770, 325) Me.Controls.Add(Me.Panel1) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon) Me.MaximizeBox = False Me.MinimizeBox = False Me.Name = "Form1" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "Datagrid Column Stylies Sample" Me.Panel1.ResumeLayout(False) CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Try ' Open Samples.mdb cn.Open() With dbAdaptr ' Populate Cars table Dim SQLStr As String = "Select * from Cars" .TableMappings.Add("Table", "Cars") Dim cmd As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand(SQLStr, cn) cmd.CommandType = CommandType.Text .SelectCommand = cmd .Fill(ds) .Dispose() ' Populate Companies table SQLStr = "Select * from Companies" .TableMappings.Add("Table", "Companies") cmd = New System.Data.OleDb.OleDbCommand(SQLStr, cn) cmd.CommandType = CommandType.Text .SelectCommand = cmd .Fill(ds) .Dispose() ' Populate States table SQLStr = "Select * from States" .TableMappings.Add("Table", "States") cmd = New System.Data.OleDb.OleDbCommand(SQLStr, cn) cmd.CommandType = CommandType.Text .SelectCommand = cmd .Fill(ds) .Dispose() ' Cancel OleDb Command cmd.Cancel() End With ' DataGrid settings With DataGrid1 ' Set DataGrid Data Binding to Cars table .SetDataBinding(ds, "Cars") ' Set DataGrid Background Color .BackgroundColor = System.Drawing.Color.Lavender ' Set DataGrid Caption Background Color .CaptionBackColor = System.Drawing.Color.SlateBlue ' Set DataGrid Caption Foreground Color .CaptionForeColor = System.Drawing.Color.LemonChiffon ' Set DataGrid Parent Rows Background Color .ParentRowsBackColor = System.Drawing.Color.Lavender ' Set DataGrid Parent Rows Foreground Color .ParentRowsForeColor = System.Drawing.Color.SlateBlue ' Set DataGrid Caption Text .CaptionText = "Cars" ' Clear DataGrid Table Styles .TableStyles.Clear() End With ' Set data grid Table Style Dim tblCrrncMngr As CurrencyManager = CType(BindingContext(ds.Tables.Item("Cars")), CurrencyManager) Dim TblStyle As New DataGridTableStyle() With TblStyle .MappingName = "Cars" .BackColor = System.Drawing.Color.White .ForeColor = System.Drawing.Color.DarkSlateBlue .GridLineColor = System.Drawing.Color.MediumSlateBlue .HeaderBackColor = System.Drawing.Color.Lavender .HeaderForeColor = System.Drawing.Color.MediumSlateBlue .AlternatingBackColor = Color.LightGray .RowHeaderWidth = 10 ' Set column styles With .GridColumnStyles ' Set datagrid ColumnStyle for Car field .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("Car"))) With .Item(0) .MappingName = "Car" .HeaderText = "Car Name" .Width = 100 .NullText = String.Empty End With ' Set datagrid ComboBox ColumnStyle for PubID field .Add(New DataGridComboBoxColumn(ds.Tables.Item("Companies"), 1, 0)) ' Datagrid ComboBox DisplayMember field has order number 1. Name of this column is "Name" ' Datagrid ComboBox ValueMember field has order number 0. Name of this column is "PubID" With .Item(1) .MappingName = "PubID" .HeaderText = "Company ID" .Width = 180 .NullText = String.Empty End With ' Set datagrid DateTimePicker ColumnStyle for DateFirst field .Add(New DataGridDateTimePicker()) With .Item(2) .MappingName = "DateFirst" .HeaderText = "Date" .Width = 70 .NullText = String.Empty End With ' Set datagrid combobox ColumnStyle for State field .Add(New DataGridComboBoxColumn(ds.Tables.Item("States"), 0, 0, , , False)) ' Datagrid ComboBox DisplayMember field has order number 0. Name of this column is "State" ' Datagrid ComboBox ValueMember field has order number 0. It is the same column like for DisplayMember With .Item(3) .MappingName = "State" .HeaderText = "State" .Width = 45 .NullText = String.Empty End With ' Set datagrid XP Style Button ColumnStyle for City field .Add(New DataGridXPButtonColumn()) ' Also you may set datagrid Button ColumnStyle for City ' field without Windows XP Style Button as the following: '.Add(New DataGridButtonColumn()) With .Item(4) .MappingName = "City" .HeaderText = "City" .Width = 90 .NullText = String.Empty End With ' Set datagrid Memo ColumnStyle for Comments field .Add(New DataGridMemoColumn("Car description")) With .Item(5) .MappingName = "Comments" .HeaderText = "Comments" .Width = 90 .NullText = String.Empty End With ' Set datagrid DateTime ColumnStyle for TimeFirst field .Add(New DataGridDateTimeColumn(DateTimeTextBox.Stencils.HHMM12, Now, ":")) With .Item(6) .MappingName = "TimeFirst" .HeaderText = "Time" .Width = 55 .NullText = String.Empty End With ' Set datagrid Numeric ColumnStyle for Price field .Add(New RustemSoft.DataGridColumns.DataGridNumericColumn(, True, , , , , 2)) With .Item(7) .MappingName = "Price" .HeaderText = "Price" .Width = 60 .NullText = String.Empty End With ' Set datagrid TextFractions ColumnStyle for Phone field .Add(New RustemSoft.DataGridColumns.DataGridTextFractionsColumn()) With .Item(8) .MappingName = "Phone" .HeaderText = "Phone" .Width = 80 .NullText = String.Empty End With ' Set datagrid TextEdit ColumnStyle for Remarks field .Add(New DataGridTextEditColumn()) With .Item(9) .MappingName = "Remarks" .HeaderText = "Remarks" .Width = 150 .NullText = String.Empty End With ' Set datagrid NumericUpDown ColumnStyle for Percent field .Add(New DataGridNumericUpDownColumn()) With .Item(10) .MappingName = "Percent" .HeaderText = "%%" .Width = 50 .NullText = String.Empty End With ' Set datagrid DomainUpDown ColumnStyle for StateID field .Add(New DataGridDomainUpDownColumn(ds.Tables.Item("States"), 0)) With .Item(11) .MappingName = "StateID" .HeaderText = "State" .Width = 50 .NullText = String.Empty End With End With End With ' Add TableStyle DataGrid1.TableStyles.Add(TblStyle) ' How to change the display format for DateTimePicker columns in a datagrid? What about other properties? ' The DataGridDateTimePicker's picker object has the same properties as the .NET DateTimePicker control. ' The following example sets the CustomFormat property so that will display the date as "12/31/2003". ' Identify DtTmPicker object that is the DataGridDateTimePicker class’ “child” Dim DtTmPicker As DataGridDateTimePicker = DataGrid1.TableStyles(0).GridColumnStyles(2) ' Set the Format type and the CustomFormat string DtTmPicker.picker.Format = DateTimePickerFormat.Custom DtTmPicker.picker.CustomFormat = "MM/dd/yyyy" ' for European people (you can see the date as "31.12.2003"): 'System.Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("de-DE") 'DtTmPicker.picker.CustomFormat = "dd.MM.yy" '!!! To empty a DateTimePickerColumn cell just click the right mouse button when the cell is selected !!! ' Change Picker's calendar title background color DtTmPicker.picker.CalendarTitleBackColor = Color.SlateBlue ' How to change the background color for DataGridComboBoxColumn columns in a datagrid? What about other properties? ' The DataGridComboBoxColumn's combo object has the same properties as the .NET ComboBox control. ' The following example sets the BackColor property so that will display red background. ' Identify StateColumn object for 4th DataGrid "State" column that is the DataGridComboBoxColumn class’ “child” Dim StateColumn As DataGridComboBoxColumn = DataGrid1.TableStyles(0).GridColumnStyles(3) ' Change combobox background color to red StateColumn.combo.BackColor = Color.Red ' Change combobox characters' color to yellow StateColumn.combo.ForeColor = Color.Yellow ' Assign XPButtonColumn column style object as 5th DataGrid "City" column XPButtonColumn = DataGrid1.TableStyles(0).GridColumnStyles(4) ' DataGridXPButtonColumn control shows a button instead of text, even when the containing cell ' doesn't have focus. To switch the feature off change value of ButtonInEachCell parameter to False. 'XPButtonColumn.ButtonInEachCell = False ' You can assign a Title for each button in your button column instead of ' populating table field’s text. To do that assign value of PerpetualText property. 'XPButtonColumn.PerpetualText = "Click Me!" ' Define NumericColumn DataGridNumericColumn object for Price field Dim NumericColumn As DataGridNumericColumn = DataGrid1.TableStyles(0).GridColumnStyles(7) ' Specify back color for the field NumericColumn.txtBox.box.BackColor = System.Drawing.Color.LightPink ' You may turn error messages off 'NumericColumn.txtBox.box.ErrMessageVisible = False ' You can specify an error message in your native language: 'NumericColumn.txtBox.box.ErrMessageNumericOnly = "Veuillez e'crire les nombres seulement!" ' Identify PhoneColumn object that is the DataGridTextFractionsColumn’s “child” Dim PhoneColumn As DataGridTextFractionsColumn = DataGrid1.TableStyles(0).GridColumnStyles(8) ' Specify Delimiter Character for the field PhoneColumn.txtBox.box.DelimiterChar = "-" ' Specify first fraction properties ' Alphanumeric symbols only are acceptable for the fraction PhoneColumn.txtBox.box.FractionsCode(0, 0) = "a" ' You can insert 3 symbols only into the first fraction PhoneColumn.txtBox.box.FractionsCode(0, 1) = 3 ' Specify second fraction properties ' Numeric symbols only are acceptable for the fraction PhoneColumn.txtBox.box.FractionsCode(1, 0) = "n" ' You can insert 3 symbols only into the second fraction PhoneColumn.txtBox.box.FractionsCode(1, 1) = 3 ' Specify third fraction properties ' Numeric symbols only are acceptable for the fraction PhoneColumn.txtBox.box.FractionsCode(2, 0) = "n" ' You can insert 4 symbols only into the third fraction PhoneColumn.txtBox.box.FractionsCode(2, 1) = 4 Catch er As Exception MessageBox.Show("Cannot open Samples.mdb" + vbLf + er.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop) cn.Close() ds.Dispose() Me.Close() End Try End Sub ' DataGridXPButtonColumn Click event runs when the datagrid XPButtonColumn Style's ' Button control is clicked. The Sub receives three arguments: ' sender as DataGridXPButtonColumn, ColumnMappingName and ColumnValueAtRow of String type Private Sub XPButtonColumn_Click(ByVal sender As DataGridXPButtonColumn, ByVal ColumnMappingName As String, ByVal ColumnValueAtRow As String) Handles XPButtonColumn.Click If ColumnMappingName = "City" Then Dim strValue As String If IsDBNull(DataGrid1.Item(DataGrid1.CurrentRowIndex, 4)) Then strValue = "Nothing :(" Else strValue = ColumnValueAtRow + " city, " + DataGrid1.Item(DataGrid1.CurrentRowIndex, 3) + "!" End If MessageBox.Show("You have chosen " + strValue, "DataGrid Button is clicked!") End If End Sub ' Save changes and close application Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click Dim myRow As DataRow, myCol As DataColumn, SQLStr, Value As String, i As Integer Dim cmd As System.Data.OleDb.OleDbCommand ' Delete previous data from Cars table Try SQLStr = "Delete from Cars" ' Identify OleDb Command cmd = New System.Data.OleDb.OleDbCommand(SQLStr, cn) ' Execute OleDb Command cmd.ExecuteNonQuery() cmd.Cancel() Catch er As Exception MessageBox.Show("Cannot update Samples.mdb" + vbLf + er.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop) ds.Dispose() Me.Close() End Try ' Write updated data to Cars table Try ' Accept Changes for Cars table ds.Tables.Item("Cars").AcceptChanges() Dim myTable As DataTable = ds.Tables.Item("Cars") ' Go row by row in Cars table For Each myRow In myTable.Rows ' Combine SQL command SQLStr = "Insert into Cars values (" i = 0 ' Go column by column for each row in Cars table For Each myCol In myTable.Columns Value = myRow.Item(i).ToString If Value Is String.Empty Then Value = "Null," Else If Mid(myCol.DataType.ToString, 8) = "Double" Then Value = myRow.Item(i).ToString + "," Else Value = "'" + myRow.Item(i).ToString + "'," End If End If SQLStr += Value i += 1 Next SQLStr = Mid(SQLStr, 1, Len(SQLStr) - 1) + ")" ' Identify OleDb Command cmd = New System.Data.OleDb.OleDbCommand(SQLStr, cn) ' Execute OleDb Command cmd.ExecuteNonQuery() Next Catch er As Exception MessageBox.Show("Cannot update Samples.mdb" + vbLf + er.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop) End Try ds.Dispose() Me.Close() End Sub ' Close application Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ds.Dispose() Me.Close() End Sub ' Go to RustemSoft.com Private Sub btnRS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRS.Click System.Diagnostics.Process.Start("http://www.RustemSoft.com") End Sub End Class |