The Use Of DataGridView Class In .NET

Introduction

 
The DataGridView class is used to create the DataGridView control. The control provides similar functionalities as compared to the DataGrid Control. However, it is used to implement user-defined and complex functionalities.
 

Properties of DataGridView Class

  • ColumnCount: Specifies or retrieves the total number of columns displayed in the control
  • CurrentCell:  Specifies or retrieves the currently active cell.
  • CurrentRow:  Retrieves a row that contains the current cell.
  • CurrentCellAddress: Retrieves the row and column indexes of the currently active cell.
  • MultiSelect: Specifies or retrieves a value indicating whether the user can select more than one cell, row, or column simultaneously.
  • NewRowIndex: Retrieves the row index for new records.
  • ReadOnly: Retrieves a value indicating whether the user is allowed to edit the calls.
  • RowCount: Specifies or retrieves the total number of rows displayed in the control.
  • SelectedCells: Retrieves a collection of cells selected by the user
  • SelectionMode:  Specifies or retrieves a value indicating the way to select the cells in the control.
  • ShowCellToolTips:  Specifies or retrieves a value indicating whether a tooltip is displayed when the mouse pointer is placed on a cell.
  • ShowRowErrors: Specifies or retrieves a value indicating whether the row headers will display error for rows containing wrong data entries.
Source code demonstrate how to set the selection mode and tool tips for the DataGridView control
  1. SqlDataAdapter sqldaProducts=new SqlDataAdapter(“Select * from Products”,sqlconproducts);  
  2. DataSet dsetProducts=new DataSet(“Products”);  
  3. sqldaProducts.Fill(dsetProducts,”Products”);  
  4. DataGridView dgvwProducts=new DataGridView();  
  5. dgvwProducts.DataSource=dsetProducts.Tables(“Products”);  
  6. dgvwProducts.Dock=DockStyle.Fill;  
  7. dgvwProducts.MultiSelect=false;  
  8. dgvwProducts.SelectionMode=DataGridViewSelectionMode.CellSelect;  
  9. dgvwProducts.ShowcellToolTips=true;  
  10. dgvwProducts.ShowRowErrors=true;  
  11. dgvwProducts.ReadOnly=true;  
  12. Controls.Add(dgvwProducts);  
  13. DataGridViewCell dgvwCell=dgvwProducts.Rows[0].Cells[1];  
  14. dgvCell.ToolTipText=”Second cell”;  
In this source code, the DataSet object is filled with the records of the Products table. An instance of the DataGridView class is created. The DataGridView control is populated with the records of the Products table by using the DataSource Property. The Dock property adjusts the size of the control to the edges of the form. This is done by using the Fill value defined in the DockStyle enumeration. The MultiSelect property is set to False. This means that the user is not permitted to simultaneously select more than one row, or column.
 
The SelectionMode property is set to the CellSelect value defined in the DataGridViewSelectionMode enumeration. The value indicates that the user can select one or more individual cells. The ShowCellToolTips property is specified as true indicating that a tool tip will be displayed when the mouse pointer is placed on a cell. The ShowRowErrors property is set to true indicating that the row headers will display error for rows containing wrong data entries. The ReadOnly property is specified as true to indicate that the cells of the DataGridView control cannot be modified by the user.
 
An instance of the DataGridViewCell class is created namely, dgvwCell. This object refers to the second cell in the first row of the DataGridView control. The ToolTipText property of the DataGridViewCell sets the tool tip text for the second cell as “second cell”. The tool tip appears when the user points towards the second cell in the first row of the grid.
 

Methods of DataGridView class

  • AreAllCellsSelected :  Retrieves a value, which indicates whether the user has selected all the current cells.
  • GetCellDisplayRectangle: Retrieves the rectangle, which is the display area for a particular cell.
  • HitTest :  Retrieves the position information of a cell, such as row and column indices, by giving the x and y coordinates.
  • Events of DataGridView Class
  • CellContentClick :  Occurs on clicking the content within a cell.
  • CellContentDoubleClick:  Occurs on double-clicking the content within a cell.
  • CellDoubleClick: Occurs on double-clicking anywhere in a cell.
  • CellEnter: Occurs when the active cell changes in the control or when the input focus is obtained by the control.
  • CellFormatting: Occurs when a cell’s content needs to be formatted.
  • CellMouseEnter: Occurs when the user enters the cell using the mouse pointer.
  • ColumnAdded: Occurs on adding a column to the control.
  • ColumnRemoved: Occurs on deleting a column from the control.
  • RowEnter: Occurs when the focus in on a row, which then, becomes the currently active row.
  • RowLeave: Occurs when the focus is taken away from a row and is now no longer the active row.
  • RowAdded: Occurs on adding a new row to the control.
  • RowsRemoved: Occurs on removing a row or multiple rows from the control.
  • RowStateChanged: Occurs on changing the row state, such as receiving or losing focus.
  • SelectionChanged: Occurs on changing the current selection in the control.
  • TextChanged : Occurs on changing the value of the Text property.
Source code demonstrate the use of RowsRemoved and SelectionChanged events of the DataGridView class.
  1. DataGridView dgvwProducts;  
  2. SqlDataAdapter sqldaProducts=new SqlDataAdapter(“Select * from Products”,sqlconProducts);  
  3. DataSet dsetProducts=new DataSet(“Products”);  
  4. sqldaProducts.Fill(destProducts,”Products”);  
  5. dgvwProducts=new DataGridView();  
  6. dgvwProducts.DataSource=dsetroducts.Tables(“Products”);  
  7. dgvwProducts.Dock=DockStyle.Top;  
  8. dgvwProducts.MultiSelect=False;  
  9. dgvwProducts.SelectionMode=DataGridViewSelectionMode.FullRowSelect;  
  10. …………………….  
  11. private void dgvwProducts_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)  
  12. {  
  13.    lblTotalRecs.Text=dgvwProducts.Rows.Count.ToString();  
  14. }  
  15. private void dgvwProducts_SelectionChanged(object sender, EventArgs e)  
  16. {  
  17.    lblRecordNumber.Text=Convert.ToString(dgvwProducts.CurrentRow.Index+1);  
  18. }  
In this source code, the DataSet object is filled with the records of the Products table. An instance of the DataGridView class is created. The DataGridView control is populated with the records of the Products table by using the DataSource property. The Dock property is used to display the control in the top area of the form. This is done by using the Top value defined in the DockStyle enumeration. The MultiSelect property is set to false. This means that the user is not permitted to simultaneously select more than one row or column.
 
The SelectionMode property is set to the FullRowSelect value defined in the DataGridViewSelectionMode enumeration. This means that when the user clicks a cell or a row header, the entire row is selected.
 
The RowsRemoved event occurs when a row is deleted from the DataGridView control. When this event is raised, the total number of rows in the control are retrieved and displayed in the Label control. The SelectionChanged event occurs when there is a change in the current selection. When this event is raised, the current row is retrieved and displayed in a Label control.
 

DataGridView Architecture

 
The .NET framework defines various classes to display and manipulate data in the DataGridView control. These classes reside in the System.Windows.Forms namespace. These classes allow working with different elements of the control such as cells, rows and columns. There are five main classes that can be used along with the DataGridView Class. These are,
 
DataGridViewElement
 
This class is the base class, which is used to refer to the elements of the DataGridView control. The class comprises of two main elements namely, cells and bands . Bands refer to the collection of grouped sub elements in the control.
 
DataGridViewCell
 
This class represents a cell in the DataGridView control. The class is used to determine the location of cells and to customizable cells in the control.
 
DataGridViewBand
 
This class represents a sequential set of elements in the DataGridView control. These elements could be set of rows and columns, which can be modified simultaneously as a group. There are no public constructors defined in this class. Therefore , it is only accessed through rows and columns of the DataGridView Control.
 
DataGridViewColumn
 
This class represents a column in the DataGridView control.
 
DataGridViewRow
 
This class displays a row from the data source of the DataGridView control.
 
Figure displays the hierarchy of these classes.
 
Class Hierarchy
 

Summary

 
The DataGridView class is used to create the DataGridView control. The control provides similar functionalities as compared to the DataGrid Control. RowCount specifies or retrieves the total number of rows displayed in the control. ShowRowErrors specifies or retrieves a value indicating whether the row headers will display error for rows containing wrong data entries. DataGridViewElement class is the base class, which is used to refer to the elements of the DataGridView control. The class comprises of two main elements namely, cells and bands.


Similar Articles