.NET DataGrid Columns assembly code samples (Windows Forms)

Learn more about DataGridView Columns .NET assembly

  • .NET DataGrid combobox. Auto-fill and Dictionary.
  • .NET Datagrid Simple Sample
  • .NET XP DataGrid Button column Style.
  • .NET DataGrid Memo column Style.
  • .NET DataGrid DateTimePicker column Style.
  • How to change the background color for DataGridComboBoxColumn in a datagrid?
  • How to change the display format for DateTimePicker in a datagrid?
  • How to setup the combo box that filters values from a related child table?
  • How to specify what Dictionary datagrid columns will be shown?
  • How to format a datagrid column using a Date/Time format? How to update data in the column?
  • How to format a datagrid column using a Numeric format? How to update data in the column?
  • How to mask the data in a DataGrid Data column? How to mask the phone number (SSN, IP address) data?
  • How to implement a .NET application with the DataGridPrint class?
  • DataGridView DateTime Column (.NET)
  • DataGridViewColumns .NET assembly
    More about DataGridViewColumns.dll
    Download DataGridViewColumns.dll
    Order DataGridView Columns

    Skater .NET Obfuscator
    More about Skater .NET Obfuscator
    Download Skater .NET Obfuscator
    Order Skater .NET Obfuscator

    ASP DataGridColumns .NET assembly
    More about ASPDataGridColumns.dll
    Download ASPDataGridColumns.dll
    Order ASPDataGridColumns.dll

    Controls .NET assembly (WinForms)
    More about RustemSoft.Controls.dll
    Download RustemSoft.Controls.dll
    Order RustemSoft.Controls.dll

    DataGridColumns .NET assembly
    More about DataGridColumns.dll
    Download DataGridColumns.dll
    Order ASPDataGridColumns.dll

    XML Converter is available!
    More about XML Converter
    Download XML Converter
    Order XML Converter


    0. .NET DataGrid combobox. Auto-fill and Dictionary. Download for VB.NET and C#

    One of the most missing things in the .NET (Windows Forms) programming is that you do not have ability to put a data-bound Combobox in a column of a Datagrid control. And that as well if you are trying to get it programmed in a Windows Forms world it has a lot of work to do. You got to write a bunch of code for a simple extensibility of the existing features of the .NET DataGrid control.
    This sample introduces how to add a DataGridColumnStyle that contains combobox to a DataGrid on your .NET form. It is not just a dropdown combobox, which appears when a datagrid cell becomes the current cell. This .NET DataGrid DataGridColumnStyle combobox control has the following attractive features:

    The combobox DataGridColumnStyle automatically fills the text at the cursor with the value of its dropdown values list. As characters are typed, the component finds the closest matching item from the list and automatically fills in the remaining characters. Appended characters are highlighted so that they are replaced by any further typing. For this auto-filling feature you can setup the character case sensibility.

    This DataGridColumnStyle gives you ability to instantly update dropdown values with a really simple and friendly user interface. When you click the additional dropdown button of the combo a dictionary of its list values will be displayed below the combo. You can update values, and insert and delete rows in the dictionary grid.

    The control gives an ability to get a nesting combo box that filters values from a related child table. In a dictionary grid, you can view and edit related data in a child table. In your database a Master table has a one-to-many relationship with a Child table; so for each row of the Master table in Dictionary grid, you or your customer can view and edit the related rows of the Child table in a dictionary grid.

    You can turn off all or some of these features and use the combobox DataGridColumnStyle just as an easy dropdown DataGrid combobox.

    You can set its DataSource, DisplayMember, and ValueMember to bind the combobox to a foreign table.

    Download the dropdown combobox samples for VB.NET and C# that show how you can use the RustemSoft DataGrid Column styles in Windows Forms .NET datagrid.

    VB .NET

    ' Set datagrid column styles
    With TableStyle.GridColumnStyles
    ' Set datagrid ColumnStyle for Car field
    .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("Car")))
    With .Item(0)
    .MappingName = "Car"
    .HeaderText = "Car Name"
    .Width = 130
    .NullText = String.Empty
    .ReadOnly = True
    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 = 200
    .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(2)
    .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 XP Button Style as the following:
    ' .Add(New DataGridButtonColumn())
    With .Item(3)
    .MappingName = "City"
    .HeaderText = "City"
    .Width = 60
    .NullText = String.Empty
    End With

    ' Set datagrid Memo ColumnStyle for Comments field
    .Add(New DataGridMemoColumn("Car description"))
    With .Item(4)
    .MappingName = "Comments"
    .HeaderText = "Comments"
    .Width = 60
    .NullText = String.Empty
    End With
    End With

    C#

    // Set column styles
    // Set datagrid ColumnStyle for Car field
    DataGridTextBoxColumn colCar = new DataGridTextBoxColumn();
    colCar.MappingName = "Car";
    colCar.HeaderText = "Car Name";
    colCar.Width = 130;
    colCar.NullText = String.Empty;
    colCar.ReadOnly = true;
    TblStyle.GridColumnStyles.Add(colCar);

    // Set datagrid ComboBox ColumnStyle for PubID field
    DataTable tblCompanies = ds.Tables["Companies"];
    TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblCompanies, 1, 0, true, false, true));
    // 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"
    TblStyle.GridColumnStyles[1].MappingName = "PubID";
    TblStyle.GridColumnStyles[1].HeaderText = "Company ID";
    TblStyle.GridColumnStyles[1].Width = 200;
    TblStyle.GridColumnStyles[1].NullText = String.Empty;

    // Set datagrid combobox ColumnStyle for State field
    DataTable tblStates = ds.Tables["States"];
    TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblStates, 0, 0, true, false, 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
    TblStyle.GridColumnStyles[2].MappingName = "State";
    TblStyle.GridColumnStyles[2].HeaderText = "State";
    TblStyle.GridColumnStyles[2].Width = 45;
    TblStyle.GridColumnStyles[2].NullText = String.Empty;

    // Set datagrid XP Style Button ColumnStyle for City field
    TblStyle.GridColumnStyles.Add(new DataGridXPButtonColumn());
    // Also you may set datagrid Button ColumnStyle for City
    // field without XP Button Style as the following:
    // .Add(New DataGridButtonColumn())
    TblStyle.GridColumnStyles[3].MappingName = "City";
    TblStyle.GridColumnStyles[3].HeaderText = "City";
    TblStyle.GridColumnStyles[3].Width = 60;
    TblStyle.GridColumnStyles[3].NullText = String.Empty;

    // Set datagrid Memo ColumnStyle for Comments field
    TblStyle.GridColumnStyles.Add(new DataGridMemoColumn("Car description"));
    TblStyle.GridColumnStyles[4].MappingName = "Comments";
    TblStyle.GridColumnStyles[4].HeaderText = "Comments";
    TblStyle.GridColumnStyles[4].Width = 60;

    The code as presented above will automatically set the selected value in each DataGridComboBoxColumn to the item from the DataRow that comes out of your database. You can also change a selected Item and call an Update with a DataAdapter, and then a new value will be updated to your database. Sure, you need the proper Select, Update, Insert and Delete Commands to match.

    Learn more about DataGridColumns assembly

    7. .NET DataGrid Memo column Style.

    The RustemSoft DataGridMemoColumn gives you a useful Memo Field control, which presents an edit window when user selects a cell of the DataGrid Memo Column on .NET DataGrid. This provides a pop-up memo field editor that you can call from your code at any time to let users edit the text in a memo field.
    This Memo field editor provides more flexibility by updating the memo field to be used in a .NET DataGrid column.
    The DataGrid Memo Column Memo field editor is useful for showing short document contents, like memos and emails.
    When using the DataGrid Memo Column editor you are automatically provided with all general features, which any typical, contemporary word processor has.

    Download the DataGridMemoColumn .NET DataGrid Memo Field control samples for VB.NET and C# that show how you can use the RustemSoft DataGrid Column styles in Windows Forms .NET datagrid.

    VB .NET

    ' Set datagrid column styles
    With TableStyle.GridColumnStyles
    ' Set datagrid ColumnStyle for Car field
    .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("Car")))
    With .Item(0)
    .MappingName = "Car"
    .HeaderText = "Car Name"
    .Width = 130
    .NullText = String.Empty
    .ReadOnly = True
    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 = 200
    .NullText = String.Empty
    End With

    .................................................

    ' Set datagrid Memo ColumnStyle for Comments field
    .Add(New DataGridMemoColumn("Car description"))
    With .Item(4)
    .MappingName = "Comments"
    .HeaderText = "Comments"
    .Width = 60
    .NullText = String.Empty
    End With
    End With

    C#

    // Set column styles
    // Set datagrid ColumnStyle for Car field
    DataGridTextBoxColumn colCar = new DataGridTextBoxColumn();
    colCar.MappingName = "Car";
    colCar.HeaderText = "Car Name";
    colCar.Width = 130;
    colCar.NullText = String.Empty;
    colCar.ReadOnly = true;
    TblStyle.GridColumnStyles.Add(colCar);

    // Set datagrid ComboBox ColumnStyle for PubID field
    DataTable tblCompanies = ds.Tables["Companies"];
    TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblCompanies, 1, 0, true, false, true));
    // 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"
    TblStyle.GridColumnStyles[1].MappingName = "PubID";
    TblStyle.GridColumnStyles[1].HeaderText = "Company ID";
    TblStyle.GridColumnStyles[1].Width = 200;
    TblStyle.GridColumnStyles[1].NullText = String.Empty;

    .................................................

    // Set datagrid Memo ColumnStyle for Comments field
    TblStyle.GridColumnStyles.Add(new DataGridMemoColumn("Car description"));
    TblStyle.GridColumnStyles[4].MappingName = "Comments";
    TblStyle.GridColumnStyles[4].HeaderText = "Comments";
    TblStyle.GridColumnStyles[4].Width = 60;

    Learn more about DataGridColumns assembly

    8. .NET DataGrid DateTimePicker column Style.

    The DateTimePicker ColumnStyle is used to allow the user to select a date and time, and to display that date/time in your DataGrid.

    Download the DataGrid DateTimePicker column Style samples for VB.NET and C# that show how you can use the RustemSoft DataGrid Column styles in Windows Forms .NET datagrid.

    VB .NET

    ' 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 = 200
    .NullText = String.Empty
    End With

    .................................................

    ' Set datagrid DateTimePicker ColumnStyle for DateFirst field
    .Add(New DataGridDateTimePicker())
    With .Item(4)
    .MappingName = "DateFirst"
    .HeaderText = "Date"
    .Width = 80
    .NullText = String.Empty
    End With
    End With

    C#

    // Set datagrid ComboBox ColumnStyle for PubID field
    DataTable tblCompanies = ds.Tables["Companies"];
    TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblCompanies, 1, 0, true, false, true));
    // 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"
    TblStyle.GridColumnStyles[1].MappingName = "PubID";
    TblStyle.GridColumnStyles[1].HeaderText = "Company ID";
    TblStyle.GridColumnStyles[1].Width = 200;
    TblStyle.GridColumnStyles[1].NullText = String.Empty;

    .................................................

    // Set datagrid DateTimePicker ColumnStyle for DateFirst field
    TblStyle.GridColumnStyles.Add(new DataGridDateTimePicker());
    TblStyle.GridColumnStyles[4].MappingName = "DateFirst";
    TblStyle.GridColumnStyles[4].HeaderText = "Date";
    TblStyle.GridColumnStyles[4].Width = 60;

    Learn more about DataGrid DateTimePicker column style

    6. .NET DataGrid XP Button column Style.

    DataGridXPButtonColumn allows you to provide the datagrid interface your users are requesting, while supporting the utterly powerful Microsoft .NET DataGrid control. The DataGridXPButtonColumn makes the MS .NET DataGrid control one of the most powerful controls that you ever had used. A RustemSoft DataGridXPButtonColumn column in a .net DataGrid control gives you ability to define custom functionality for items in the grid beyond the edit, delete, select, and hyperlink features of other column types. For example, you can use a DataGridXPButtonColumn to create a "Link to a website" button.
    The DataGridXPButtonColumn has a pushbutton-style only. The button captions can be a text read from a database.
    DataGridXPButtonColumn control shows a button instead of text, even when the containing cell does not have focus. To switch the feature off change value of ButtonInEachCell parameter to False and only the selected button column's cell will appear as a button. Also you can assign a Title for each button in your button column instead of populating table field's text. Just assign value of PerpetualText property.

    When you define a button column, you specify a command associated with the button. The Click event of DataGridXPButtonColumn class occurs when the user clicks the button, the button's command is passed to a container where you can handle it with your custom code.

    Download the DataGridXPButtonColumn style samples for VB.NET and C# that show how you can use the RustemSoft DataGrid Column styles in Windows Forms .NET datagrid.

    VB .NET

    ' Set datagrid column styles
    With TableStyle.GridColumnStyles
    ' Set datagrid ColumnStyle for Car field
    .Add(New DataGridTextBoxColumn(tblCrrncMngr.GetItemProperties.Item("Car")))
    With .Item(0)
    .MappingName = "Car"
    .HeaderText = "Car Name"
    .Width = 130
    .NullText = String.Empty
    .ReadOnly = True
    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 = 200
    .NullText = String.Empty
    End With

    ......................

    ' Set datagrid XP Style Button ColumnStyle for City field
    .Add(New DataGridXPButtonColumn(SystemColors.ControlText, SystemColors.Control))
    ' Also you may set datagrid Button ColumnStyle for City
    ' field without XP Button Style as the following:
    ' .Add(New DataGridButtonColumn())
    With .Item(3)
    .MappingName = "City"
    .HeaderText = "City"
    .Width = 60
    .NullText = String.Empty
    End With

    End With

    ......................

    ' 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, 3)) Then
    strValue = "Nothing :("
    Else
    strValue = ColumnValueAtRow + " city, " + DataGrid1.Item(DataGrid1.CurrentRowIndex, 2) + "!"
    End If
    MessageBox.Show("You have chosen " + strValue, "DataGrid Button is clicked!")
    End If
    End Sub

    C#

    // Set column styles
    // Set datagrid ColumnStyle for Car field
    DataGridTextBoxColumn colCar = new DataGridTextBoxColumn();
    colCar.MappingName = "Car";
    colCar.HeaderText = "Car Name";
    colCar.Width = 130;
    colCar.NullText = String.Empty;
    colCar.ReadOnly = true;
    TblStyle.GridColumnStyles.Add(colCar);

    // Set datagrid ComboBox ColumnStyle for PubID field
    DataTable tblCompanies = ds.Tables["Companies"];
    TblStyle.GridColumnStyles.Add(new DataGridComboBoxColumn(ref tblCompanies, 1, 0, true, false, true));
    // 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"
    TblStyle.GridColumnStyles[1].MappingName = "PubID";
    TblStyle.GridColumnStyles[1].HeaderText = "Company ID";
    TblStyle.GridColumnStyles[1].Width = 200;
    TblStyle.GridColumnStyles[1].NullText = String.Empty;

    ......................

    // Set datagrid XP Style Button ColumnStyle for City field
    TblStyle.GridColumnStyles.Add(new
    DataGridXPButtonColumn(SystemColors.ControlText, SystemColors.Control));
    // Also you may set datagrid Button ColumnStyle for City
    // field without XP Button Style as the following:
    // .Add(New DataGridButtonColumn())
    TblStyle.GridColumnStyles[3].MappingName = "City";
    TblStyle.GridColumnStyles[3].HeaderText = "City";
    TblStyle.GridColumnStyles[3].Width = 60;
    TblStyle.GridColumnStyles[3].NullText = String.Empty;

    ......................

    // DataGridXPButtonColumn Click event runs when the datagrid XPButtonColumn Style's
    // Button control is clicked. The procedure receives three arguments:
    // sender as DataGridXPButtonColumn, ColumnMappingName and ColumnValueAtRow of String type
    public void XPButtonColumn_Click(DataGridXPButtonColumn sender, string ColumnMappingName, string ColumnValueAtRow)
    {
    if (ColumnMappingName == "City") {
    String strValue;
    if (DataGrid1[DataGrid1.CurrentRowIndex, 3] == DBNull.Value) {
    strValue = "Nothing :(";
    }
    else {
    strValue = ColumnValueAtRow + " city, " + DataGrid1[DataGrid1.CurrentRowIndex, 2] + "!";
    }
    MessageBox.Show("You have chosen " + strValue, "DataGrid Button is clicked!");
    }
    }


    Learn more about DataGridColumns assembly

    How to change the background color for DataGridComboBoxColumn columns in a datagrid?

    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.
    This code assumes that an instance of a DataGridComboBoxColumn grid column style has been created on your custom DataGrid.

    VB .NET

    Private Sub DataGrid1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Enter
    ' Identify TempColumn object that is the DataGridComboBoxColumn's "child"
    Dim TempColumn As DataGridComboBoxColumn = DataGrid1.TableStyles(0).GridColumnStyles(1)
    ' Change combobox background color to red
    TempColumn.combo.BackColor = Color.Red
    ' Change combobox characters' color to yellow
    TempColumn.combo.ForeColor = Color.Yellow
    End Sub

    C#

    // Identify TempColumn object that is the DataGridComboBoxColumn's "child"
    DataGridComboBoxColumn TempColumn =
    (DataGridComboBoxColumn) DataGrid1.TableStyles[0].GridColumnStyles[3];
    // Change combobox background color to red
    TempColumn.combo.BackColor = Color.Red;
    / Change combobox characters' color to yellow
    TempColumn.combo.ForeColor = Color.Yellow;

    How to change the display format for DateTimePicker columns in a datagrid?

    The DataGridDateTimePicker's picker object has the same properties as the .NET DateTimePicker control.
    The following example sets the CustomFormat property so that the will display the date as "12/31/2003".
    This code assumes that an instance of a DataGridDateTimePicker grid column style has been created on your custom DataGrid.

    To empty a DateTimePickerColumn cell runtime just click the right mouse button when the cell is selected.
    VB .NET

    Private Sub DataGrid1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Enter
    ' Identify TempColumn object that is the DataGridDateTimePicker's "child"
    Dim TempColumn As DataGridDateTimePicker = DataGrid1.TableStyles(0).GridColumnStyles(2)
    ' Set the Format type and the CustomFormat string
    TempColumn.picker.Format = DateTimePickerFormat.Custom
    TempColumn.picker.CustomFormat = "MM/dd/yyyy"
    ' for German people: (you can see the date as "31.12.2003")
    'System.Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("de-DE")
    'TempColumn.picker.CustomFormat = "dd.MM.yy"

    ' Change Picker's calendar title background color
    TempColumn.picker.CalendarTitleBackColor = Color.Red
    End Sub

    C#

    // Identify TempColumn object that is the DataGridDateTimePicker's "child"
    DataGridDateTimePicker TempColumn =
    (DataGridDateTimePicker) DataGrid1.TableStyles[0].GridColumnStyles[4];
    // Set the Format type and the CustomFormat string
    TempColumn.picker.Format = DateTimePickerFormat.Custom;
    TempColumn.picker.CustomFormat = "MM/dd/yyyy";
    // for German people: (you can see the date as "31.12.2003")
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE");
    TempColumn.picker.CustomFormat = "dd.MM.yy";

    // Change Picker's calendar title background color
    TempColumn.picker.CalendarTitleBackColor = Color.Red;

    To empty a DateTimePickerColumn cell runtime just click the right mouse button when the cell is selected.

    Learn more about DataGridColumns assembly

    How to setup the combo box that filters values from a related child table?

    The DataGridComboBoxColumn column style control gives an ability to get a nesting combo box that filters values from a related child table. In a dictionary grid, you can view and edit related data in a child table.
    In our sample database we have "Corps" Master table that has a one-to-many relationship with a "Cars" Child table; so for each row of the "Corps" table in Dictionary grid, we can view and edit the related rows of the "Cars" child table in a dictionary grid. These two tables are related each other by fields key: "ID + Corp".
    For this case we have to define two string arrays with key fields' names MasterFields and ChildFields. To get the filtering we must identify Master and Child tables' key fields. These fields' names in both arrays must have the same order sequence.

    VB .NET

    ' Identify "Corps" Master and "Cars" Child tables' key fields
    Dim dc As DataGridComboBoxColumn = DataGrid1.TableStyles(0).GridColumnStyles(2)
    ' Fill MasterFields array by Master "Corps" table key fields' names
    dc.combo.MasterFields(0) = "ID"
    dc.combo.MasterFields(1) = "Corp"

    ' Fill ChildFields array by Child "Cars" table key fields' names
    dc.combo.ChildFields(0) = "ID"
    dc.combo.ChildFields(1) = "Corp"

    C#

    // Identify "Corps" Master and "Cars" Child tables' key fields
    DataGridComboBoxColumn dc =
    (DataGridComboBoxColumn) DataGrid1.TableStyles[0].GridColumnStyles[2];
    // Fill MasterFields array by Master "Corps" table key fields' names
    dc.combo.MasterFields[0] = "ID";
    dc.combo.MasterFields[1] = "Corp";

    // Fill ChildFields array by Child "Cars" table key fields' names
    dc.combo.ChildFields[0] = "ID";
    dc.combo.ChildFields[1] = "Corp";

    Learn more about DataGridColumns assembly

    How to specify what Dictionary datagrid columns will be shown?

    DataGridComboBoxColumn ActivateDictionary event runs when the datagrid ComboBoxColumn Style's
    nesting Dictionary datagrid is activated (the additional dropdown button is clicked).
    We use the procedure to specify what dictionary columns will be shown.

    VB .NET

    ' Define "Models" Dictionary Grid Column
    ModelColumn = DataGrid1.TableStyles(0).GridColumnStyles(3)

    .............

    Private Sub ModelColumn_ActivateDictionary(ByVal sender As DataGridComboBoxColumn) Handles ModelColumn.ActivateDictionary
    Dim TableStyle As New DataGridTableStyle()
    Dim DictionrGridColumns As GridColumnStylesCollection = TableStyle.GridColumnStyles

    ' Define columns
    With DictionrGridColumns
    .Add(New DataGridTextBoxColumn())
    With .Item(0)
    .MappingName = "ModelID"
    .HeaderText = "Model ID"
    .Width = 40
    .NullText = String.Empty
    End With
    .Add(New DataGridTextBoxColumn())
    With .Item(1)
    .MappingName = "Model"
    .HeaderText = "Model"
    .Width = 60
    .NullText = String.Empty
    End With
    .Add(New DataGridComboBoxColumn(ds.Tables.Item("BodyStyle"), 0, 0))
    With .Item(2)
    .MappingName = "Body Style"
    .HeaderText = "Body Style"
    .Width = 120
    .NullText = String.Empty
    End With
    .Add(New DataGridNumericColumn(True, True, Year(Now), 1955, 2020, 4))
    With .Item(3)
    .MappingName = "Year"
    .HeaderText = "Year"
    .Width = 50
    .NullText = String.Empty
    End With
    End With
    ' Define Dictionary Grid Columns Styles
    ModelColumn.combo.DictionaryGridColumns = DictionrGridColumns
    ' Define Dictionary Grid Height
    ModelColumn.combo.DictionaryGridHeight = 250
    ' Define Dictionary Grid Width
    ModelColumn.combo.DictionaryGridWidth = 350
    End Sub

    C#

    // Define "Models" Dictionary Grid Column
    ModelColumn = (DataGridComboBoxColumn) DataGrid1.TableStyles[0].GridColumnStyles[3];
    // ModelColumn object handles the
    // RustemSoft.DataGridColumns.DataGridComboBoxColumn.ActivateDictionary event
    ModelColumn.ActivateDictionary +=
    new RustemSoft.DataGridColumns.DataGridComboBoxColumn.ActivateDictionaryEventHandler(this.ModelColumn_ActivateDictionary);

    .............

    private void ModelColumn_ActivateDictionary(DataGridComboBoxColumn sender)
    {
    DataGridTableStyle TableStyle = new DataGridTableStyle();
    GridColumnStylesCollection DictionrGridColumns = TableStyle.GridColumnStyles;

    // Define columns
    DictionrGridColumns.Add(new DataGridTextBoxColumn());
    DictionrGridColumns[0].MappingName = "ModelID";
    DictionrGridColumns[0].HeaderText = "Model ID";
    DictionrGridColumns[0].Width = 40;
    DictionrGridColumns[0].NullText = String.Empty;

    DictionrGridColumns.Add(new DataGridTextBoxColumn());
    DictionrGridColumns[1].MappingName = "Model";
    DictionrGridColumns[1].HeaderText = "Model";
    DictionrGridColumns[1].Width = 60;
    DictionrGridColumns[1].NullText = String.Empty;

    DataTable tblBodyStyle = ds.Tables["BodyStyle"];
    // One more DataGridComboBoxColumn object is enclosed into the datagrid
    DictionrGridColumns.Add(new DataGridComboBoxColumn(ref tblBodyStyle, 0, 0, true, false, true, RustemSoft.DataGridColumns.DataGridComboBoxColumn.DisplayModes.ShowDisplayMember,0));
    DictionrGridColumns[2].MappingName = "Body Style";
    DictionrGridColumns[2].HeaderText = "Body Style";
    DictionrGridColumns[2].Width = 120;
    DictionrGridColumns[2].NullText = String.Empty;

    DictionrGridColumns.Add(new DataGridNumericColumn(true, true, DateTime.Now.Year, 1955, 2020, 4, null));
    DictionrGridColumns[3].MappingName = "Year";
    DictionrGridColumns[3].HeaderText = "Year";
    DictionrGridColumns[3].Width = 50;
    DictionrGridColumns[3].NullText = String.Empty;

    // Define Dictionary Grid Columns Styles
    ModelColumn.combo.DictionaryGridColumns = DictionrGridColumns;
    // Define Dictionary Grid Height
    ModelColumn.combo.DictionaryGridHeight = 250;
    // Define Dictionary Grid Width
    ModelColumn.combo.DictionaryGridWidth = 350;
    }

    Learn more about DataGridColumns assembly

    How to format a datagrid column using Numeric, DateTime formats? How to mask data in the column?

    These formatted intelligent DateTimeColumn, NumericColumn, TextFractionsColumn style controls can mask the date, time, numbers, digits, decimal as well as the text fractions. It gives you ability to manage the IP Address, SS#, Phone numbers, etc., and checks the validation, and automatically set the delimiter location.

    VB .NET

    ' Set datagrid DateTime ColumnStyle for TimeFirst field
    .Add(New DataGridDateTimeColumn(DateTimeTextBox.Stencils.HHMM24, Now, ":"))
    With .Item(6)
    .MappingName = "TimeFirst"
    .HeaderText = "Time"
    .Width = 40
    .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

    End With
    End With
    ' Add TableStyle
    DataGrid1.TableStyles.Add(TblStyle)

    ' 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 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

    ' To specify Password column only one text fraction must be defined:
    '' To let accept any symbols for the fraction we need leave first array element empty:
    'PhoneColumn.txtBox.box.FractionsCode(0, 0) = ""
    '' You can insert 30 symbols only into the password fraction
    'PhoneColumn.txtBox.box.FractionsCode(0, 1) = 30
    '' Specify Password Character for the field
    'PhoneColumn.txtBox.box.PasswordChar = "*"

    C#

    // Set datagrid DateTime ColumnStyle for TimeFirst field
    TblStyle.GridColumnStyles.Add(new DataGridDateTimeColumn(DateTimeTextBox.Stencils.HHMM24, DateTime.Now, ":"));
    //TblStyle.GridColumnStyles.Add(new DataGridTextBoxColumn());
    TblStyle.GridColumnStyles[6].MappingName = "TimeFirst";
    TblStyle.GridColumnStyles[6].HeaderText = "Time";
    TblStyle.GridColumnStyles[6].Width = 40;
    TblStyle.GridColumnStyles[6].NullText = String.Empty;

    // Set datagrid Numeric ColumnStyle for Price field
    TblStyle.GridColumnStyles.Add(new DataGridNumericColumn(false, true, null, null, 1000000, null, 2));
    //TblStyle.GridColumnStyles.Add(new DataGridTextBoxColumn());
    TblStyle.GridColumnStyles[7].MappingName = "Price";
    TblStyle.GridColumnStyles[7].HeaderText = "Price";
    TblStyle.GridColumnStyles[7].Width = 60;
    TblStyle.GridColumnStyles[7].NullText = String.Empty;

    // Set datagrid TextFractions ColumnStyle for Phone field
    TblStyle.GridColumnStyles.Add(new DataGridTextFractionsColumn());
    TblStyle.GridColumnStyles[8].MappingName = "Phone";
    TblStyle.GridColumnStyles[8].HeaderText = "Phone";
    TblStyle.GridColumnStyles[8].Width = 80;
    TblStyle.GridColumnStyles[8].NullText = String.Empty;

    // Add TableStyle
    DataGrid1.TableStyles.Add(TblStyle);

    // Define NumericColumn DataGridNumericColumn object for Price field
    DataGridNumericColumn NumericColumn = (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 crire les nombres seulement!";

    // Identify PhoneColumn object that is the DataGridTextFractionsColumn's "child"
    DataGridTextFractionsColumn PhoneColumn = (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;

    // To specify Password column only one text fraction must be defined:
    // To let accept any symbols for the fraction we need leave first array element empty:
    //PhoneColumn.txtBox.box.FractionsCode[0, 0] = "";
    // You can insert 30 symbols only into the password fraction
    //PhoneColumn.txtBox.box.FractionsCode[0, 1] = 30;
    // Specify Password Character for the field
    //PhoneColumn.txtBox.box.PasswordChar = "*";

    Learn more about DataGridColumns assembly

    How to implement a .NET application with the DataGrid Print class?

    The DataGrid Print class is intended to help you create a print output based on your .NET datagrid content. The class has been included into the DataGrid Columns .NET assembly since it is build for .NET WinForms datagrid control and very helpful for your .NET datagrid design. It helps you to implement a .NET Windows Forms application with a print preview and a print support.
    The following example creates a DataGridPrint object.

    VB .NET

    Private PrintGrid As DataGridPrint
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
    Dim fpr As New frmPrint()
    With fpr
    .Title = DataGrid1.CaptionText
    .ShowDialog()
    If .Result > 0 Then
    PrintGrid = New DataGridPrint(PrintDocument1, DataGrid1, .bBlackWhite)
    PrintGrid.PrintTitle = .bTitle
    PrintGrid.Title = .Title
    Select Case .Result
    Case 1 ' Print
    If PrintDialog1.ShowDialog() = DialogResult.OK Then PrintDocument1.Print()
    Case 2 ' Page Setup
    PageSetupDialog1.ShowDialog()
    Case 3 ' Preview
    PrintPreviewDialog1.Icon = fpr.Icon
    PrintPreviewDialog1.ShowDialog()
    End Select
    End If
    End With
    End Sub

    Private Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.HasMorePages = PrintGrid.Print(e.Graphics)
    End Sub

    C#

    private DataGridPrint PrintGrid;
    private void btnPrint_Click(object sender, System.EventArgs e)
    {
    frmPrint fpr = new frmPrint();
    fpr.Title = DataGrid1.CaptionText;
    fpr.ShowDialog();
    if (fpr.Result > 0)
    {
    PrintGrid = new DataGridPrint(printDocument1, DataGrid1, fpr.bBlackWhite);
    PrintGrid.PrintTitle = fpr.bTitle;
    PrintGrid.Title = fpr.Title;
    if (fpr.Result == 1) // Print
    {
    if (printDialog1.ShowDialog() == DialogResult.OK)
    {
    printDocument1.Print();
    }
    }
    else if (fpr.Result == 2) // Page setup
    {
    pageSetupDialog1.ShowDialog();
    }
    else if (fpr.Result == 3) // Preview
    {
    printPreviewDialog1.Icon = fpr.Icon;
    printPreviewDialog1.ShowDialog();
    }
    }
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.HasMorePages = PrintGrid.Print(e.Graphics);
    }

    Learn more about DataGridColumns assembly


    DataGridView DateTime Column

    You have a DataGridView with a DateTime field that displays the date/time as "2/20/2008 7:00:00 PM". You would like to display only the time in the following format "7:00 PM". You would like to insert a new time in the column or update current time value only. Let us suppose that you are European and would like to see in date field something like "20.02.2008". The DataGridViewDateTimeColumn can help you design the concept that will resolve your needs.
    The DateTime Column is used to allow the user to insert formatted date and time, and to display that date/time in your DataGridView.

    Syntax
    DataGridViewDateTimeColumn()

    Some Properties

    box - DateTime textbox object in selected cell. Gets the hosted box control that is an overridden instance of the DateTime textbox control.

    box.DateTimeFormat - Various formats may be set. You may use a date/time format (like MMDDYY, MMDDYYYY, DDMMYY, DDMMYYYY, YYYYMMDD, HHMM12, HHMMSS12, HHMM24, HHMMSS24) to enable the date/time inserting and updating. Default is MMDDYYYY.

    box.DefaultValue - any DateTime value. For example, you can set the DefaultValue to today's date. Default is empty DateTime (null/Nothing).

    box.DelimiterChar - a delimiter between date/time fractions (between days and month or hours and minutes). Default is "/".


    Example  
    The following example creates a DataGridViewDateTimeColumn and adds it into the DataGridView layout.  
    VB .NET   
    ' Set datagrid DateTime Column for TimeFirst field  
    Dim ColumnTime As New DataGridViewDateTimeColumn  
    With ColumnTime  
        .DataPropertyName = "TimeFirst"  
        .HeaderText = "Time"  
        .Width = 60  
        ' Identify selected cell's DateTime format to edit  
        .box.DateTimeFormat = DateTimeStencils.HHMM12  
        ' Identify whole column DateTime format to display in each inactive column  
        .DateTimeDisplayFormat = DateTimeStencils.HHMM12  
        ' Specify Delimiter Character for the field  
        .box.DelimiterChar = "."  
        .box.DefaultValue = New DateTime(2000, 5, 26, 11, 11, 11)  
        ' Identify column selected cell back and foreground color  
        .box.BackColor = Color.LightGray  
        .box.ForeColor = Color.DarkRed  
        ' Define cell warning color  
        .box.ProblemColor = Color.LightPink  
        ' Identify whole column back and fore colors  
        .CellTemplate.Style.BackColor = Color.Ivory  
        .CellTemplate.Style.ForeColor = Color.DarkBlue  
        ' Define cell border style  
        .box.BorderStyle = BorderStyle.None  
        ' You may turn error messages off  
        'ColumnTime.box.ErrMessageVisible = False  
    End With  
    DataGridView1.Columns.Add(ColumnTime)  
     
    C#  
    // Set datagrid DateTime Column for TimeFirst field  
    DataGridViewDateTimeColumn ColumnTime = new DataGridViewDateTimeColumn();  
    ColumnTime.DataPropertyName = "TimeFirst";  
    ColumnTime.HeaderText = "Time";  
    ColumnTime.Width = 60;  
    // Identify selected cell's DateTime format to edit  
    ColumnTime.box.DateTimeFormat = DateTimeStencils.HHMM12;  
    // Identify whole column DateTime format to display in each inactive column  
    ColumnTime.DateTimeDisplayFormat = DateTimeStencils.HHMM12;  
    // Specify Delimiter Character for the field  
    ColumnTime.box.DelimiterChar = ".";  
    ColumnTime.box.DefaultValue = new DateTime(2000, 5, 26, 11, 11, 11).ToString();  
    // Identify column selected cell back and foreground color  
    ColumnTime.box.BackColor = Color.LightGray;  
    ColumnTime.box.ForeColor = Color.DarkRed;  
    // Define cell warning color  
    ColumnTime.box.ProblemColor = Color.LightPink;  
    // Identify whole column back and fore colors  
    ColumnTime.CellTemplate.Style.BackColor = Color.Ivory;  
    ColumnTime.CellTemplate.Style.ForeColor = Color.DarkBlue;  
    // Define cell border style  
    ColumnTime.box.BorderStyle = BorderStyle.None;  
    // You may turn error messages off  
    //ColumnTime.box.ErrMessageVisible = False;  
     
    // Add ColumnTime onto DataGridView layout  
    DataGridView1.Columns.Add(ColumnTime);  
    Please read the DataGridViewColumns assembly documentation to learn more about the DataGridViewDateTimeColumn class provided events and class members.

    Learn more about DataGridView Columns assembly (.NET)


    Visual Basic News and Information Source - www.VBWire.com




    Copyright © 2001-2014 Rustemsoft LLC