DataViewGrid are sometimes referred to as spreadsheet widgets or tabular control. This is due to the way the widget presents the data – via grid views visual and sometimes behavioral similarity to spreadsheet applications.
The DataGridView control provides a powerful and flexible way to display data in a tabular fashion. It can also be utilized to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data. Furthermore, it can be extended in a number of ways to build custom behaviors into your applications. For example, you can programmatically specify your own sorting algorithms, and you can create your own types of cells. Customizing its look and feel is also an easy feat. And last but not least, it can be easily be connected (bounded) to a database source easily using datasource property.
Due to these characteristics, I have personally used this tool to present a group of data for several of the applications that I have built over the past few years.
I am writing this page as a reference for avid programmers who like to use DataViewGrid extensively like me.
Adding Rows
This is how you would add rows. Of course, you can add a loop and add multiple rows.
private void AddRows() { this.dgvTagValues.Rows.Clear(); this.dgvTagValues.Refresh(); this.dgvTagValues.Rows.Add("Rabi","Gurung","Programmer"); this.dgvTagValues.Rows.Add("Tri","Huynh","Developer"); this.dgvTagValues.Rows.Add("Ben","James","SCADA Engineer"); this.dgvTagValues.Rows.Add("Harry","Jones","Manager"); }
Remove Header Column
I don’t really like the ugly column on the extreme left. I feel it is a waste of space and have served no purpose for my application thus far. You will have to edit the DataGridViewColumn.RowHeadersVisible property to FALSE to disable it.
If you like to remove the header rows (the one on the top that shows title), you will have to edit the DataGridViewColumn.ColumnHeadersVisible property to FALSE to disable it.
Column AutoFit / Fill
Sometimes application real estate is not something we have, therefore, we need to be able to show as much data as possible in a given DataViewGrid. For that kind of scenario we have to autofit our column on the largest text for the cell.
You need to use the DataGridViewColumn.AutoSizeMode property; and these are the options you have.
- AllCells: The column width adjusts to fit the contents of all cells in the column, including the header cell.
- AllCellsExceptHeader: The column width adjusts to fit the contents of all cells in the column, excluding the header cell.
- DisplayedCells: The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, including the header cell.
- DisplayedCellsExceptHeader: The column width adjusts to fit the contents of all cells in the column that are in rows currently displayed onscreen, excluding the header cell.
- Fill: The column width adjusts so that the width of all columns exactly fills the display area of the control.
You can edit the AutoSizeMode in the “Property” window (in edit mode on the right-hand side of the IDE). Alternatively, you can programatically apply AutoSizeMode to each individual columns like this.
DataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; DataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
Enable vs ReadOnly
“ReadOnly” is used (set to TRUE) when you you want to allow users to to see and copy, but not modify the data in the DataViewGrid. On the other hand, “Enabled” is used (set to TRUE) in instants when you can completely limit all and any user interaction with the DataViewGrid and its data.
Happy Programming!!!
Reference