Bunifu Datagrid View

Optimized theme design for high-performance; Flexible data editing, multiple data binding methods, and data export.

Overview

Bunifu Datagrid View is a feature-rich .NET valuable control for displaying data in a tabular format. It comes with beautiful in-built themes and a vast range of functions: data binding, editing, adding, custom sorting, selecting, and support for PDF formats.

Getting Started

This section provides a quick overview of working with the tabular data for WinForms. We shall walk through the entire process of creating a real-world data grid.

Adding BunifuDatagridView at Design Time

It's easy to add BunifuDatagridView control at design time. Start by locating BunifuDatagridView from your toolbox and drag it to your form as shown below. Then customize it to your desired look and feel using properties elaborated on later in this documentation.

Adding BunifuDataGridView at Run Time

BunifuDataGridView works for both C# and VB.NET. We will use the Form’s Load event to write our sample code to create a BunifuDatagridView and populate the table with built-in sample data.

Here's the output for the above code:

Creating a simple data entry application (No code!)

Step 1: Create the following form below with the following controls as shown on the labels.

For the DataGridView set the backgroundColor property to a color value of white.

Step two: Create columns on the Datagrid. Here's how to do it in the design view:

a) Select the Datagrid and click on the glyph play icon ( ▶️ ) at the top right position in order to open the Smart Tag.

b) Click on the Add Column link which opens a dialog that displays Column Collection Editor

c) Add the following columns as shown below

d) Now, run the application, the below output will occur

Appearance

Bunifu DataGrid View allows you to customize the appearance of your tabular data with ease. Here are the features that you can customize:

1. Built-In Themes

BunifuDataGridView offers 16+ different types of themes that you may utilize in your application. The Theme property allows you to get the property's value or set an enumerated theme value for your tabular data.

BunifuDataGridView exposes an array of properties that can be used to customize the theme. The following is a sampling of the properties related to our chosen theme:

In some cases, you may need to change some of the property values in your current theme. The AllowCustomTheming property allows you to get the property's boolean value or set a boolean value that, when set to true, will enable you to edit the property values that were used to design the theme you selected.

Property

Description

CurrentTheme

This property contains an array of properties that allows you to get or set the design attributes of the theme chosen with ease.

a) GridColor

This property allows you to get or set a color value that will be applied on the grid lines separating the cells. It supports the use of RGB and HEX color values.

b) AlternatingRows

This property contains an array of properties that allow you to get or set the design attributes and styles associated with the odd rows.

Property

Description

BackColor

This property allows you to get or set the background color for each cell associated with odd rows. It supports the use of both RGB and HEX color values.

Font

This property allows you to get or set the font style for each cell's text in odd rows.

ForeColor

This property allows you to get or set the text color for each cell in an odd row.

SelectionBackColor

This property allows you to get or set the background color for each odd row cell, which gets applied when the row is selected. It supports the use of both RGB and HEX color values.

SelectionForeColor

This property allows you to get or set the text color for each odd row cell, which gets applied when the row is selected. It supports the use of both RGB and HEX color values.

c) RowsStyle

This property contains an array of properties that allow you to get or set the design attributes and cell styles associated with a non-odd row in the data grid.

Below is a description of the properties that you can modify in the RowsStyle prop:

Property

Description

BackColor

This property allows you to get or set the background color for each cell of non-odd rows. It supports the use of both RGB and HEX color values.

Font

This property allows you to get or set the font style for each cell's text in non-odd rows.

ForeColor

This property allows you to get or set the text color for each cell's text in non-odd rows.

SelectionBackColor

This property allows you to get or set the background color for each non-odd row cell, which gets applied when the row is selected. It supports the use of both RGB and HEX color values.

SelectionForeColor

This property allows you to get or set the text color for each non-odd row cell, which gets applied when the row is selected. It supports the use of both RGB and HEX color values.

d) HeaderStyle

This property contains an array of properties that allow you to get or set the design attributes and styles associated with the column headers.

Below is a description of the properties that you can modify in the HeaderStyle prop:

Property

Description

BackColor

This property allows you to get or set the background color of each column header cell. It supports the use of both RGB and HEX color values.

Font

This property allows you to get or set the font style of the text in the column header cells.

ForeColor

This property allows you to get or set the color for the text in each column header cells.

SelectionBackColor

This property allows you to get or set the background color of the column's header cell, which will be applied when the column is selected. It supports the use of both RGB and HEX color values.

SelectionForeColor

This property allows you to get or set the color of text content in a column header cell, which will be applied when the column is selected. It supports the use of both RGB and HEX color values.

2. Column properties

BunifuDataGridView provides you support for creating and editing both bound and unbound columns with ease. The Below section shows the various properties you can work with to design your column appearance and behavior.

Property name

Description

Columns

This property allows you to get the current collection of columns or set a column collection object containing an array of unbound columns for your tabular data.

The column collection editor is provided in the design view, and it allows you to view, add, update, and delete the existing columns. The columns to be added can be chosen from built-in column types or your own column.

Like the standard DataGridView, the created the unbound column gives you further ability to edit its properties. See how to get more insights on editing the properties provided in the column.

You also can add or edit different types of columns(e.g., Textbox columns, image view columns, e.t.c) using any language you prefer to code with( i.e., the C# and VB.NET languages).

Here's how you can add textbox columns through code:

On running the code above, the below output will occur:

Alternatively, BunifuDataGridView can be bound to a data source, where the columns will be generated automatically. This will be discussed in further detail, later in this documentation.

The columns can be accessed through its column index or using its Name property from the bunifuDataGrid.Columns collection.

A column can be removed using the Remove and RemoveAt methods. The Clear method removes all columns from bunifuDataGrid.Columns collection.

Property name

Description

ColumnHeadersDefaultCellStyle

This property allows you to get the style object applied in the columns or set a cell style object that contains an array of cell style properties that can change the appearance of the column header row.

Here are the properties that you can edit to the preferred style of your column header row.:

To ensure that the properties are working at runtime, ensure the AllowCustomTheming prop is set to true.

Property

Description

ColumnHeadersHeightSizeMode

This property allows you to get or set the behavior of adjusting the column headers' height during runtime. You can specify the following values: EnableResizing, DisableResizing, and AutoSize.

ColumnHeadersHeight

This property allows you to get or set an integer value that will resize the column header.

ColumnHeadersBorderStyle

This property allows you to get or set the border style applied to the column header.

ColumnHeadersVisible

This property allows you to get or set a boolean value that will hide the column header row when set to false. The default value is true.

3. Row properties

BunifuDataGridView enables you to add, populate, edit or remove rows for both bound and unbound data. Here are the properties you can use to design your rows, populate and edit your rows with ease:

Populating your rows:

It is very easy to populate rows with an unbound data with bunifuDataGridView.Rows collection property.

Rows are only populated at runtime. Also, note that to add the rows, you must have the columns in the first place.

Here's how you can add and populate a row in BunifuDataGridView using C# or VB.NET language. This example assumes that you have added 5 DataGridViewTextBoxColumn instances to BunifuDataGridView control's column collection.

Steps to create the unbound data:

Step one: Create the following class with the get and set properties:

Step two: In your Form's Load event write the following code:

Now, after running your application with the codes above, the following output will occur:

You can also use the Rows collection to modify the values in the control or to remove rows. The following example shows you how to modify cell values programmatically.

Styling a row cell

The appearance of a BunifuDataGrid cell can be customized using the bunifuDataGridView.Rows collection Style property. We have the option to style the back color, its selection color, the font, its forecolor, and the alignment of text in a particular cell.

The following example shows you how to style the appearance of a cell back color programmatically. The assumption is that you already have some data in your table.

The above code will output the occurrence below:

4. Selection

BunifuDataGrid enables you to select a single row or cell or multiple rows or cells. Sometimes you want your application to perform actions such as printing out the selected rows or columns based on user selections within the tabular control. Depending on the actions, you may want to restrict the kinds of selection that are possible.

This section will explain the types of selection available for GridControl and its usability.

a) Row selection

You can specify a full row type of selection by setting the BunifuDataGridView.SelectionMode property to FullRowSelect.

When a cell or row header is clicked in the DataGrid, an entire row selection is created.

By default, users can select multiple rows, columns, or cells by dragging with the mouse, pressing CTRL or SHIFT keys while selecting to extend or modify a selection. To prevent this behavior set the MultiSelect property to false.

To retrieve the selected values in a row, refer to the following code:

The selected values are printed in the output screen as follows:

b) Cell selection

A cell selection is specified by setting BunifuDataGridView.Selection property to CellSelect. When a user clicks on a cell, a selection is created on that cell.

The following image shows for multiple mode cell selection:

Here's a code that retrieves the selected cell values:

The selected values are printed in the output screen as follows:

c) Header column selection

BunifuDataGridView allows you to select or deselect rows within a specific column by interacting with the column's header cell. To set this type of selection set the SelectionMode prop to ColumnHeaderSelect.

To make this type of selection work, ensure that the SortMode property for the textbox columns is set to programmatic. The sorting techniques will be discussed in greater detail later in this documentation.

To retrieve the selected values in a row, refer to the following code:

d) Disable selection

The selection can be disabled by setting the Enabled property to False.

5. Custom Tooltips

There are instances where Datagrid cells contain extensive text content, and this information is hidden due to the cell's size. As a result, ToolTips are added to provide support for displaying the entire text content in a pop-up window that appears when the mouse hovers over the cells of a BunifuDataGrid.

The steps below demonstrate how we will override the standard DataGrid tooltip and replace it with a more customizable Bunifu tooltip control. It is assumed that you already have a Datagrid with data in its cells.

Step one: Drag and drop a Bunifu Tooltip control in the form.

Step two: Access the properties of BunifuDataGrid and look out for the ShowCellTooltipsproperty. Set it to false. This will enable us to add our own custom tooltip.

Step three: Go to the event properties of BunifuDataGrid and look out for the CellMouseEnter event. Double-click on its text area to navigate to the code view.

Step four: Write the following code:

Step 5: Run your application. Here's is an example of our application running the codes above, the following output will occur:

Data Binding

BunifuDataGrid has been designed to support the use of bounded data.

Bunifu DataGrid View has data-binding support to work out-of-the-box with all popular data sources like SQL Server, Oracle, DataTable, and IEnumerable, and data providers like LINQ to SQL, ADO.NET, Entity Framework, and WCF Data Service.

If the data source implements the INotifyCollectionChanged interface, then BunifuDataGridView will automatically refresh the UI when an item is added, removed, or while the list cleared.

1. Binding to a Generic Collection List

It's simple and clear to create a generic collection with binding properties, which results in bounded data. The following code illustrates how we can bind a collection of list objects with the BindingList Class to create tabular data for BunifuDataGridView

Note that the column header names will be created automatically based on the property names given to the class objects.

Step 1: Create the following OrderDemo class with its get and set properties:

Step 2: In the form load event of your form write the following code:

Step 3: Run your application. Here's the output of the DataGrid running the codes above code:

2. Binding with a DataTable

Binding with a DataTable is quite easy withBunifuDataGridView. Once your DataTable is set with the rows and columns, you just need to assign it to the DataSource property of BunifuDataGridView. The DataGrid control automatically refreshes the UI when rows are added, removed, or cleared in the DataTable. Here's a code to bind the DataTable to BunifuDataGridView.

Step 1: Create the class with the get and set properties. We will use it and create a list of this class type to hold our table values.

Step 2: In the Form load event, we will create the DataTable and assign it as a data source for BunifuDataGridView. Firstly write the following code to add the Order objects in a list:

Secondly, write the following code snippet to create a DataTable and assign columns and rows to it:

Here's the output upon running the codes above:

3. Binding to XML Data

We can load data easily from an XML file toBunifuDataGridView. This can be achieved by using the DataSet object which provides the necessary methods that are used to read XML data into the dataset.

To generate an XML file, right-click the project's name in the solution explorer tab and select the add option. In the pop-up window displayed navigate to the Data tab and select the XML File item.

The following is an example of XML Data that we will bind to BunifuDataGridView.

Write the following code in the form load event to read the above XML file and display the data in BunifuDataGridView:

Here's the output of the above code:

It is also possible to save the changes back to the XML file.

4. Binding Data from ADO.NET Framework Entity

BunifuDataGridView provides the support to bind data from an ADO.NET Entity Framework. The ADO.NET entity framework is a database access technology that contains object-oriented classes to create high performance when accessing any kind of data source.

ADO .NET uses SQL queries and stored procedures to read write update and delete data from a data source. This walk-through describes binding data from ADO.NET Entity Framework and saving back the changes to the database.

Step 1: Create a data model using the entity framework.

To create the data model:

a) Right-click the project's name in the solution explorer, select the Add option, then click on New Item.

b) In the pop-up wizard displayed, select the Data node, then select the ADO.NET Entity Data Mode item and click the add button.

c) Next, on the wizard displayed as shown below, select the EF Designer from database option and then click Next.

d) In the Choose Your Data Connection wizard, click the new connection button, select Microsoft SQL SERVER as the data source and click the continue button. To configure/modify connection, refer: How to: Create Connections to SQL Server Databases.

This section assumes that you already have a database in your SQL Server. It is important to ensure that you have installed the Microsoft SQL Express Server and its management studio.

Click the OK button after successfully testing the connection.

e) In the choose your data connection Click the Next button . Ensure that you have a connection string displayed on the window and you have enabled/checked the save connection setting option.

f) In the next window select Entity version 6.x as the version entity framework to use, then click the next button.

g) Select the tables that you want to include in your entity model and click the on the finish button.

Once it has finished creating the model, the following diagram will appear as follows in your visual studio:

Step 2: Loading data from the model to BunifuDataGridView:

To bind the data from the entity model, write the following code in the form load event:

The following is the output of the above code where the data has been loaded from the database:

To update a cell value and reflect the changes in the database, write the following code in the cellEndEdit event of BunifuDataGridView

The output of the above code will be as follows:

You can use SQL Queries and Linq to update, create, read and update data in the ADO.NET Entity Framework

The following example shows how to retrieve the orders data using an SQL Query and bind the data inBunifuDataGridView using the form load event:

References: https://docs.microsoft.com/en-us/ef/ef6/fundamentals/databinding/winforms

5. Binding Data from MySQL Database (Using Micron)

The Micron library allows us to easily query data from both MySQL and MariaDB database engines. It automatically creates models that match your database schema. To install and configure the Micron library, refer: How to install the Micron ORM library.

Below is the walk-through that describes binding data from the MySQL database using the Micron ORM Framework and saving back the changes to the database.

Step 1: Connecting to MySQL Database:

To connect to a MySQL database :

First, download the demo database file below and import it in your MySQL database.

46KB
Open
Demo Inventory Database

b) Write the following code in the main entry point of your application:

c) Open Nuget Package Manager Console and execute command below:

update-micron database-first

This tests the connection set above and on a successful connection it creates the models matching the database schema

The following folder will be created with the models from the database tables:

Step 2: Loading data from the customers model to BunifuDataGridView

Micron returns IEnumerable<T> interface when retrieving data from a model. Write the following code to select data from the customers table:

The above code outputs the following MySQL data in the Datagrid:

Data virtualization

BunifuDataGridView provides support to handle a large amount of data through built-in virtualization features.

With Data virtualization, there is the processing of the data in on-demand for better performance while loading a large amount of data. A large amount of data can be loaded in less time by setting VirtualMode property to true.

Exporting Data to PDF(using Bunifu Reports)

Bunifu reports is a special kind of reporting tool that can be integrated seamlessly into your visual studio project. It allows you as a developer to add the reporting module that can generate pdf content from any .NET control that conveys data to the user.

To install and configure Bunifu reports, refer to NuGet Gallery | Bunifu.Reports 1.0.5

Below is the walk-through that describes how to export BunifuDatagridView content to a PDF file using Bunifu reports:

Step one: In the design view, drag and drop a Bunifu report component from the toolbox to your form.

Step two: Have your BunifuDatagridView populated with ready data from any data source.

Step three: Create a save button in your form and double click it to navigate to the code view.

Step four: Write the following code in the click event of the button:

Here's an output of a pdf generated DatagridView report:

Take Away

We hope you have gained insights into using BunifuDataGridView and that the knowledge will help you create a better tabular data application interface for your users. Should you have feedback or suggestions, please send us via chat on the bottom right corner of the screen.

Last updated

Was this helpful?