Note: This class is new in the .NET Framework version 2.0.
Displays data in a customizable grid.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class DataGridView
Inherits Control
Implements ISupportInitialize
Dim instance As DataGridView
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
public class DataGridView : Control, ISupportInitialize
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
public ref class DataGridView : public Control, ISupportInitialize
/** @attribute ComVisibleAttribute(true) */
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
public class DataGridView extends Control implements ISupportInitialize
ComVisibleAttribute(true)
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)
public class DataGridView extends Control implements ISupportInitialize
The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor. For more information, see Basic Formatting and Styling in the Windows Forms DataGridView Control.
You can use a DataGridView control to display data with or without an underlying data source. Without specifying a data source, you can create columns and rows that contain data and add them directly to the DataGridView. Alternatively, you can set the DataSource and DataMember properties to bind the DataGridView to a data source and automatically populate it with data. For more information, see Displaying Data in the Windows Forms DataGridView Control.
When working with very large amounts of data, you can set the VirtualMode property to true to display a subset of the available data. Virtual mode requires the implementation of a data cache from which the DataGridView control is populated. For more information, see Data Display Modes in the Windows Forms DataGridView Control.
For additional information about the features available in the DataGridView control, see DataGridView Control (Windows Forms).
Although the DataGridView control replaces and adds functionality to the DataGrid control of previous versions, the DataGrid control is retained for both backward compatibility and future use if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.
The following code example demonstrates how to initialize an unbound DataGridView. To run this example, paste the following code into a form that contains a DataGridView named dataGridView1, and three buttons named addNewRowButton, deleteRowButton, and ledgerStyleButton. Call the SetUpDataGridView and PopulateDataGridView methods from the form's constructor or Load event handler. Ensure all events are associated with their event handlers.
Private Sub ledgerStyleButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ledgerStyleButton.Click
' Create a new cell style.
Dim style As New DataGridViewCellStyle
With style
.BackColor = Color.Beige
.ForeColor = Color.Brown
.Font = New Font("Verdana", 8)
End With
' Apply the style as the default cell style.
dataGridView1.AlternatingRowsDefaultCellStyle = style
ledgerStyleButton.Enabled = False
End Sub
Private Sub SetUpDataGridView()
Me.Controls.Add(dataGridView1)
dataGridView1.ColumnCount = 5
With dataGridView1.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(dataGridView1.Font, FontStyle.Bold)
End With
With dataGridView1
.EditMode = DataGridViewEditMode.EditOnEnter
.Name = "dataGridView1"
.Location = New Point(8, 8)
.Size = New Size(500, 300)
.AutoSizeRowsMode = _
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
.ColumnHeadersBorderStyle = _
DataGridViewHeaderBorderStyle.Raised
.CellBorderStyle = _
DataGridViewCellBorderStyle.Single
.GridColor = SystemColors.ActiveBorder
.RowHeadersVisible = False
.Columns(0).Name = "Release Date"
.Columns(1).Name = "Track"
.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.Columns(2).Name = "Title"
.Columns(3).Name = "Artist"
.Columns(4).Name = "Album"
' Make the font italic for row four.
.Columns(4).DefaultCellStyle.Font = _
New Font(Control.DefaultFont, _
FontStyle.Italic)
.SelectionMode = _
DataGridViewSelectionMode.FullRowSelect
.MultiSelect = False
.BackgroundColor = Color.Honeydew
.Dock = DockStyle.Fill
End With
End Sub
Private Sub PopulateDataGridView()
' Create the string array for each row of data.
Dim row0 As String() = {"11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]"}
Dim row1 As String() = {"4/4/1960", "6", "Fools Rush In", _
"Frank Sinatra", "Nice 'N' Easy"}
Dim row2 As String() = {"11/11/1971", "1", _
"One of These Days", "Pink Floyd", "Meddle"}
Dim row3 As String() = {"4/4/1988", "7", "Where Is My Mind?", _
"Pixies", "Surfer Rosa"}
Dim row4 As String() = {"5/1981", "9", "Can't Find My Mind", _
"Cramps", "Psychedelic Jungle"}
Dim row5 As String() = {"6/10/2003", "13", _
"Scatterbrain. (As Dead As Leaves.)", "Radiohead", _
"Hail to the Thief"}
Dim row6 As String() = {"6/30/1992", "3", "Dress", _
"P J Harvey", "Dry"}
' Add a row for each string array.
With Me.dataGridView1.Rows
.Add(row0)
.Add(row1)
.Add(row2)
.Add(row3)
.Add(row4)
.Add(row5)
.Add(row6)
End With
' Change the order the columns are displayed.
With Me.dataGridView1
.Columns(0).DisplayIndex = 3
.Columns(1).DisplayIndex = 4
.Columns(2).DisplayIndex = 0
.Columns(3).DisplayIndex = 1
.Columns(4).DisplayIndex = 2
End With
End Sub
private void ledgerStyleButton_Click(object sender, System.EventArgs e)
{
// Create a new cell style.
DataGridViewCellStyle style = new DataGridViewCellStyle();
{
style.BackColor = Color.Beige;
style.ForeColor = Color.Brown;
style.Font = new Font("Verdana", 8);
}
// Apply the style as the default cell style.
dataGridView1.AlternatingRowsDefaultCellStyle = style;
ledgerStyleButton.Enabled = false;
}
private void SetUpDataGridView()
{
this.Controls.Add(dataGridView1);
dataGridView1.ColumnCount = 5;
DataGridViewCellStyle style =
dataGridView1.ColumnHeadersDefaultCellStyle;
style.BackColor = Color.Navy;
style.ForeColor = Color.White;
style.Font = new Font(dataGridView1.Font, FontStyle.Bold);
dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
dataGridView1.Name = "dataGridView1";
dataGridView1.Location = new Point(8, 8);
dataGridView1.Size = new Size(500, 300);
dataGridView1.AutoSizeRowsMode =
DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
dataGridView1.ColumnHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Raised;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dataGridView1.GridColor = SystemColors.ActiveBorder;
dataGridView1.RowHeadersVisible = false;
dataGridView1.Columns[0].Name = "Release Date";
dataGridView1.Columns[1].Name = "Track";
dataGridView1.Columns[1].DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleCenter;
dataGridView1.Columns[2].Name = "Title";
dataGridView1.Columns[3].Name = "Artist";
dataGridView1.Columns[4].Name = "Album";
// Make the font italic for row four.
dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Italic);
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.BackgroundColor = Color.Honeydew;
dataGridView1.Dock = DockStyle.Fill;
dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
dataGridView1.CellParsing += new DataGridViewCellParsingEventHandler(dataGridView1_CellParsing);
addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
ledgerStyleButton.Click += new EventHandler(ledgerStyleButton_Click);
dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
}
private void PopulateDataGridView()
{
// Create the string array for each row of data.
string[] row0 = { "11/22/1968", "29", "Revolution 9", "Beatles", "The Beatles [White Album]" };
string[] row1 = { "4/4/1960", "6", "Fools Rush In", "Frank Sinatra", "Nice 'N' Easy" };
string[] row2 = { "11/11/1971", "1", "One of These Days", "Pink Floyd", "Meddle" };
string[] row3 = { "4/4/1988", "7", "Where Is My Mind?", "Pixies", "Surfer Rosa" };
string[] row4 = { "5/1981", "9", "Can't Find My Mind", "Cramps", "Psychedelic Jungle" };
string[] row5 = { "6/10/2003", "13", "Scatterbrain. (As Dead As Leaves.)", "Radiohead", "Hail to the Thief" };
string[] row6 = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };
// Add a row for each string array.
{
DataGridViewRowCollection rows = this.dataGridView1.Rows;
rows.Add(row0);
rows.Add(row1);
rows.Add(row2);
rows.Add(row3);
rows.Add(row4);
rows.Add(row5);
rows.Add(row6);
}
// Change the order the columns are displayed.
{
DataGridViewColumnCollection columns = this.dataGridView1.Columns;
columns[0].DisplayIndex = 3;
columns[1].DisplayIndex = 4;
columns[2].DisplayIndex = 0;
columns[3].DisplayIndex = 1;
columns[4].DisplayIndex = 2;
}
}
void ledgerStyleButton_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
// Create a new cell style.
DataGridViewCellStyle^ style = gcnew DataGridViewCellStyle;
{
style->BackColor = Color::Beige;
style->ForeColor = Color::Brown;
style->Font = gcnew System::Drawing::Font( "Verdana",8 );
}
// Apply the style as the default cell style.
dataGridView1->AlternatingRowsDefaultCellStyle = style;
ledgerStyleButton->Enabled = false;
}
void SetUpDataGridView()
{
this->Controls->Add( dataGridView1 );
dataGridView1->ColumnCount = 5;
DataGridViewCellStyle^ style = dataGridView1->ColumnHeadersDefaultCellStyle;
style->BackColor = Color::Navy;
style->ForeColor = Color::White;
style->Font = gcnew System::Drawing::Font( dataGridView1->Font,FontStyle::Bold );
dataGridView1->EditMode = DataGridViewEditMode::EditOnEnter;
dataGridView1->Name = "dataGridView1";
dataGridView1->Location = Point(8,8);
dataGridView1->Size = System::Drawing::Size( 500, 300 );
dataGridView1->AutoSizeRowsMode = DataGridViewAutoSizeRowsMode::DisplayedCellsExceptHeaders;
dataGridView1->ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle::Raised;
dataGridView1->CellBorderStyle = DataGridViewCellBorderStyle::Single;
dataGridView1->GridColor = SystemColors::ActiveBorder;
dataGridView1->RowHeadersVisible = false;
dataGridView1->Columns[ 0 ]->Name = "Release Date";
dataGridView1->Columns[ 1 ]->Name = "Track";
dataGridView1->Columns[ 1 ]->DefaultCellStyle->Alignment = DataGridViewContentAlignment::MiddleCenter;
dataGridView1->Columns[ 2 ]->Name = "Title";
dataGridView1->Columns[ 3 ]->Name = "Artist";
dataGridView1->Columns[ 4 ]->Name = "Album";
// Make the font italic for row four.
dataGridView1->Columns[ 4 ]->DefaultCellStyle->Font = gcnew System::Drawing::Font( DataGridView::DefaultFont,FontStyle::Italic );
dataGridView1->SelectionMode = DataGridViewSelectionMode::FullRowSelect;
dataGridView1->MultiSelect = false;
dataGridView1->BackgroundColor = Color::Honeydew;
dataGridView1->Dock = DockStyle::Fill;
dataGridView1->CellFormatting += gcnew DataGridViewCellFormattingEventHandler( this, &Form1::dataGridView1_CellFormatting );
dataGridView1->CellParsing += gcnew DataGridViewCellParsingEventHandler( this, &Form1::dataGridView1_CellParsing );
addNewRowButton->Click += gcnew EventHandler( this, &Form1::addNewRowButton_Click );
deleteRowButton->Click += gcnew EventHandler( this, &Form1::deleteRowButton_Click );
ledgerStyleButton->Click += gcnew EventHandler( this, &Form1::ledgerStyleButton_Click );
dataGridView1->CellValidating += gcnew DataGridViewCellValidatingEventHandler( this, &Form1::dataGridView1_CellValidating );
}
void PopulateDataGridView()
{
// Create the string array for each row of data.
array<String^>^row0 = {"11/22/1968","29","Revolution 9","Beatles","The Beatles [White Album]"};
array<String^>^row1 = {"4/4/1960","6","Fools Rush In","Frank Sinatra","Nice 'N' Easy"};
array<String^>^row2 = {"11/11/1971","1","One of These Days","Pink Floyd","Meddle"};
array<String^>^row3 = {"4/4/1988","7","Where Is My Mind?","Pixies","Surfer Rosa"};
array<String^>^row4 = {"5/1981","9","Can't Find My Mind","Cramps","Psychedelic Jungle"};
array<String^>^row5 = {"6/10/2003","13","Scatterbrain. (As Dead As Leaves.)","Radiohead","Hail to the Thief"};
array<String^>^row6 = {"6/30/1992","3","Dress","P J Harvey","Dry"};
// Add a row for each string array.
{
DataGridViewRowCollection^ rows = this->dataGridView1->Rows;
rows->Add( row0 );
rows->Add( row1 );
rows->Add( row2 );
rows->Add( row3 );
rows->Add( row4 );
rows->Add( row5 );
rows->Add( row6 );
}
// Change the order the columns are displayed.
{
DataGridViewColumnCollection^ columns = this->dataGridView1->Columns;
columns[ 0 ]->DisplayIndex = 3;
columns[ 1 ]->DisplayIndex = 4;
columns[ 2 ]->DisplayIndex = 0;
columns[ 3 ]->DisplayIndex = 1;
columns[ 4 ]->DisplayIndex = 2;
}
}
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.DataGridView
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
.NET Framework
Supported in: 2.0