The DataGridView control allows us to display data in tabular format using a powerful and flexible way. It is easier to define basic cell appearance and display formatting of cell values by using DataGridView control. The cell is the key to the DataGridView interaction. The DataGridViewCell base class is used to draw all cells. Each cell in the DataGridView can have its own style of text, background color, front color and font. However, typically several cells share specific characteristics of the style. The default data type is of type Object for the cell's value property.
You can use the DataGridView control to view a small amount of data read - only or scale it to display editable views of very large data sets.
Display and edit tabular data from many different types of data sources with the DataGridView control. It is simple and intuitive to bind data to DataGridView control and is often as simple as configuring the DataSource property. Specify the DataMember property to a string with the list or table to link to if we bind to a data source that contains multiple lists or Tables.
The DataGridView control supports the Windows Forms standard data binding model, thus binding on class instances described below.
Syntax: dataGridView1.Columns[Index].Name = "Column Name";
Example:
Add row in DataGridView
on button click
Example:
Delete row in DataGridView
on button click, First of all we want to check selected row has count grater than 0
and selected row index not having -1
.
Example:
Here, we convert "Release Date" column values to Long date using ToLongDateString()
on CellFormatting
event, for this first we check cell value is not equal to null.
Source Code:
We can change DataGridView ColumnHeaders style ,color and Fonts, etc.
Example:
Complete Source Code:
DataGridView Control Source Code
We use cookies to ensure you get the best experience on our website, if you continue to browse you'll be acconsent with our Cookies Policy.
Got it!