The DataGridView control

The DataGridView control

This topic uses the program code from the topic:


Contents


1. What is the purpose of the DataGridView control?

In Microsoft Visual Studio, the dataGridView control is designed for use in applications created using the Windows Forms Application template. This control allows you to organize data in the form of a table. Data can be obtained from a database, a collection, internal variables – arrays or other program objects.

This control is similar to the TStringGrid component in Delphi.

This control is placed on the ToolBox toolbar in the “All Windows Forms” or “Data” tabs (Figure 1).

After placement on the form, the system creates an object (variable) named dataGridView1. Using this name, you can programmatically manipulate the methods and properties of this control.

Figure 1. The DataGridView1 control and Properties window

 

2. Is it possible to use the DataGridView directly without linking it to a database?

Yes, it is. In the DataGridView, data can be retrieved from a database, a collection, internal data structures (arrays, structures, etc.).

 

3. How to programmatically set the size of the DataGridView? Properties Width, Height

To set the size of the DataGridView, the Width and Height properties are used.

// set the size of dataGridView1
// Height - 200 pixels. Width - 350 pixels
dataGridView1.Width = 350;
dataGridView1.Height = 200;

 

4. What kinds of data can be represented in the DataGridView cells?

Types of data that can be represented in dataGridView cells:

  • dataGridViewButtonColumn. Cells are represented as buttons of type Button;
  • dataGridViewCheckBoxColumn. The cells are represented by CheckBox controls, which allow you to select several options (options) from the set of proposed ones;
  • dataGridViewComboBoxColumn. The cells are represented by the ComboBox type control elements, designed to select one of several options;
  • dataGridViewImageColumn. Table cells are images of type Image;
  • dataGridViewLinkColumn. Table cells are represented by references;
  • dataGridViewTextBoxColumn. This option is proposed by default when adding (creating) a new column. In this case, the table cells are represented as input fields. This allows you to enter data into a table as a matrix.

 

5. Adding a column programmatically. Property “Column”. Methods Add() and Insert()

You can add a column in the dataGridView in two ways:

  • with the help of a special master;
  • programmatically.

Columns in the dataGridView are organized as a Columns collection of the DataGridViewColumnCollection type. To add a column programmatically, use the Add method (command) from the Columns collection.

The Add method has 2 implementation options:

int DataGridViewColumnCollection.Add(DataGridViewColumn dataGridViewColumn);

int DataGridViewColumnCollection.Add(string ColumnName, string HeaderText);

where

  • DataGridViewColumn – type System.Windows.Forms.Column that is added;
  • ColumnName – the name by which the column will be referenced from other methods;
  • HeaderText – text that will be displayed in the header of the column.

A snippet of code that adds two arbitrary columns is the following:

// Add a column named column-1, the header of the column is "Header column-1"
dataGridView1.Columns.Add("column-1", "Header column - 1");

// Add a column named 'column-2'
dataGridView1.Columns.Add("column-2", "Header column - 2");

In real programs, the name of the column and its header are obtained from other controls, for example, TextBox.

To insert a column, use the Insert method, which has the following declaration

void DataGridViewColumnCollection.Insert(int columnIndex, DataGridViewColumn dataGridViewColumn);

Calling this method from the program code is similar to the Add method.

 

6. How to programmatically implement the removal of the column?

To delete a column, use one of two methods from the Columns collection:

  • the RemoveAt() method – removes the column at the specified index in the collection;
  • the Remove() method removes the column by its name.

General view of the RemoveAt() method:

void DataGridViewColumnCollection.RemoveAt(int index);

where

  • index – the specified index in the collection. Indexes are numbered from 0.
void DataGridViewColumnCollection.Remove(string ColumnName);

where

  • ColumnName – the name of the column (but not the title of the column header) that is specified in the Add() method by the first parameter. Columns in a collection can have the same ColumnName value. If the call to the Remove() method does not have a column named ColumnName, an exception is thrown.

The code snippet for deleting a column using the RemoveAt() method:

// deleting a column at index
int index; // number of the column to be deleted
int n; // the current number of columns in the dataGridView

// set column number to be deleted
index = 1;

// determine the current number of columns in the dataGridView
n = dataGridView1.Columns.Count;

// deleting
if ((n > 0) && (index >= 0) && (index < n))
{
    dataGridView1.Columns.RemoveAt(index);
    label1.Text = "Column deleted";
}
else
{
    label1.Text = "Column not deleted";
}

 

7. Adding a row programmatically. Method Add()

You can add a row in one of two ways:

  • by direct input from the keyboard;
  • programmatically.

The rows in the DataGridView are organized as a collection of Rows, such as dataGridViewRowCollection. Below is an event handler that adds 2 arbitrary rows to the table

// Add rows to the table
if (dataGridView1.Columns.Count <= 0)
{
    label1.Text = "Rows are not added";
    return;
}

dataGridView1.Rows.Add("Ivanov I.I.", 25, "New York");
dataGridView1.Rows.Add("Petrenko P.P.", 38, "Moscow");
label1.Text = "Rows are added";

 

8. Deleting the row programmatically. Methods Remove() and RemoveAt()

To delete a row, use one of two methods:

  • method RemoveAt() – deletes the string at the specified index;
  • the Remove() method removes a string that is an input parameter of the DataGridViewRow type.

The code snippet for the row deletion looks like this:

// Delete row
int nr, nc;
nc = dataGridView1.Columns.Count; // number of columns
nr = dataGridView1.RowCount;

if ((nc > 0) && (nr > 1))
{
    dataGridView1.Rows.RemoveAt(0); // delete the first row
    label1.Text = "Row is deleted";
}
else
{
    label1.Text = "Row is not deleted";
}

 

9. Setting the header text in the specified column programmatically

To set the header text in the specified column, use the HeaderText property. The code snippet for setting the header text in the column with the index 0 is:

// set text in title
int nc = dataGridView1.ColumnCount;

if (nc > 0)
{
    // set new text in the header of the first column
    dataGridView1.Columns[0].HeaderText = "Header - 1";
    label1.Text = "Text is set";
}
else
{
    label1.Text = "Text is not set";
}

 

10. Setting the header alignment in the specified column programmatically

The alignment of the header in the column is specified using the HeaderCell.Style.Alignment property.

Snippet of the alignment setting code in the column header with the index 0:

// title alignment
int nc;
nc = dataGridView1.ColumnCount;

if (nc > 0)
{
    // Set the alignment in the center (horizontally and vertically)
    dataGridView1.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    label1.Text = "Alignment complete";
}
else
{
    label1.Text = "Alignment not performed";
}

 

11. Specifying the title font in the columns programmatically

To set the font in the column headers, use the ColumnHeadersDefaultCellStyle property. This property uses the Font property.

In the code snippet, the Arial font is created, having a size of 12 and italic font.

// set the font of the title
int nc;
nc = dataGridView1.ColumnCount;

// create a font "Arial", size 12, font - "italics"
Font F = new Font("Arial", 12, FontStyle.Italic);

if (nc > 0)
{
    dataGridView1.ColumnHeadersDefaultCellStyle.Font = F;
    label1.Text = "Font is set";
}
else
{
    label1.Text = "The font is not set";
}

 

12. Setting the font color of headings programmatically

To set the font color of the headers programmatically, you must use the ColumnHeaderDefaultCellStyle property. This property has ForeColor and BackColor properties.

int nc;
nc = dataGridView1.ColumnCount;

if (nc > 0)
{
    // create a system font
    Font F = new Font("Arial", 14);

    // set the color in column headers
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Purple;

    // set the font
    dataGridView1.Columns[0].DefaultCellStyle.Font = F;
    label1.Text = "Header color is changed";
}
else
{
    label1.Text = "Color not changed";
}

 

13. Setting the size of dataGridView1 programmatically
// set the size of dataGridView1
dataGridView1.Width = 600;
dataGridView1.Height = 150;

 

14. Set the width of the specified column dataGridView1
// set the width of a column
int nc;
nc = dataGridView1.ColumnCount;

if (nc > 0)
{
    // Укажите ширину столбца с индексом 0
    dataGridView1.Columns[0].Width = 70;
    label1.Text = "Column width is set";
}
else
{
    label1.Text = "Column width is not set";
}

 

15. Setting the height of the specified row dataGridView1
// set the height of row
int nc, nr;
nc = dataGridView1.ColumnCount;
nr = dataGridView1.RowCount;

if ((nc > 0) && (nr > 1))
{
    dataGridView1.Rows[0].Height = 50;
    label1.Text = "Row height is set";
}
else
{
    label1.Text = "Row height is not set";
}

 

16. Setting the alignment in the specified column and row
// alignment in the rows
int nc, nr;
nc = dataGridView1.ColumnCount;
nr = dataGridView1.RowCount;

if ((nc > 0)&&(nr>1))
{
    // alignment for all rows
    dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;

    // alignment for a row with index 0
    dataGridView1.Rows[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

    // alignment for a column with index 0
    dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft;
}

 

17.Setting the font, color of characters and background in the first column

To set the font, color of characters and background in the first column, the DefaultCellStyle property of the column with index 0 is used. This property has the properties Font, BackColor, ForeColor.

Below is an code snippet that sets the color of the font, characters, and background in the dataGridView1.

// font and color in the first column
int nc, nr;
nc = dataGridView1.ColumnCount;
nr = dataGridView1.RowCount;

if ((nc > 0) && (nr > 1))
{
    // create a font
    Font F = new Font("Times New Roman", 10, FontStyle.Bold);

    // color of symbols and background in the first column
    dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Red;
    dataGridView1.Columns[0].DefaultCellStyle.ForeColor = Color.Blue;

    // font in the first column
    dataGridView1.Columns[0].DefaultCellStyle.Font = F;

    label1.Text = "Font and color in the 1st column are changed";
}
else
{
    label1.Text = "Font and color in the 1st column are not changed";
}

 

18. Determining the number of columns
// Determine the number of columns
int n;
n = dataGridView1.Columns.Count;
label1.Text = n.ToString();

 

19. Determining the number of rows
// Determine the number of rows without the title row
int n;
n = dataGridView1.Rows.Count;
label1.Text = (n - 1).ToString();

 

20. Determining the width of the specified column in pixels
// column width in pixels
int w;
int nc;
nc = dataGridView1.Columns.Count;

if (nc > 0)
{
    w = dataGridView1.Columns[0].Width;
    label1.Text = w.ToString();
}

 

21. Determining the height of the specified row in pixels
// Determine the height of the specified row in pixels
int h;
int nr, nc;

nc = dataGridView1.Columns.Count;
nr = dataGridView1.RowCount;

if ((nr>1)&&(nc>0))
{
    h = dataGridView1.Rows[0].Height;
    label1.Text = h.ToString();
}

 


Related topics