DataGridView Control

DataGridView Control or DataGrid Control

The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data or to edit a table with millions of records.

With the DataGridView control, you can display and edit tabular data from many different kinds of data sources (database). 

The DataGridView control can also be used in unbound mode, with no underlying data source.

In bound mode DataSource and DataMember properties are used to bind data grid view to the data source.

Property :

1. DataSource : It is the name of the data source, for example, DataSet11.  The data source can be DataSet, DataTable, etc. 

For example, DataGridView1.DataSource = “DataSet11”

2. DataMember: It is the name of the table in a dataset such as the Employee table, the DataGridView should display. You do not need to set this property when binding to a data source that contains a single table or list .

For example, DataGrid1.DataMember = “Employee”

3. Columns : By using columns collection, you can add columns, remove columns, and count the columns contained in the DataGridView control. DataGrid1.Columns.Add(columnName, headerText)

For example, DataGridView1.Columns.Add(“Col1”, “Employee No.”)

  • To change the column header using the column’s index DataGridView1.Columns(0).HeaderText = “EMP Name”
  • To change the width of the column do as follow DataGridView1.Columns(0).Width = 300
  1. Rows : A DataGridViewRowCollection that contains all the rows in the DataGridView.
  2. AllowUserToAddRows : Default is True. If it is set to true, then the user can add a new row.
  3. AllowUserToDeleteRows : Default is True. If it is set to true, then the user can delete the row.
  4. AllowUserToOrderColumns : Default is False. If it is set to true, then the user can reposition the column.
  5. AllowUserToResizeColumns : Default is True. If it is set to true, then the user can resize the column’s width.
  6. AllowUserToResizeRows : Default is True. If it is set to true, then the user can resize the row’s height.
  7. AlternatingRowsDefaultCellStyle : It is used to set the cell styles to odd-numbered rows of the DataGridView.
  8. ColumnHeadersVisible : Default is True. If it is set to true, then the column header will be visible.
  9. RowHeadersVisible : Default is True. If it is set to true, then the row header will be visible.
  10. ReadOnly : Default is False.

Event :

1. RowEnter : Occurs when a row receives input focus.

2. RowLeave : Occurs when a row loses input focus

'Coding of Form Load Event. 
‘In Form Load Event, add three columns & set its width. 

        DataGridView1.AllowUserToAddRows = False 
        DataGridView1.Columns.Add("Col1", "Book No.") 
        DataGridView1.Columns.Add("Col2", "Book Name")         DataGridView1.Columns.Add("Col3", "Price") 
        DataGridView1.Columns(0).Width = 80 
        DataGridView1.Columns(1).Width = 100 
        DataGridView1.Columns(2).Width = 70 
'Coding of Add to DataGridViewButton.

        Dim NewRowIndex As Integer = DataGridView1.Rows.Count- 1 
        DataGridView1.Rows(NewRowIndex).Cells(0).Value = TextBox1.Text 
        DataGridView1.Rows(NewRowIndex).Cells(1).Value = TextBox2.Text 
        DataGridView1.Rows(NewRowIndex).Cells(2).Value = TextBox3.Text 
'Coding of Display in TextBox Button. 
‘Selected row should be display in to the TextBox control. 

Dim CurSelRowIndexAs Integer = DataGridView1.CurrentRow.Index 
TextBox1.Text = DataGridView1.Rows(CurSelRowIndex).Cells(0).Value.ToString 
TextBox2.Text = DataGridView1.Rows(CurSelRowIndex).Cells(1).Value.ToString 
TextBox3.Text = DataGridView1.Rows(CurSelRowIndex).Cells(2).Value.ToString 

Leave a Comment