Export (0) Print
Expand All
7 out of 10 rated this helpful - Rate this topic

DataGridColumnStyle Class

Specifies the appearance and text formatting and behavior of a System.Windows.Forms.DataGrid control column. This class is abstract.

For a list of all members of this type, see DataGridColumnStyle Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.DataGridColumnStyle
            System.Windows.Forms.DataGridBoolColumn
            System.Windows.Forms.DataGridTextBoxColumn

[Visual Basic]
MustInherit Public Class DataGridColumnStyle
   Inherits Component
   Implements IDataGridColumnStyleEditingNotificationService
[C#]
public abstract class DataGridColumnStyle : Component,
   IDataGridColumnStyleEditingNotificationService
[C++]
public __gc __abstract class DataGridColumnStyle : public
   Component, IDataGridColumnStyleEditingNotificationService
[JScript]
public abstract class DataGridColumnStyle extends Component
   implements IDataGridColumnStyleEditingNotificationService

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The collection of DataGridColumnStyle objects (the GridColumnStylesCollection) is accessed through the System.Windows.Forms.DataGrid control's TableStyles property.

The System.Windows.Forms.DataGrid control automatically creates a collection of DataGridColumnStyle objects for you when you set the DataSource property to an appropriate data source. The objects created actually are instances of one of the following classes that inherit from DataGridColumnStyle: DataGridBoolColumn or DataGridTextBoxColumn class.

To format the data display, set the Format property of the DataGridTextBoxColumn class to one of the formatting values. For more information about valid formatting values, see Date and Time Format Strings and Standard Numeric Format Strings.

You can also create your own set of DataGridColumnStyle objects and add them to the GridColumnStylesCollection. When you do so, you must set the MappingName of each column style to the ColumnName of a DataColumn to synchronize the display of columns with the actual data.

CAUTION   Always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection before adding DataGridTableStyle objects to the GridTableStylesCollection. When you add an empty DataGridTableStyle with a valid MappingName value to the collection, DataGridColumnStyle objects are automatically generated for you. Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection.

When one of the derived classes is instantiated by a System.Windows.Forms.DataGrid control, the class created depends on the DataType of the DataColumn associated with the DataGridColumnStyle object. For example, a DataColumn with its DataType set to System.Boolean will be associated with a DataGridBoolColumn. To determine the type of any DataGridColumnStyle, use the GetType method.

To create your own column classes, you can inherit from DataGridColumnStyle. You might want to do this in order to create special columns that host controls, as exemplified by the DataGridTextBox class, which hosts the TextBox control. For example, you can host an Image control to show pictures in columns, or you can create your own user control to host in the column.

The functionality of the DataGridColumnStyle shouldn't be confused with that of the DataColumn. Whereas the DataColumn contains the properties and methods appropriate to creating a data table's schema, the DataGridColumnStyle contains the properties and methods related to the appearance of an individual column on the screen.

If a row contains a DBNull.Value, the text displayed in the column can be set with the NullText property.

The DataGridColumnStyle class also allows you to specify the behavior of a column while its data is being changed. The BeginUpdate and EndUpdate methods temporarily suspend the drawing of the column while large updates are being made to the column's data. Without this functionality, every change in every cell of the grid would be immediately drawn; this could be distracting to the user and a performance liability.

Several methods allow monitoring of the column as it is edited by the user, including the Edit and Commit events.

Most of the properties and methods of the class are tailored to controlling a column's appearance. But a few, such as the GetColumnValueAtRow and SetColumnValueAtRow allow you to examine and change the value in a specified cell.

Notes to Implementers:  When you inherit from DataGridColumnStyle, you must override the following members: Abort, Commit, Edit, and Paint (twice).

Example

[Visual Basic, C#, C++] The following example creates a DataGridColumnStyle that hosts a DateTimePicker control.

[Visual Basic] 
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

' This example shows how to create your own column style that
' hosts a control, in this case, a DateTimePicker.
Public Class DataGridTimePickerColumn
    Inherits DataGridColumnStyle

    Private customDateTimePicker1 As New CustomDateTimePicker()

    ' The isEditing field tracks whether or not the user is
    ' editing data with the hosted control.
    Private isEditing As Boolean

    Public Sub New()
        customDateTimePicker1.Visible = False
    End Sub

    Protected Overrides Sub Abort(ByVal rowNum As Integer)
        isEditing = False
        RemoveHandler customDateTimePicker1.ValueChanged, _
            AddressOf TimePickerValueChanged
        Invalidate()
    End Sub

    Protected Overrides Function Commit _
        (ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) _
        As Boolean

        customDateTimePicker1.Bounds = Rectangle.Empty

        RemoveHandler customDateTimePicker1.ValueChanged, _
            AddressOf TimePickerValueChanged

        If Not isEditing Then
            Return True
        End If
        isEditing = False

        Try
            Dim value As DateTime = customDateTimePicker1.Value
            SetColumnValueAtRow(dataSource, rowNum, value)
        Catch
        End Try

        Invalidate()
        Return True
    End Function

    Protected Overloads Overrides Sub Edit( _
        ByVal [source] As CurrencyManager, _
        ByVal rowNum As Integer, _
        ByVal bounds As Rectangle, _
        ByVal [readOnly] As Boolean, _
        ByVal displayText As String, _
        ByVal cellIsVisible As Boolean)

        Dim value As DateTime = _
        CType(GetColumnValueAtRow([source], rowNum), DateTime)
        If cellIsVisible Then
            customDateTimePicker1.Bounds = New Rectangle _
            (bounds.X + 2, bounds.Y + 2, bounds.Width - 4, _
            bounds.Height - 4)

            customDateTimePicker1.Value = value
            customDateTimePicker1.Visible = True
            AddHandler customDateTimePicker1.ValueChanged, _
            AddressOf TimePickerValueChanged
        Else
            customDateTimePicker1.Value = value
            customDateTimePicker1.Visible = False
        End If

        If customDateTimePicker1.Visible Then
            DataGridTableStyle.DataGrid.Invalidate(bounds)
        End If

        customDateTimePicker1.Focus()

    End Sub

    Protected Overrides Function GetPreferredSize( _
        ByVal g As Graphics, _
        ByVal value As Object) As Size

        Return New Size(100, customDateTimePicker1.PreferredHeight + 4)

    End Function

    Protected Overrides Function GetMinimumHeight() As Integer
        Return customDateTimePicker1.PreferredHeight + 4
    End Function

    Protected Overrides Function GetPreferredHeight( _
        ByVal g As Graphics, ByVal value As Object) As Integer

        Return customDateTimePicker1.PreferredHeight + 4

    End Function

    Protected Overloads Overrides Sub Paint( _
        ByVal g As Graphics, ByVal bounds As Rectangle, _
        ByVal [source] As CurrencyManager, ByVal rowNum As Integer)

        Paint(g, bounds, [source], rowNum, False)

    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
        ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, _
        ByVal rowNum As Integer, ByVal alignToRight As Boolean)

        Paint(g, bounds, [source], rowNum, Brushes.Red, _
            Brushes.Blue, alignToRight)

    End Sub

    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
        ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, _
        ByVal rowNum As Integer, ByVal backBrush As Brush, _
        ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

        Dim [date] As DateTime = _
        CType(GetColumnValueAtRow([source], rowNum), DateTime)
        Dim rect As Rectangle = bounds
        g.FillRectangle(backBrush, rect)
        rect.Offset(0, 2)
        rect.Height -= 2
        g.DrawString([date].ToString("d"), _
            Me.DataGridTableStyle.DataGrid.Font, foreBrush, _
            RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))

    End Sub

    Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
        MyBase.SetDataGridInColumn(value)
        If Not (customDateTimePicker1.Parent Is Nothing) Then
            customDateTimePicker1.Parent.Controls.Remove(customDateTimePicker1)
        End If
        If Not (value Is Nothing) Then
            value.Controls.Add(customDateTimePicker1)
        End If
    End Sub

    Private Sub TimePickerValueChanged( _
        ByVal sender As Object, ByVal e As EventArgs)

        ' Remove the handler to prevent it from being called twice in a row.
        RemoveHandler customDateTimePicker1.ValueChanged, _
            AddressOf TimePickerValueChanged
        Me.isEditing = True
        MyBase.ColumnStartedEditing(customDateTimePicker1)

    End Sub

End Class

Public Class CustomDateTimePicker
    Inherits DateTimePicker

    Protected Overrides Function ProcessKeyMessage(ByRef m As Message) As Boolean
        ' Keep all the keys for the DateTimePicker.
        Return ProcessKeyEventArgs(m)
    End Function

End Class

Public Class MyForm
    Inherits Form

    Private namesDataTable As DataTable
    Private myGrid As DataGrid = New DataGrid()

    Public Sub New()

        InitForm()

        namesDataTable = New DataTable("NamesTable")
        namesDataTable.Columns.Add(New DataColumn("Name"))
        Dim dateColumn As DataColumn = _
             New DataColumn("Date", GetType(DateTime))
        dateColumn.DefaultValue = DateTime.Today
        namesDataTable.Columns.Add(dateColumn)
        Dim namesDataSet As DataSet = New DataSet()
        namesDataSet.Tables.Add(namesDataTable)
        myGrid.DataSource = namesDataSet
        myGrid.DataMember = "NamesTable"
        AddGridStyle()
        AddData()

    End Sub

    Private Sub AddGridStyle()
        Dim myGridStyle As DataGridTableStyle = _
                    New DataGridTableStyle()
        myGridStyle.MappingName = "NamesTable"

        Dim nameColumnStyle As DataGridTextBoxColumn = _
            New DataGridTextBoxColumn()
        nameColumnStyle.MappingName = "Name"
        nameColumnStyle.HeaderText = "Name"
        myGridStyle.GridColumnStyles.Add(nameColumnStyle)

        Dim customDateTimePicker1ColumnStyle As DataGridTimePickerColumn = _
            New DataGridTimePickerColumn()
        customDateTimePicker1ColumnStyle.MappingName = "Date"
        customDateTimePicker1ColumnStyle.HeaderText = "Date"
        customDateTimePicker1ColumnStyle.Width = 100
        myGridStyle.GridColumnStyles.Add(customDateTimePicker1ColumnStyle)

        myGrid.TableStyles.Add(myGridStyle)
    End Sub

    Private Sub AddData()
        Dim dRow As DataRow = namesDataTable.NewRow()
        dRow("Name") = "Name 1"
        dRow("Date") = New DateTime(2001, 12, 1)
        namesDataTable.Rows.Add(dRow)

        dRow = namesDataTable.NewRow()
        dRow("Name") = "Name 2"
        dRow("Date") = New DateTime(2001, 12, 4)
        namesDataTable.Rows.Add(dRow)

        dRow = namesDataTable.NewRow()
        dRow("Name") = "Name 3"
        dRow("Date") = New DateTime(2001, 12, 29)
        namesDataTable.Rows.Add(dRow)

        dRow = namesDataTable.NewRow()
        dRow("Name") = "Name 4"
        dRow("Date") = New DateTime(2001, 12, 13)
        namesDataTable.Rows.Add(dRow)

        dRow = namesDataTable.NewRow()
        dRow("Name") = "Name 5"
        dRow("Date") = New DateTime(2001, 12, 21)
        namesDataTable.Rows.Add(dRow)

        namesDataTable.AcceptChanges()
    End Sub

    Private Sub InitForm()
        Me.Size = New Size(500, 500)
        myGrid.Size = New Size(350, 250)
        myGrid.TabStop = True
        myGrid.TabIndex = 1
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Controls.Add(myGrid)
    End Sub

    <STAThread()> _
    Public Shared Sub Main()
        Application.Run(New MyForm())
    End Sub

End Class

[C#] 
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public class DataGridTimePickerColumn : DataGridColumnStyle
{
    private CustomDateTimePicker customDateTimePicker1 = 
        new CustomDateTimePicker();

    // The isEditing field tracks whether or not the user is
    // editing data with the hosted control.
    private bool isEditing;

    public DataGridTimePickerColumn() : base()
    {
        customDateTimePicker1.Visible = false;
    }

    protected override void Abort(int rowNum)
    {
        isEditing = false;
        customDateTimePicker1.ValueChanged -=
            new EventHandler(TimePickerValueChanged);
        Invalidate();
    }

    protected override bool Commit
        (CurrencyManager dataSource, int rowNum)
    {
        customDateTimePicker1.Bounds = Rectangle.Empty;

        customDateTimePicker1.ValueChanged -=
            new EventHandler(TimePickerValueChanged);

        if (!isEditing)
            return true;

        isEditing = false;

        try
        {
            DateTime value = customDateTimePicker1.Value;
            SetColumnValueAtRow(dataSource, rowNum, value);
        }
        catch (Exception)
        {
            Abort(rowNum);
            return false;
        }

        Invalidate();
        return true;
    }

    protected override void Edit(
        CurrencyManager source,
        int rowNum,
        Rectangle bounds,
        bool readOnly,
        string displayText,
        bool cellIsVisible)
    {
        DateTime value = (DateTime)
            GetColumnValueAtRow(source, rowNum);
        if (cellIsVisible)
        {
            customDateTimePicker1.Bounds = new Rectangle
                (bounds.X + 2, bounds.Y + 2,
                bounds.Width - 4, bounds.Height - 4);
            customDateTimePicker1.Value = value;
            customDateTimePicker1.Visible = true;
            customDateTimePicker1.ValueChanged +=
                new EventHandler(TimePickerValueChanged);
        }
        else
        {
            customDateTimePicker1.Value = value;
            customDateTimePicker1.Visible = false;
        }

        if (customDateTimePicker1.Visible)
            DataGridTableStyle.DataGrid.Invalidate(bounds);

        customDateTimePicker1.Focus();
    }

    protected override Size GetPreferredSize(
        Graphics g,
        object value)
    {
        return new Size(100, customDateTimePicker1.PreferredHeight + 4);
    }

    protected override int GetMinimumHeight()
    {
        return customDateTimePicker1.PreferredHeight + 4;
    }

    protected override int GetPreferredHeight(Graphics g,
        object value)
    {
        return customDateTimePicker1.PreferredHeight + 4;
    }

    protected override void Paint(Graphics g,
        Rectangle bounds,
        CurrencyManager source,
        int rowNum)
    {
        Paint(g, bounds, source, rowNum, false);
    }

    protected override void Paint(
        Graphics g,
        Rectangle bounds,
        CurrencyManager source,
        int rowNum,
        bool alignToRight)
    {
        Paint(
            g, bounds,
            source,
            rowNum,
            Brushes.Red,
            Brushes.Blue,
            alignToRight);
    }

    protected override void Paint(
        Graphics g,
        Rectangle bounds,
        CurrencyManager source,
        int rowNum,
        Brush backBrush,
        Brush foreBrush,
        bool alignToRight)
    {
        DateTime date = (DateTime)
            GetColumnValueAtRow(source, rowNum);
        Rectangle rect = bounds;
        g.FillRectangle(backBrush, rect);
        rect.Offset(0, 2);
        rect.Height -= 2;
        g.DrawString(date.ToString("d"),
            this.DataGridTableStyle.DataGrid.Font,
            foreBrush, rect);
    }

    protected override void SetDataGridInColumn(DataGrid value)
    {
        base.SetDataGridInColumn(value);
        if (customDateTimePicker1.Parent != null)
        {
            customDateTimePicker1.Parent.Controls.Remove
                (customDateTimePicker1);
        }
        if (value != null)
        {
            value.Controls.Add(customDateTimePicker1);
        }
    }

    private void TimePickerValueChanged(object sender, EventArgs e)
    {
        // Remove the handler to prevent it from being called twice in a row.
        customDateTimePicker1.ValueChanged -=
            new EventHandler(TimePickerValueChanged);
        this.isEditing = true;
        base.ColumnStartedEditing(customDateTimePicker1);
    }
}

public class CustomDateTimePicker : DateTimePicker
{
    protected override bool ProcessKeyMessage(ref Message m)
    {
        // Keep all the keys for the DateTimePicker.
        return ProcessKeyEventArgs(ref m);
    }
}

public class MyForm : Form
{
    private DataTable namesDataTable;
    private DataGrid grid = new DataGrid();
    public MyForm() : base()
    {
        InitForm();

        namesDataTable = new DataTable("NamesTable");
        namesDataTable.Columns.Add(new DataColumn("Name"));
        DataColumn dateColumn = new DataColumn
            ("Date", typeof(DateTime));
        dateColumn.DefaultValue = DateTime.Today;
        namesDataTable.Columns.Add(dateColumn);
        DataSet namesDataSet = new DataSet();
        namesDataSet.Tables.Add(namesDataTable);
        grid.DataSource = namesDataSet;
        grid.DataMember = "NamesTable";
        AddGridStyle();
        AddData();
    }

    private void AddGridStyle()
    {
        DataGridTableStyle myGridStyle = new DataGridTableStyle();
        myGridStyle.MappingName = "NamesTable";

        DataGridTextBoxColumn nameColumnStyle =
            new DataGridTextBoxColumn();
        nameColumnStyle.MappingName = "Name";
        nameColumnStyle.HeaderText = "Name";
        myGridStyle.GridColumnStyles.Add(nameColumnStyle);

        DataGridTimePickerColumn timePickerColumnStyle =
            new DataGridTimePickerColumn();
        timePickerColumnStyle.MappingName = "Date";
        timePickerColumnStyle.HeaderText = "Date";
        timePickerColumnStyle.Width = 100;
        myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);

        grid.TableStyles.Add(myGridStyle);
    }

    private void AddData()
    {

        DataRow dRow = namesDataTable.NewRow();
        dRow["Name"] = "Name 1";
        dRow["Date"] = new DateTime(2001, 12, 01);
        namesDataTable.Rows.Add(dRow);

        dRow = namesDataTable.NewRow();
        dRow["Name"] = "Name 2";
        dRow["Date"] = new DateTime(2001, 12, 04);
        namesDataTable.Rows.Add(dRow);

        dRow = namesDataTable.NewRow();
        dRow["Name"] = "Name 3";
        dRow["Date"] = new DateTime(2001, 12, 29);
        namesDataTable.Rows.Add(dRow);

        dRow = namesDataTable.NewRow();
        dRow["Name"] = "Name 4";
        dRow["Date"] = new DateTime(2001, 12, 13);
        namesDataTable.Rows.Add(dRow);

        dRow = namesDataTable.NewRow();
        dRow["Name"] = "Name 5";
        dRow["Date"] = new DateTime(2001, 12, 21);
        namesDataTable.Rows.Add(dRow);

        namesDataTable.AcceptChanges();
    }

    private void InitForm()
    {
        this.Size = new Size(500, 500);
        grid.Size = new Size(350, 250);
        grid.TabStop = true;
        grid.TabIndex = 1;
        this.StartPosition = FormStartPosition.CenterScreen;
        this.Controls.Add(grid);
    }
 
    [STAThread]
    public static void Main()
    {
        MyForm myForm1 = new MyForm();
        myForm1.ShowDialog();
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.Data.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Data;
using namespace System::Windows::Forms;
using namespace System::Drawing;

// This example shows how to create your own column style that
// hosts a control, in this case, a DateTimePicker.
public __gc class CustomDateTimePicker : public DateTimePicker
{
protected:
    bool ProcessKeyMessage(Message* m)
    {
        // Keep all the keys for the DateTimePicker.
        return ProcessKeyEventArgs(m);
    }
};

public __gc class DataGridTimePickerColumn : public DataGridColumnStyle
{
private:
    CustomDateTimePicker* customDateTimePicker1;

    // The isEditing field tracks whether or not the user is
    // editing data with the hosted control.
    bool isEditing;

public:
    DataGridTimePickerColumn()
    {
        customDateTimePicker1 = new CustomDateTimePicker();
        customDateTimePicker1->Visible = false;
    }

protected:
    void Abort(int /*rowNum*/)
    {
        isEditing = false;
        customDateTimePicker1->ValueChanged -=
            new EventHandler(this, &DataGridTimePickerColumn::TimePickerValueChanged);
        Invalidate();
    }

protected:
    bool Commit
        (CurrencyManager* dataSource, int rowNum)
    {
        customDateTimePicker1->Bounds = Rectangle::Empty;

        customDateTimePicker1->ValueChanged -=
            new EventHandler(this, &DataGridTimePickerColumn::TimePickerValueChanged);

        if (!isEditing)
            return true;

        isEditing = false;

        try
        {
            DateTime value = customDateTimePicker1->Value;
            SetColumnValueAtRow(dataSource, rowNum,__box(value));
        }
        catch (Exception*)
        {
            Abort(rowNum);
            return false;
        }

        Invalidate();
        return true;
    }

protected:
    void Edit(
        CurrencyManager* source,
        int rowNum,
        Rectangle bounds,
        bool /*readOnly*/,
        String* /*displayText*/,
        bool cellIsVisible)
    {
        DateTime value = *dynamic_cast<DateTime*>(GetColumnValueAtRow(source, rowNum));
        if (cellIsVisible)
        {
            customDateTimePicker1->Bounds = Rectangle
                (bounds.X + 2, bounds.Y + 2,
                bounds.Width - 4, bounds.Height - 4);
            customDateTimePicker1->Value = value;
            customDateTimePicker1->Visible = true;
            customDateTimePicker1->ValueChanged +=
                new EventHandler(this, &DataGridTimePickerColumn::TimePickerValueChanged);
        }
        else
        {
            customDateTimePicker1->Value = value;
            customDateTimePicker1->Visible = false;
        }

        if (customDateTimePicker1->Visible)
            DataGridTableStyle->DataGrid->Invalidate(bounds);

        customDateTimePicker1->Focus();
    }

protected:
    Size GetPreferredSize(
        Graphics* /*g*/,
        Object* /*value*/)
    {
        return Size(100, customDateTimePicker1->PreferredHeight + 4);
    }

protected:
    int GetMinimumHeight()
    {
        return customDateTimePicker1->PreferredHeight + 4;
    }

protected:
    int GetPreferredHeight(Graphics* /*g*/,
        Object* /*value*/)
    {
        return customDateTimePicker1->PreferredHeight + 4;
    }

protected:
    void Paint(Graphics* g,
        Rectangle bounds,
        CurrencyManager* source,
        int rowNum)
    {
        Paint(g, bounds, source, rowNum, false);
    }

protected:
    void Paint(
        Graphics* g,
        Rectangle bounds,
        CurrencyManager* source,
        int rowNum,
        bool alignToRight)
    {
        Paint(
            g, bounds,
            source,
            rowNum,
            Brushes::Red,
            Brushes::Blue,
            alignToRight);
    }

protected:
    void Paint(
        Graphics* g,
        Rectangle bounds,
        CurrencyManager* source,
        int rowNum,
        Brush* backBrush,
        Brush* foreBrush,
        bool /*alignToRight*/)
    {
        DateTime date = *dynamic_cast<DateTime*>(GetColumnValueAtRow(source, rowNum));
        Rectangle rect = bounds;
        g->FillRectangle(backBrush, rect);
        rect.Offset(0, 2);
        rect.Height -= 2;
        g->DrawString(date.ToString(S"d"),
            this->DataGridTableStyle->DataGrid->Font,
            foreBrush, RectangleF::op_Implicit(rect));
    }

protected:
    void SetDataGridInColumn(DataGrid* value)
    {
        DataGridColumnStyle::SetDataGridInColumn(value);
        if (customDateTimePicker1->Parent != 0)
        {
            customDateTimePicker1->Parent->Controls->Remove
                (customDateTimePicker1);
        }
        if (value != 0)
        {
            value->Controls->Add(customDateTimePicker1);
        }
    }

private:
    void TimePickerValueChanged(Object* /*sender*/, EventArgs* /*e*/)
    {
        // Remove the handler to prevent it from being called twice in a row.
        customDateTimePicker1->ValueChanged -=
            new EventHandler(this, &DataGridTimePickerColumn::TimePickerValueChanged);
        this->isEditing = true;
        DataGridColumnStyle::ColumnStartedEditing(customDateTimePicker1);
    }
};

public __gc class MyForm : public Form
{
private:
    DataTable* namesDataTable;
    DataGrid* grid;
public:
    MyForm()
    {
        grid = new DataGrid();
        InitForm();

        namesDataTable = new DataTable(S"NamesTable");
        namesDataTable->Columns->Add(new DataColumn(S"Name"));
        DataColumn* dateColumn = new DataColumn
            (S"Date", __typeof(DateTime));
        dateColumn->DefaultValue = __box(DateTime::Today);
        namesDataTable->Columns->Add(dateColumn);
        DataSet* namesDataSet = new DataSet();
        namesDataSet->Tables->Add(namesDataTable);
        grid->DataSource = namesDataSet;
        grid->DataMember = S"NamesTable";
        AddGridStyle();
        AddData();
    }

private:
    void AddGridStyle()
    {
        DataGridTableStyle* myGridStyle = new DataGridTableStyle();
        myGridStyle->MappingName = S"NamesTable";

        DataGridTextBoxColumn* nameColumnStyle =
            new DataGridTextBoxColumn();
        nameColumnStyle->MappingName = S"Name";
        nameColumnStyle->HeaderText = S"Name";
        myGridStyle->GridColumnStyles->Add(nameColumnStyle);

        DataGridTimePickerColumn* timePickerColumnStyle =
            new DataGridTimePickerColumn();
        timePickerColumnStyle->MappingName = S"Date";
        timePickerColumnStyle->HeaderText = S"Date";
        timePickerColumnStyle->Width = 100;
        myGridStyle->GridColumnStyles->Add(timePickerColumnStyle);

        grid->TableStyles->Add(myGridStyle);
    }

private:
    void AddData()
    {

        DataRow* dRow = namesDataTable->NewRow();
        dRow->Item[S"Name"] = S"Name 1";
        dRow->Item[S"Date"] = __box(DateTime(2001, 12, 01));
        namesDataTable->Rows->Add(dRow);

        dRow = namesDataTable->NewRow();
        dRow->Item[S"Name"] = S"Name 2";
        dRow->Item[S"Date"] = __box(DateTime(2001, 12, 04));
        namesDataTable->Rows->Add(dRow);

        dRow = namesDataTable->NewRow();
        dRow->Item[S"Name"] = S"Name 3";
        dRow->Item[S"Date"] = __box(DateTime(2001, 12, 29));
        namesDataTable->Rows->Add(dRow);

        dRow = namesDataTable->NewRow();
        dRow->Item[S"Name"] = S"Name 4";
        dRow->Item[S"Date"] = __box(DateTime(2001, 12, 13));
        namesDataTable->Rows->Add(dRow);

        dRow = namesDataTable->NewRow();
        dRow->Item[S"Name"] = S"Name 5";
        dRow->Item[S"Date"] = __box(DateTime(2001, 12, 21));
        namesDataTable->Rows->Add(dRow);

        namesDataTable->AcceptChanges();
    }

private:
    void InitForm()
    {
        this->Size = System::Drawing::Size(500, 500);
        grid->Size = System::Drawing::Size(350, 250);
        grid->TabStop = true;
        grid->TabIndex = 1;
        this->StartPosition = FormStartPosition::CenterScreen;
        this->Controls->Add(grid);
    }
};

[STAThread]
int main()
{
    MyForm* myForm1 = new MyForm();
    myForm1->ShowDialog();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

DataGridColumnStyle Members | System.Windows.Forms Namespace | DataColumn | DataGrid | DataGridBoolColumn | DataGridTextBox | DataGridTableStyle | DataView | GridColumnStylesCollection

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.