VIBlend
Home Products Downloads Purchase Support Forum About Blog

Format DataGridView Cells

by viblend 7. June 2012 17:32
In this post, we will demonstrate you how to format the Cells in the VIBlend DataGridView for WinForms.

To achieve this, follow these steps:  

1.Create a new instance of the vDataGridView via drag and drop from the toolbox or create it programmatically.  
2.Bind the DataGrid to the Northwind Invoices table( Nwind.mdb is included in the download of the VIBlend Controls for WinForms package).  
 
C#

NwindDataSetTableAdapters.InvoicesTableAdapter adapter = new DataBindingAndEditors.NwindDataSetTableAdapters.InvoicesTableAdapter();
adapter.Fill(nwindDataSet1.Invoices);

DataTable tbl = this.nwindDataSet1.Invoices;

vDataGridView1.BoundFields.Clear();
vDataGridView1.BoundPivotColumns.Clear();
vDataGridView1.BoundPivotRows.Clear();
vDataGridView1.BoundPivotValues.Clear();
vDataGridView1.DataSource = null;

for (int i = 0; i < tbl.Columns.Count; i++)
{
    DataColumn col = tbl.Columns[i];
    BoundField boundField = new BoundField(col.Caption, col.ColumnName);
    vDataGridView1.BoundFields.Add(boundField);
}

vDataGridView1.DataSource = tbl;

vDataGridView1.ColumnsHierarchy.AutoResize();
vDataGridView1.RowsHierarchy.AutoResize();
vDataGridView1.Refresh();

VB .NET

Dim adapter As NwindDataSetTableAdapters.InvoicesTableAdapter = New DataBindingAndEditors.NwindDataSetTableAdapters.InvoicesTableAdapter()
adapter.Fill(nwindDataSet1.Invoices)

Dim tbl As DataTable = Me.nwindDataSet1.Invoices

vDataGridView1.BoundFields.Clear()
vDataGridView1.BoundPivotColumns.Clear()
vDataGridView1.BoundPivotRows.Clear()
vDataGridView1.BoundPivotValues.Clear()
vDataGridView1.DataSource = Nothing

Dim i As Integer = 0

Do While i < tbl.Columns.Count
    Dim col As DataColumn = tbl.Columns(i)
    Dim boundField As BoundField = New BoundField(col.Caption, col.ColumnName)
    vDataGridView1.BoundFields.Add(boundField)
    i += 1
Loop

vDataGridView1.DataSource = tbl

vDataGridView1.ColumnsHierarchy.AutoResize()
vDataGridView1.RowsHierarchy.AutoResize()
vDataGridView1.Refresh()

3. Set a custom format string to an entire grid column. 
 
C#

int columnIndex = this.nwindDataSet1.Invoices.Columns.IndexOf(this.nwindDataSet1.Invoices.RequiredDateColumn);
vDataGridView1.ColumnsHierarchy.Items[columnIndex].CellsFormatString = "{0:yyyy/MMMM/dd}";

VB .NET 

Dim columnIndex As Integer = Me.nwindDataSet1.Invoices.Columns.IndexOf(Me.nwindDataSet1.Invoices.RequiredDateColumn)
vDataGridView1.ColumnsHierarchy.Items(columnIndex).CellsFormatString = "{0:yyyy/MMMM/dd}"

 

WinForms DataGridView Search Functionality

by viblend 7. May 2012 00:50

In the latest version of VIBlend Controls for WinForms, we added a new utility control called vDataGridViewSeachHeader.
This small control adds a Search Functionality to the DataGridView and allows the users to easily find a record in a large data set. The post will show you how to associate this control to an instance of the vDataGridView control.

- Drag and drop a new instance of the vDataGridViewSearchHeader.
- Drag and drop a new instance of the vDataGridView.
- Set the DataGridView property of the vDataGridViewSearchHeader to point to your vDataGridView instance.

Here are the DataGridView and its Search Header at design-time.



In the code behind, bind the Grid to a DataTable. In the post, we will use a sample data.

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VIBlend.WinForms.DataGridView;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable table = new DataTable();
            for (int i = 0; i < 100; i++)
            {
                table.Columns.Add("Column " + i);
            }

            for (int i = 0; i < 100; i++)
            {
                string[] values = new string[100];
                for (int j = 0; j < 100; j++)
                {
                    values[j] = "Cell " + i + "." + j;
                }
                table.Rows.Add(values);
            }
            this.vDataGridView1.DataSource = table;
            this.vDataGridView1.ColumnsHierarchy.AutoResize();

        }
    }
}

 
VB .NET

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports VIBlend.WinForms.DataGridView

Namespace WindowsFormsApplication6
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()

            Dim table As New DataTable()
            For i As Integer = 0 To 99
                table.Columns.Add("Column " & i)
            Next i

            For i As Integer = 0 To 99
                Dim values(99) As String
                For j As Integer = 0 To 99
                    values(j) = "Cell " & i + AscW("." )+ j
                Next j
                table.Rows.Add(values)
            Next i
            Me.vDataGridView1.DataSource = table
            Me.vDataGridView1.ColumnsHierarchy.AutoResize()

        End Sub
    End Class
End Namespace

Here's the result:


Creating Rows and Columns Hierarchies in Unbound Mode

by viblend 3. April 2012 16:46

In this post, we will show you how to create rows and column hierarchies in unbound mode and fill the DataGrid cells with data. In order the add Columns in unbound mode, you should use the DataGrid.ColumnsHierarchy.Items.Add method. To add rows in unbound mode, use the DataGrid.RowsHierarchy.Items.Add method. The 'Add' method returns a HierarchyItem object. This allows you to add sub-items to the HierarchyItem because it has Items.Add method, too.

The code below creates a 2-level Rows Hierarchy and can be used with the VIBlend DataGrid control for Silverlight, WPF and Windows Forms. 

C#

this.DataGrid.RowsHierarchy.CompactStyleRenderingEnabled = false;
for (int i = 0; i < 10; i++)
{
    HierarchyItem row = this.DataGrid.RowsHierarchy.Items.Add("Row" + i);
    HierarchyItem column = this.DataGrid.ColumnsHierarchy.Items.Add("Column" + i);

    for (int j = 0; j < 3; j++)
    {
        HierarchyItem subRow = row.Items.Add("Row" + i + "." + j);
    }
}
  
VB .NET

Me.DataGrid.RowsHierarchy.CompactStyleRenderingEnabled = False
For i As Integer = 0 To 9
    Dim row As HierarchyItem = Me.DataGrid.RowsHierarchy.Items.Add("Row" & i)
    Dim column As HierarchyItem = Me.DataGrid.ColumnsHierarchy.Items.Add("Column" & i)

    For j As Integer = 0 To 2
        Dim subRow As HierarchyItem = row.Items.Add("Row" & i + AscW("." )+ j)
    Next j
Next i

To Fill the DataGrid with data in unbound mode, you should use the DataGrid.CellsArea.SetCellValue method. The method accepts three parameteres - row, column and the cell value.

C#

for (int i = 0; i < 10; i++)
{
    HierarchyItem row = this.DataGrid.RowsHierarchy.Items[i];
    for (int j = 0; j < 10; j++)
    {
        HierarchyItem column = this.DataGrid.ColumnsHierarchy.Items[j];
        this.DataGrid.CellsArea.SetCellValue(row, column, "Cell" + i + "." + j);
        for (int p = 0; p < 3; p++)
        {
            this.DataGrid.CellsArea.SetCellValue(row.Items[p], column, "Cell" + p + "." + j);             
        }
    }
}
       
this.DataGrid.RowsHierarchy.ExpandAllItems();
this.DataGrid.ColumnsHierarchy.ExpandAllItems();
this.DataGrid.RowsHierarchy.AutoResize(AutoResizeMode.FIT_ALL);
this.DataGrid.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ALL);
  
VB .NET

For i As Integer = 0 To 9
    Dim row As HierarchyItem = Me.DataGrid.RowsHierarchy.Items(i)
    For j As Integer = 0 To 9
        Dim column As HierarchyItem = Me.DataGrid.ColumnsHierarchy.Items(j)
        Me.DataGrid.CellsArea.SetCellValue(row, column, "Cell" & i + AscW("." )+ j)
        For p As Integer = 0 To 2
            Me.DataGrid.CellsArea.SetCellValue(row.Items(p), column, "Cell" & p + AscW("." )+ j)
        Next p
    Next j
Next i

Me.DataGrid.RowsHierarchy.ExpandAllItems()
Me.DataGrid.ColumnsHierarchy.ExpandAllItems()
Me.DataGrid.RowsHierarchy.AutoResize(AutoResizeMode.FIT_ALL)
Me.DataGrid.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ALL)

Bind the VIBlend DataGridView for WinForms to SQL Database

by viblend 16. March 2012 00:01

This blog post will show you how to bind the WinForms DataGridView to the Customer table of the AdventureWorksLT Database.

To achieve this, follow these steps:  

1. Create a new instance of the vDataGridView via drag and drop from the toolbox or create it programmatically.  

2. Create a new AdventureWorksLTDataSet. In order to create it, do the following: Select Data->Add New DataSource->DataBase->DataSet. Add a connection to the AdventureWorksLT Database and Click the 'Next' button. Then expand the 'Tables' tree node, select Customer and click the 'Finish' button.

Note: The AdventureWorksLT Database is installed with the installation of the SQL Server. However, you can also download and install it from http://sqlserversamples.codeplex.com  

 

3. Create a new instance of the AdventureWorksLTDataSet data set. Then create a new CustomerTableAdapter and fill the Customer table with data. Finally, bind the VIBlend DataGrid for WinForms to the Customer table. 

C#

AdventureWorksLTDataSet dataSet = new AdventureWorksLTDataSet();
AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter adapter = new AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter();
adapter.Fill(dataSet.Customer);
this.vDataGridView1.VIBlendTheme = VIBlend.Utilities.VIBLEND_THEME.OFFICE2010BLUE;
this.vDataGridView1.DataSource = dataSet.Customer;
this.vDataGridView1.ColumnsHierarchy.AutoResize();

VB .NET

Dim dataSet As New AdventureWorksLTDataSet()
Dim adapter As New AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter()
adapter.Fill(dataSet.Customer)
Me.vDataGridView1.VIBlendTheme = VIBlend.Utilities.VIBLEND_THEME.OFFICE2010BLUE
Me.vDataGridView1.DataSource = dataSet.Customer
Me.vDataGridView1.ColumnsHierarchy.AutoResize()

 

VIBlend Silverlight Controls 6.0 With New Metro UI Themes

by viblend 18. January 2012 16:47

We are happy to announce the availability of VIBlend Silverlight Controls 6.0.  The newest version of VIBlend Silverlight Controls includes a set of new Metro UI Themes - Metro Blue, Metro Green and Metro Orange themes.

To download an evaluation version, please visit our Download page. 

View the updated Online Demo.

 

Display a DataGridView in a Popup

by Admin 13. January 2012 22:48

In this post we will show you how to display the WinForms GridView in a Popup control. We will also use the new Metro Blue theme.

1. The first step is to drag and drop a new instance of the VIBlend DataGridView and the ControlBox control.
The ControlBox control is a control similar to the WinForms ComboBox, but it can display any content in its Popup.

2. The next step is to initialize the Grid. We will add 5 rows and 5 columns and will populate it in unbound mode. The first column will display images in the grid cells. The rest columns will be traditional text columns.

- Create the Grid Rows and Columns:

C#

for (int i = 0; i < 5; i++)
{
    Grid1.RowsHierarchy.Items.Add(i.ToString());
    Grid1.ColumnsHierarchy.Items.Add("Column " + i);
}

VB .NET

For i As Integer = 0 To 4
    Grid1.RowsHierarchy.Items.Add(i.ToString())
    Grid1.ColumnsHierarchy.Items.Add("Column " & i)
Next i

Fill the first grid column with images. In order to display an image in a grid cell, you need to do two things - set the cell's display settings to DisplaySettings.ImageOnly and call the SetCellImage method passing the cell's row, column and image index parameters.

C#

Grid1.ImageList = imageList1;
HierarchyItem columnImage = Grid1.ColumnsHierarchy.Items[0];

for (int i = 0; i < Grid1.RowsHierarchy.Items.Count; i++)
{
    HierarchyItem rowItem = Grid1.RowsHierarchy.Items[i];
    Grid1.CellsArea.SetCellDisplaySettings(rowItem, columnImage, DisplaySettings.ImageOnly);
    Grid1.CellsArea.SetCellImage(rowItem, columnImage, i);
}

VB .NET
 
Grid1.ImageList = imageList1
Dim columnImage As HierarchyItem = Grid1.ColumnsHierarchy.Items(0)

For i As Integer = 0 To Grid1.RowsHierarchy.Items.Count - 1
    Dim rowItem As HierarchyItem = Grid1.RowsHierarchy.Items(i)
    Grid1.CellsArea.SetCellDisplaySettings(rowItem, columnImage, DisplaySettings.ImageOnly)
    Grid1.CellsArea.SetCellImage(rowItem, columnImage, i)
Next i

Fill the rest of the columns by using the SetCellValue method.

C#

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[0], Grid1.ColumnsHierarchy.Items[1], "Porsche Boxster");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[1], Grid1.ColumnsHierarchy.Items[1], "Acura TL");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[2], Grid1.ColumnsHierarchy.Items[1], "Audi A4");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[3], Grid1.ColumnsHierarchy.Items[1], "BMW 645Ci");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[4], Grid1.ColumnsHierarchy.Items[1], "BMW 760Li");

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[0], Grid1.ColumnsHierarchy.Items[2], true);
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[1], Grid1.ColumnsHierarchy.Items[2], true);
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[2], Grid1.ColumnsHierarchy.Items[2], true);
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[3], Grid1.ColumnsHierarchy.Items[2], true);
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[4], Grid1.ColumnsHierarchy.Items[2], true);

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[0], Grid1.ColumnsHierarchy.Items[3], "4 years");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[1], Grid1.ColumnsHierarchy.Items[3], "4 years");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[2], Grid1.ColumnsHierarchy.Items[3], "4 years");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[3], Grid1.ColumnsHierarchy.Items[3], "4 years");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[4], Grid1.ColumnsHierarchy.Items[3], "4 years");

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[0], Grid1.ColumnsHierarchy.Items[4], "www.porsche.com");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[1], Grid1.ColumnsHierarchy.Items[4], "www.acura.com");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[2], Grid1.ColumnsHierarchy.Items[4], "www.audi.com");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[3], Grid1.ColumnsHierarchy.Items[4], "www.bmw.com");
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items[4], Grid1.ColumnsHierarchy.Items[4], "www.bmw.com");

VB .NET

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(0), Grid1.ColumnsHierarchy.Items(1), "Porsche Boxster")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(1), Grid1.ColumnsHierarchy.Items(1), "Acura TL")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(2), Grid1.ColumnsHierarchy.Items(1), "Audi A4")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(3), Grid1.ColumnsHierarchy.Items(1), "BMW 645Ci")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(4), Grid1.ColumnsHierarchy.Items(1), "BMW 760Li")

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(0), Grid1.ColumnsHierarchy.Items(2), True)
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(1), Grid1.ColumnsHierarchy.Items(2), True)
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(2), Grid1.ColumnsHierarchy.Items(2), True)
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(3), Grid1.ColumnsHierarchy.Items(2), True)
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(4), Grid1.ColumnsHierarchy.Items(2), True)

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(0), Grid1.ColumnsHierarchy.Items(3), "4 years")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(1), Grid1.ColumnsHierarchy.Items(3), "4 years")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(2), Grid1.ColumnsHierarchy.Items(3), "4 years")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(3), Grid1.ColumnsHierarchy.Items(3), "4 years")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(4), Grid1.ColumnsHierarchy.Items(3), "4 years")

Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(0), Grid1.ColumnsHierarchy.Items(4), "www.porsche.com")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(1), Grid1.ColumnsHierarchy.Items(4), "www.acura.com")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(2), Grid1.ColumnsHierarchy.Items(4), "www.audi.com")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(3), Grid1.ColumnsHierarchy.Items(4), "www.bmw.com")
Grid1.CellsArea.SetCellValue(Grid1.RowsHierarchy.Items(4), Grid1.ColumnsHierarchy.Items(4), "www.bmw.com")

3. In order to display the Grid in the ControlBox's Popup,  you need to set the ControlBox's ContentControl property to point to the Grid instance.

C#

this.vControlBox.ContentControl = this.Grid1;

VB .NET

Me.vControlBox.ContentControl = Me.Grid1

 

WinForms Grid with Merged Cells

by viblend 4. January 2012 01:06
In this post, we will show you how to use the cells merge functionality of VIBlend DataGridView for WinForms.

The first step is to add the DataGridView to your Form. You can do this by drag and drop from the Visual Studio toolbox or you can create it in the code behind.

C#

private vDataGridView grid = new vDataGridView();

public Form1()
{
    InitializeComponent();
    grid.Dock = DockStyle.Fill;
    grid.VIBlendTheme = VIBLEND_THEME.NERO;
    this.Controls.Add(grid);
    this.LoadData();
    this.MergeCells();
    this.SetCellsValues();
}

VB .NET

Private grid As New vDataGridView()

Public Sub New()
    InitializeComponent()
    grid.Dock = DockStyle.Fill
    grid.VIBlendTheme = VIBLEND_THEME.NERO
    Me.Controls.Add(grid)
    Me.LoadData()
    Me.MergeCells()
    Me.SetCellsValues()
End Sub

Now, we need to implement the LoadData, MergeCells and SetCellValues methods. The LoadData method will add rows and columns to the DataGridView.

C#

private void LoadData()
{
    // add columns.
    for (int i = 1; i <= 8; i++)
    {
        grid.ColumnsHierarchy.Items.Add("Column " + i);
    }
    // add rows.
    for (int i = 1; i <= 25; i++)
    {
        grid.RowsHierarchy.Items.Add("Row " + i);
    }

    grid.RowsHierarchy.AutoResize();
    grid.ColumnsHierarchy.AutoResize();
    grid.Refresh();
}

VB .NET

Private Sub LoadData()
    ' add columns.
    For i As Integer = 1 To 8
        grid.ColumnsHierarchy.Items.Add("Column " & i)
    Next i
    ' add rows.
    For i As Integer = 1 To 25
        grid.RowsHierarchy.Items.Add("Row " & i)
    Next i

    grid.RowsHierarchy.AutoResize()
    grid.ColumnsHierarchy.AutoResize()
    grid.Refresh()
End Sub

To add rows and columns we use the Add method of the DataGridView's Rows and Columns hierarchies.

The SetCellValues method will simply insert values in the Grid cells. This will be achieved by using the SetCellValue method which accepts three parameters - row, column and cell value.

C#

private void SetCellsValues()
{
    HierarchyItem rowItem1 = grid.RowsHierarchy.Items[0];
    HierarchyItem columnItem1 = grid.ColumnsHierarchy.Items[0];

    grid.CellsArea.SetCellValue(rowItem1, columnItem1, "Monthly Housing and Transportation Expenses");
    grid.CellsArea.SetCellValue(grid.RowsHierarchy.Items[1], columnItem1, "Primary residence");
    grid.CellsArea.SetCellValue(grid.RowsHierarchy.Items[1], this.grid.ColumnsHierarchy.Items[3], "Transportation Expenses");
    // fill data

    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[2], columnItem1, "Mortgage Payment");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[3], columnItem1, "Property Tax");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[4], columnItem1, "Insurance");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[5], columnItem1, "Electricity");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[6], columnItem1, "Water");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[7], columnItem1, "Cabel TV Service");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[8], columnItem1, "High Speed Internet");

    // fill second column data.
    HierarchyItem columnItem2 = this.grid.ColumnsHierarchy.Items[1];
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[2], columnItem2, "$1,459,76");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[3], columnItem2, "$212.54");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[4], columnItem2, "$49.21");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[5], columnItem2, "$73.44");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[6], columnItem2, "$41.48");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[7], columnItem2, "$22.14");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[8], columnItem2, "$24.99");

    // fill third column data.

    HierarchyItem columnItem3 = this.grid.ColumnsHierarchy.Items[3];
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[2], columnItem3, "Vehicle 1 Payment");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[3], columnItem3, "Vehicle 1 Insurance");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[4], columnItem3, "Vehicle 1 Gas");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[5], columnItem3, "Vehicle 1 Maintenance");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[6], columnItem3, "Vehicle 2 Lease");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[7], columnItem3, "Vehicle 2 Insurance");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[8], columnItem3, "Vehicle 2 Gas");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[9], columnItem3, "Vehicle 2 Maintenance");

    // fiil fourth column data.
    HierarchyItem columnItem4 = this.grid.ColumnsHierarchy.Items[4];
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[2], columnItem4, "$351,34");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[3], columnItem4, "$55.12");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[4], columnItem4, "$129.21");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[5], columnItem4, "$55.17");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[6], columnItem4, "$311.12");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[7], columnItem4, "$109.35");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[8], columnItem4, "$114.99");
    grid.CellsArea.SetCellValue(this.grid.RowsHierarchy.Items[9], columnItem4, "$35.19");
}

VB .NET

Private Sub SetCellsValues()
    Dim rowItem1 As HierarchyItem = grid.RowsHierarchy.Items(0)
    Dim columnItem1 As HierarchyItem = grid.ColumnsHierarchy.Items(0)

    grid.CellsArea.SetCellValue(rowItem1, columnItem1, "Monthly Housing and Transportation Expenses")
    grid.CellsArea.SetCellValue(grid.RowsHierarchy.Items(1), columnItem1, "Primary residence")
    grid.CellsArea.SetCellValue(grid.RowsHierarchy.Items(1), Me.grid.ColumnsHierarchy.Items(3), "Transportation Expenses")
    ' fill data

    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(2), columnItem1, "Mortgage Payment")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(3), columnItem1, "Property Tax")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(4), columnItem1, "Insurance")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(5), columnItem1, "Electricity")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(6), columnItem1, "Water")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(7), columnItem1, "Cabel TV Service")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(8), columnItem1, "High Speed Internet")

    ' fill second column data.
    Dim columnItem2 As HierarchyItem = Me.grid.ColumnsHierarchy.Items(1)
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(2), columnItem2, "$1,459,76")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(3), columnItem2, "$212.54")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(4), columnItem2, "$49.21")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(5), columnItem2, "$73.44")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(6), columnItem2, "$41.48")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(7), columnItem2, "$22.14")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(8), columnItem2, "$24.99")

    ' fill third column data.

    Dim columnItem3 As HierarchyItem = Me.grid.ColumnsHierarchy.Items(3)
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(2), columnItem3, "Vehicle 1 Payment")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(3), columnItem3, "Vehicle 1 Insurance")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(4), columnItem3, "Vehicle 1 Gas")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(5), columnItem3, "Vehicle 1 Maintenance")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(6), columnItem3, "Vehicle 2 Lease")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(7), columnItem3, "Vehicle 2 Insurance")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(8), columnItem3, "Vehicle 2 Gas")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(9), columnItem3, "Vehicle 2 Maintenance")

    ' fiil fourth column data.
    Dim columnItem4 As HierarchyItem = Me.grid.ColumnsHierarchy.Items(4)
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(2), columnItem4, "$351,34")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(3), columnItem4, "$55.12")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(4), columnItem4, "$129.21")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(5), columnItem4, "$55.17")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(6), columnItem4, "$311.12")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(7), columnItem4, "$109.35")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(8), columnItem4, "$114.99")
    grid.CellsArea.SetCellValue(Me.grid.RowsHierarchy.Items(9), columnItem4, "$35.19")
    columnItem3.Width = 150
    columnItem1.Width = 150
    grid.Refresh()
End Sub

The MergeCells method will lllustrate how to merge grid cells by using the SetCellSpan method. When you use the SetCellSpan method, you need to pass four parameters to it - row, column, rows span and columns span. In the method we will merge cells in the first and second rows and we will also set custom visual style to the merged cells.

C#

private void MergeCells()
{
    HierarchyItem rowItem1 = grid.RowsHierarchy.Items[0];
    HierarchyItem columnItem1 = grid.ColumnsHierarchy.Items[0];

    // create a custom cell style.
    GridCellStyle style = new GridCellStyle();
    style.FillStyle = new FillStyleSolid(Color.FromArgb(255, 0, 175, 240));
    style.Font = new Font(this.Font.FontFamily, 11.0f, FontStyle.Bold);
    style.TextColor = Color.White;

    GridCellStyle orangestyle = new GridCellStyle();
    orangestyle.FillStyle = new FillStyleSolid(Color.FromArgb(255, 254, 122, 1));
    orangestyle.Font = new Font(this.Font.FontFamily, 10.0f, FontStyle.Bold);
    orangestyle.TextColor = Color.White;

    grid.CellsArea.SetCellTextAlignment(rowItem1, columnItem1, ContentAlignment.MiddleCenter);
    // set the cell span to 1 row and 5 columns.
    grid.CellsArea.SetCellSpan(rowItem1, columnItem1, 1, 5);
    // set the merged cell value and style.
    grid.CellsArea.SetCellDrawStyle(rowItem1, columnItem1, style);
    // set the cell span to 1 row and 2 columns.
    grid.CellsArea.SetCellSpan(grid.RowsHierarchy.Items[1], columnItem1, 1, 3);
    grid.CellsArea.SetCellDrawStyle(grid.RowsHierarchy.Items[1], columnItem1, orangestyle);
    // set the merged cell value.
    // set the cell span to 1 row and 2 columns.
    grid.CellsArea.SetCellSpan(grid.RowsHierarchy.Items[1], this.grid.ColumnsHierarchy.Items[3], 1, 2);
    // set the merged cell value.
    grid.CellsArea.SetCellDrawStyle(grid.RowsHierarchy.Items[1], this.grid.ColumnsHierarchy.Items[3], orangestyle);
}

VB .NET

Private Sub MergeCells()
    Dim rowItem1 As HierarchyItem = grid.RowsHierarchy.Items(0)
    Dim columnItem1 As HierarchyItem = grid.ColumnsHierarchy.Items(0)

    ' create a custom cell style.
    Dim style As New GridCellStyle()
    style.FillStyle = New FillStyleSolid(Color.FromArgb(255, 0, 175, 240))
    style.Font = New Font(Me.Font.FontFamily, 11.0f, FontStyle.Bold)
    style.TextColor = Color.White

    Dim orangestyle As New GridCellStyle()
    orangestyle.FillStyle = New FillStyleSolid(Color.FromArgb(255, 254, 122, 1))
    orangestyle.Font = New Font(Me.Font.FontFamily, 10.0f, FontStyle.Bold)
    orangestyle.TextColor = Color.White

    grid.CellsArea.SetCellTextAlignment(rowItem1, columnItem1, ContentAlignment.MiddleCenter)
    ' set the cell span to 1 row and 5 columns.
    grid.CellsArea.SetCellSpan(rowItem1, columnItem1, 1, 5)
    ' set the merged cell value and style.
    grid.CellsArea.SetCellDrawStyle(rowItem1, columnItem1, style)
    ' set the cell span to 1 row and 2 columns.
    grid.CellsArea.SetCellSpan(grid.RowsHierarchy.Items(1), columnItem1, 1, 3)
    grid.CellsArea.SetCellDrawStyle(grid.RowsHierarchy.Items(1), columnItem1, orangestyle)
    ' set the merged cell value.
    ' set the cell span to 1 row and 2 columns.
    grid.CellsArea.SetCellSpan(grid.RowsHierarchy.Items(1), Me.grid.ColumnsHierarchy.Items(3), 1, 2)
    ' set the merged cell value.
    grid.CellsArea.SetCellDrawStyle(grid.RowsHierarchy.Items(1), Me.grid.ColumnsHierarchy.Items(3), orangestyle)
End Sub

Sort WinForms DataGridView Rows and Columns by Label

by Admin 15. October 2011 21:16

VIBlend WinForms DataGridView has a built-in feature which allows you to set a custom Sort comparer. In this post, we will show you how to sort the DataGridView rows or columns by label.
At first, we will create a Comparer which compares HierarchyItem objects by the HierarchyItem.Caption property.

C#

private class ItemLabelComparer : IComparer<HierarchyItem>
{
    public int Compare(HierarchyItem item1, HierarchyItem item2)
    {
        return string.Compare(item1.Caption, item2.Caption);
    }
}

VB .NET

Private Class ItemLabelComparer
    Implements IComparer(Of HierarchyItem)
    Public Function Compare(ByVal item1 As HierarchyItem, ByVal item2 As HierarchyItem) As Integer
        Return String.Compare(item1.Caption, item2.Caption)
    End Function
End Class

Create an instance of the ItemLabelComparer object and pass it to the RowsHierarchy.SortBy method. If you want to sort the ColumnsHierarchy, call the ColumnsHierarchy.SortBy method.

C#

ItemLabelComparer Comparer = new ItemLabelComparer();
 this.vDataGridView1.RowsHierarchy.SortBy(this.vDataGridView1.ColumnsHierarchy.Items[0], sorting, Comparer);
this.Refresh();

VB .NET

Dim Comparer As New ItemLabelComparer()
 Me.vDataGridView1.RowsHierarchy.SortBy(Me.vDataGridView1.ColumnsHierarchy.Items(0), sorting, Comparer)
Me.Refresh()

Working with the VIBlend DataGrid and Nested Data Structures

by viblend 8. June 2011 22:29

This example demonstrates how to bind the VIBlend DataGrid for Silverlight to a list of invoices that has nested properties. The schema of data source contains the following DataFields / Properties: SalesPerson(string), ShipperCompany(string), ProductName(string), UnitPrice(double),
Quantity(double), Discount(double), ExtendedPrice(double), Freight(double) and Address(Address). The address object has the following properties: City(string), Region(string) and Country(string).

We'll create a Pivot Table with 3 pivot rows - City, Country and Region and 1 pivot column - Shipper Company.

<viblend:DataGrid ItemsSource="{Binding}" x:Name="dataGrid1" Margin="4" Background="White" AutoGenerateColumns="True">
    <viblend:DataGrid.BoundFields>
        <viblend:BoundField Text="Country" DataField="Address.Country" Width="160" SortMode="NotSortable"/>
        <viblend:BoundField Text="City" DataField="Address.City" Width="140" SortMode="NotSortable"/>
        <viblend:BoundField Text="Region" DataField="Address.Region" Width="140" SortMode="NotSortable"/>
        <viblend:BoundField Text="Carrier" DataField="ShipperCompany" Width="140" SortMode="NotSortable" />
        <viblend:BoundField Text="ExtendedPrice" DataField="ExtendedPrice" Width="140" SortMode="NotSortable" />
    </viblend:DataGrid.BoundFields>
    <viblend:DataGrid.BoundPivotRows>
        <viblend:BoundField Text="Country" DataField="Address.Country" Width="160" SortMode="NotSortable"/>
        <viblend:BoundField Text="Region" DataField="Address.Region" Width="160" SortMode="NotSortable"/>
        <viblend:BoundField Text="City" DataField="Address.City" Width="140" SortMode="NotSortable"/>
    </viblend:DataGrid.BoundPivotRows>
    <viblend:DataGrid.BoundPivotColumns>
        <viblend:BoundField Text="Carrier" DataField="ShipperCompany" Width="140" SortMode="NotSortable"/>
    </viblend:DataGrid.BoundPivotColumns>
    <viblend:DataGrid.BoundPivotValues>
        <viblend:BoundValueField Width="120" CellHorizontalContentAlignment="Center" Text="Count of Sales" Function="Count" DataField="ExtendedPrice"></viblend:BoundValueField>
         <viblend:BoundValueField Width="120" CellHorizontalContentAlignment="Right" CellVerticalContentAlignment="Center" CellTextFormatString="{}{0:C}" Text="Amount of Sales" Function="Sum" DataField="ExtendedPrice"></viblend:BoundValueField>
        <viblend:BoundValueField Width="120" CellHorizontalContentAlignment="Right" CellVerticalContentAlignment="Center" CellTextFormatString="{}{0:C}" Text="Avg Sale Amount" Function="Average" DataField="ExtendedPrice"></viblend:BoundValueField>
    </viblend:DataGrid.BoundPivotValues>
</viblend:DataGrid>

The following code generates the sample data in the example:

C#

public PivotTableDemo()
{
    InitializeComponent();
    PrepareGridData();
    this.DataContext = this.lst;
}

public class Invoice
{
    public Invoice(
        string City,
        string Region,
        string Country,
        string SalesPerson,
        string ShipperCompany,
        string ProductName,
        double UnitPrice,
        double Quantity,
        double Discount,
        double ExtendedPrice,
        double Freight)
    {
        this.SalesPerson = SalesPerson;
        this.Address = new Address();
        this.Address.City = City;
        this.Address.Region = string.IsNullOrEmpty(Region) ? "All Regions" : Region;
        this.Address.Country = Country;
        this.ShipperCompany = ShipperCompany;
        this.ProductName = ProductName;
        this.UnitPrice = UnitPrice;
        this.Quantity = Quantity;
        this.Discount = Discount;
        this.ExtendedPrice = ExtendedPrice;
        this.Freight = Freight;
    }

    public string SalesPerson { get; set; }
    public string ShipperCompany { get; set; }
    public Address Address { get; set; }
    public string ProductName { get; set; }
    public double UnitPrice { get; set; }
    public double Quantity { get; set; }
    public double Discount { get; set; }
    public double ExtendedPrice { get; set; }
    public double Freight { get; set; }
}

public class Address
{
    public string City { get; set; }
    public string Region { get; set; }
    public string Country { get; set; }
}

public List<Invoice> lst = new List<Invoice>();
private void PrepareGridData()
{
    try
    {
        StreamResourceInfo sri = Application.GetResourceStream(new Uri("IntegratedDemo;component/Examples/Grid/invoices.tab", UriKind.Relative));
        System.IO.StreamReader sr = new System.IO.StreamReader(sri.Stream);
        string line = null;
        sr.ReadLine();
        while ((line = sr.ReadLine()) != null)
        {
            string[] tabs = line.Split('\t');
            lst.Add(new Invoice(tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], double.Parse(tabs[6]), double.Parse(tabs[7]), double.Parse(tabs[8]), double.Parse(tabs[9]), double.Parse(tabs[10])));
        }
        sr.Close();
    }
    catch (System.Exception)
    {
    }
}

VB .NET

Public Sub New()
    InitializeComponent()
    PrepareGridData()
    Me.DataContext = Me.lst
End Sub

Public Class Invoice
    Public Sub New(ByVal City As String, ByVal Region As String, ByVal Country As String, ByVal SalesPerson As String, ByVal ShipperCompany As String, ByVal ProductName As String, ByVal UnitPrice As Double, ByVal Quantity As Double, ByVal Discount As Double, ByVal ExtendedPrice As Double, ByVal Freight As Double)
        Me.SalesPerson = SalesPerson
        Me.Address = New Address()
        Me.Address.City = City
        Me.Address.Region = If(String.IsNullOrEmpty(Region), "All Regions", Region)
        Me.Address.Country = Country
        Me.ShipperCompany = ShipperCompany
        Me.ProductName = ProductName
        Me.UnitPrice = UnitPrice
        Me.Quantity = Quantity
        Me.Discount = Discount
        Me.ExtendedPrice = ExtendedPrice
        Me.Freight = Freight
    End Sub

    Private privateSalesPerson As String
    Public Property SalesPerson() As String
        Get
            Return privateSalesPerson
        End Get
        Set(ByVal value As String)
            privateSalesPerson = value
        End Set
    End Property
    Private privateShipperCompany As String
    Public Property ShipperCompany() As String
        Get
            Return privateShipperCompany
        End Get
        Set(ByVal value As String)
            privateShipperCompany = value
        End Set
    End Property
    Private privateAddress As Address
    Public Property Address() As Address
        Get
            Return privateAddress
        End Get
        Set(ByVal value As Address)
            privateAddress = value
        End Set
    End Property
    Private privateProductName As String
    Public Property ProductName() As String
        Get
            Return privateProductName
        End Get
        Set(ByVal value As String)
            privateProductName = value
        End Set
    End Property
    Private privateUnitPrice As Double
    Public Property UnitPrice() As Double
        Get
            Return privateUnitPrice
        End Get
        Set(ByVal value As Double)
            privateUnitPrice = value
        End Set
    End Property
    Private privateQuantity As Double
    Public Property Quantity() As Double
        Get
            Return privateQuantity
        End Get
        Set(ByVal value As Double)
            privateQuantity = value
        End Set
    End Property
    Private privateDiscount As Double
    Public Property Discount() As Double
        Get
            Return privateDiscount
        End Get
        Set(ByVal value As Double)
            privateDiscount = value
        End Set
    End Property
    Private privateExtendedPrice As Double
    Public Property ExtendedPrice() As Double
        Get
            Return privateExtendedPrice
        End Get
        Set(ByVal value As Double)
            privateExtendedPrice = value
        End Set
    End Property
    Private privateFreight As Double
    Public Property Freight() As Double
        Get
            Return privateFreight
        End Get
        Set(ByVal value As Double)
            privateFreight = value
        End Set
    End Property
End Class

Public Class Address
    Private privateCity As String
    Public Property City() As String
        Get
            Return privateCity
        End Get
        Set(ByVal value As String)
            privateCity = value
        End Set
    End Property
    Private privateRegion As String
    Public Property Region() As String
        Get
            Return privateRegion
        End Get
        Set(ByVal value As String)
            privateRegion = value
        End Set
    End Property
    Private privateCountry As String
    Public Property Country() As String
        Get
            Return privateCountry
        End Get
        Set(ByVal value As String)
            privateCountry = value
        End Set
    End Property
End Class

Public lst As List(Of Invoice) = New List(Of Invoice)()
Private Sub PrepareGridData()
    Try
        Dim sri As StreamResourceInfo = Application.GetResourceStream(New Uri("IntegratedDemo;component/Examples/Grid/invoices.tab", UriKind.Relative))
        Dim sr As New System.IO.StreamReader(sri.Stream)
        Dim line As String = Nothing
        sr.ReadLine()
        line = sr.ReadLine()
        Do While line IsNot Nothing
            Dim tabs() As String = line.Split(ControlChars.Tab)
            lst.Add(New Invoice(tabs(0), tabs(1), tabs(2), tabs(3), tabs(4), tabs(5), Double.Parse(tabs(6)), Double.Parse(tabs(7)), Double.Parse(tabs(8)), Double.Parse(tabs(9)), Double.Parse(tabs(10))))
            line = sr.ReadLine()
        Loop
        sr.Close()
    Catch e1 As System.Exception
    End Try
End Sub


Note: The 'invoices.tab' file is included in the installation package of VIBlend Controls for Silverlight. The default path to the file is: C:\Program Files\VIBlend\SilverlightControls v.5.0\Source Code\CSharp\Examples\Grid\invoices.tab.

VIBlend extends the WinForms DataGridView with new features

by viblend 13. February 2011 08:44

The new version of VIBlend Controls for WinForms is coming in a few weeks and will deliver an upgrade to the DataGridView control with new features, better UI and styling capabilities.

-  Extended filtering options

The DataGridView filtering window will be extended by providing an easier and user-friendly experience. The new filtering window will show a checked list, allowing for single and/or multiple selections. The previous filtering mechanism will still be available and you will be able to switch to it by clicking a single button. The filtering improvements will also include new case-sensitive filtering options.



-    Improved support for Totals

The Pivot-Table totals support will be extended by adding a capability that will allow you to display Sub-Totals, Grand-Totals or a combination of Sub-Totals and Grand-Totals

-    New Cell Editors

The new version will include several new cell editors – Currency, Percentage, Number, Mask and Fixed-Point Editors

-    New Pivot-Table Filtering and enhanced Pivot-Design panel

The DataGridView will expose a new BoundFieldsFilters collection that will allow you to filter data in the Pivot-Table and display only a subset of data that meet the filtering criteria (criteria: Conditions you specify to limit which records are included in the result set of a filter.) that you specify and hides data that you do not want displayed. The Pivot-Design panel will feature a new Filter Area box that will enable you to manipulate the BoundFieldsFilters collection by drag and drop. Another addition to the Pivot-Design panel will be the Localization support.

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET