VIBlend
Home Products Downloads Purchase Support Forum About Blog

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()

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.

How to customize cell styles in VIBlend DataGridView for WinForms

by viblend 27. July 2010 04:17

Each cell within the VIBlend WinForms DataGridView control can have its own style. A cell style can modify properties like text format, background color, foreground color, border color, and font. In most cases multiple cells will share particular style characteristics. To customize a cell style, start by creating a new instance of the GridCellStyle class.

GridCellStyle cellStyle = new GridCellStyle();

The next step is to set the appropriate values for each Style propoperty:

FillStyle cellFillStyle = new FillStyleGradientEx(Color.Aqua, Color.Aqua, Color.Aqua, Color.Aqua,
90, 0.7f, 0.7f);

cellStyle.TextColor = Color.Black;
cellStyle.TextColorSelected = Color.Black;
cellStyle.Font = this.Font;
cellStyle.FillStyle = cellFillStyle;
cellStyle.FillStyleSelected = cellFillStyle;
cellStyle.BorderColorSelected = Color.Transparent;
cellStyle.BorderColor = Color.Transparent;

 Finally, apply the style to one or more grid cells:

HierarchyItem rowItem = this.Grid1.RowsHierarchy.Items[1];
HierarchyItem colItem = this.Grid1.ColumnsHierarchy.Items[1];
this.Grid1.CellsArea.SetCellDrawStyle(rowItem, colItem, cellStyle); 

You can also apply a cell style to all grid cells in any row:

          HierarchyItem rowItem = this.Grid1.RowsHierarchy.Items[1];

rowItem.CellsStyle = cellStyle;


Similarily, you can apply a cell style to all grid cells in any column:
HierarchyItem colItem = this.Grid1.ColumnsHierarchy.Items[1];
colItem.CellsStyle = cellStyle;

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

winforms grid

What's new in VIBlend DataGridView for WinForms?

by viblend 13. July 2010 09:20

In the latest release of VIBlend Controls for WinForms, we introduced several new features in our DataGridView control. These are the Rows Grouping panel and the Column Chooser. The main purpose of the Rows Grouping panel is to allow you to easily group/ungroup columns via a built-in drag and drop and to visually represent the grouped columns.


The Column Chooser is an additional small feature which allows you to manage the visibility of your columns.  In order to activate the column chooser, set the EnableColumnChooser property to true. To show the column chooser, use the ShowColumnChooser method. If you want to hide it, use the HideColumnChooser method.



Another improvement in the DataGridView is the extended functionality of the ComboBox and DateTimePicker editors. Their pop up controls are now automatically opened when the editor is activated.  If you want to disable this feature, you can set the AutoOpenEditorOnActivation property of the editor to false.

Currently rated 3.2 by 5 people

  • Currently 3.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

DataGrid | WinForms | winforms grid

VIBlend WinForms Controls ver. 4.6 – Released

by viblend 22. April 2010 09:17

We are happy to announce the release of the latest version of VIBlend Controls for WinForms.

VIBlend Windows Forms Controls 4.6.0 - Release Notes

1. Added support for Visual Studio 2010
2. Moved all design time classes of our controls to separate assembly
3. DataGridView changes

In this version we extended the HierarhcyItem mouse click events. Previously, the grid was raising only the ItemClick and ItemDoubleClick events.

These two events are replaced by the following set of events:

 - HierarchyItemMouseUp
 - HierarchyItemMouseDown
 - HierarchyItemMouseClick
 - HierarchyItemMouseDoubleClick

We also added several improvements in the in-place cell editors' infrastructure. Specifically we added the following activation and deactivation events:

 - Click on selected cell (MOUSE_CLICK_SELECTED_CELL)
 - Enter key activation (KEY_PRESS_ENTER
 - Esc key deactivation (KEY_PRESS_ESC)

The default activation and deactivation flags for the built-in cell editors where modified to use the new activation events.

Please, note that these are breaking changes. If you are using an earlier version of VIBlend Controls for Windows Forms you will have to make minor changes to your code.

New in version 4.6 are the HierarchyItems and Cells selection changed events:
 - DataGridView.HierarchyItemSelectionChanged
 - DataGridView.CellSelectionChanged

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

DataGrid | olap grid | winforms grid | WinForms

Visual Studio 2010 and .Net Framework 4.0

by viblend 12. April 2010 03:22

VIBlend will fully support the .NET4 Client Profile in the next version of VIBlend Controls for WinForms which is expected in the middle of May. We will create a new VIBlend.WinForms.Controls.Design assembly and will move all the design time classes of our controls into it. The change is needed because the client profile does not include the standard design time classes and we need to remove the reference to the System.Design namespace in our present assemblies.

The upcoming release of the VIBlend Controls for Silverlight, which is due in May, will incorporate a full support for Visual Studio 2010. We will also have a new build for Silverlight 4 and our product will benefit from the new features in the latest platform’s edition. In addition, the new release will be shipped with three new themes for all of our controls and performance optimizations in the VIBlend OLAP Grid for Silverlight.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

DataGrid | Industry news | olap grid | pivot grid | Silverlight Controls | winforms grid

Using VIBlend DataGridView for WinForms in a WPF Application

by viblend 3. April 2010 21:56

In this blog post we will show you how to use the VIBlend DataGridView for WinForms in a WPF application.

The following code example creates a WinForms DataGrid control with Office2010Black theme in a WPF application. This example uses a WindowsFormsHost element to place the DataGrid control within the main window’s root  element. The WindowsFormsHost element could be found in the WindowsFormsIntegration.dll.

[xaml]

  <Window x:Class="HostWinFormsControlInWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viblend="clr-namespace:VIBlend.WinForms.DataGridView;assembly=VIBlendGrid"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <WindowsFormsHost>
            <viblend:vDataGridView x:Name="VIBlendDataGrid" VIBlendTheme="OFFICE2010BLACK"/>               
        </WindowsFormsHost>
    </Grid>
</Window>

In order to bind the data grid, you need to do the following:  Click on the Data menu item in your Visual Studio, then select the “Add new DataSource” item.  Browse to the Nwind.mdb which ships with the WinForms controls installation, choose the Employees table and click finish. In the code behind, after the InitializeComponent call or in the Load event handler , bind the data grid to the Employees table.

            NwindDataSet dataSet = new NwindDataSet();
            EmployeesTableAdapter adapter = new EmployeesTableAdapter();
            adapter.Fill(dataSet.Employees);
            this.VIBlendDataGrid.DataSource = dataSet.Employees.DefaultView;
            this.VIBlendDataGrid.DataBind();

Here is the result:

Using VIBlend DataGridView for WinForms in Virtual Mode

by viblend 7. July 2009 18:35

VIBlend DataGridView for WinForms allows you to set the grid cells' source to Virtual mode. There are two steps to setup Virtual mode:

1. Set the cells' data source to Virtual. You can do that for a specific cell as well as for all cells under a row or a column:

  vDataGridView1.CellsArea.SetCellDataSource(column, GridCellDataSource.Virtual);

2. Implement a GridCellValueNeeded event handler. Whenever the data grid renders a cell it will raise the GridCellValueNeeded event:

    vDataGridView1.GridCellValueNeeded += new vDataGridView.GridCellValueNeededEventHandler(DataGridView1_GridCellValueNeeded);

    void DataGridView1_GridCellValueNeeded(object sender, GridCellValueNeededEventArgs args)

    {
      args.Value = "Some text";

     }

Here's a complete example which creates a list of 50,000 rows and sets up the grid in virtual mode:

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

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


        public class EmployeeSales
        {
            public EmployeeSales(string Name, DateTime Date, int ProductId, int Quantity)
            {
                this.Name = Name;
                this.Date = Date;
                this.ProductId = ProductId;
                this.Quantity = Quantity;
            }

            #region Private Members
            private string _name;
            private DateTime _date;
            private int _productid;
            private int _quantity;
            #endregion

            #region Properties
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public DateTime Date
            {
                get { return _date; }
                set { _date = value; }
            }

            public string ProductName
            {
                get { return productNames[_productid, 0]; }

            }

            public double SalesAmount
            {
                get { return (double)_quantity * UnitPrice; }
            }

            public double UnitPrice
            {
                get
                {
                    double unitPrice = double.Parse(productNames[_productid, 1]);
                    return unitPrice;
                }
            }

            public int Quantity
            {
                get { return _quantity; }
                set { _quantity = value; }
            }

            public int ProductId
            {
                get { return _productid; }
                set { _productid = value; }
            }

            #endregion
        }


        // Implement the cell value needed event handler
        void DataGridView1_GridCellValueNeeded(object sender, GridCellValueNeededEventArgs args)
        {
            int rowIndex = int.Parse(args.RowItem.Caption) - 1;

            if (args.ColumnItem.Caption == "Employee Name")
                args.CellValue = lst[rowIndex].Name;
            else if (args.ColumnItem.Caption == "Transaction Date")
                args.CellValue = lst[rowIndex].Date;
            else if (args.ColumnItem.Caption == "Product Name")
                args.CellValue = lst[rowIndex].ProductName;
            else if (args.ColumnItem.Caption == "Quantity")
                args.CellValue = lst[rowIndex].Quantity;
            else if (args.ColumnItem.Caption == "Unit Price")
                args.CellValue = lst[rowIndex].UnitPrice;
            else if (args.ColumnItem.Caption == "Transaction Amount")
                args.CellValue = lst[rowIndex].SalesAmount;
        }

        #region Data
        string[] firstNames = new string[]
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Michael",
                    "Guylène"
                };

        string[] lastNames = new string[]
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Saavedra",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Peterson",
                    "Winkler",
                    "Bein",
                    "Petersen",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Nodier"
                };

        static string[,] productNames = new string[,]
                {
                    {"Black Tea", "1.95"},
                    {"Green Tea", "1.95"},
                    {"Caffe Espresso", "1.45"},
                    {"Doubleshot Espresso", "1.75"},
                    {"Caffe Latte", "2.25"},
                    {"White Chocolate Mocha", "2.35"},
                    {"Cramel Latte", "2.35"},
                    {"Caffe Americano", "1.65"},
                    {"Cappuccino", "2.10"},
                    {"Espresso Truffle", "2.45"},
                    {"Espresso con Panna", "1.81"},
                    {"Peppermint Mocha Twist", "1.99"}
                };
        #endregion

        List<EmployeeSales> lst = new List<EmployeeSales>();

        private void Form1_Load(object sender, EventArgs e)
        {
            vDataGridView1.RowsHierarchy.AllowResize = true;
            vDataGridView1.RowsHierarchy.AllowDragDrop = false;
           
            vDataGridView1.ColumnsHierarchy.AllowResize = true;
            vDataGridView1.ColumnsHierarchy.AllowDragDrop = false;

            vDataGridView1.SelectionMode = vDataGridView.SELECTION_MODE.FULL_ROW_SELECT;
            vDataGridView1.MultipleSelectionEnabled = true;

            vDataGridView1.GridCellValueNeeded += new vDataGridView.GridCellValueNeededEventHandler(DataGridView1_GridCellValueNeeded);

            const int rowsToLoad = 50000;

            // Generate test data      
            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                lst.Add(new EmployeeSales(
                    string.Format("{0} {1}", firstNames[rand.Next(0, firstNames.Length - 1)], lastNames[rand.Next(0, lastNames.Length - 1)]),
                    DateTime.Now.AddDays(-rand.Next(10, 100)),
                    productId,
                    quantity)
                    );
            }


            BindGridVirtualMode();
        }

        private void BindGridVirtualMode()
        {

            Cursor.Current = Cursors.WaitCursor;

            vDataGridView1.RowsHierarchy.Items.Clear();
            vDataGridView1.RowsHierarchy.SummaryItems.Clear();
            vDataGridView1.ColumnsHierarchy.Items.Clear();
            vDataGridView1.ColumnsHierarchy.SummaryItems.Clear();

            vDataGridView1.CellsArea.CellFormatting.ClearFormatProviders();
            vDataGridView1.CellsArea.CellFormatting.ClearFormatSettings();
            vDataGridView1.CellsArea.CellFormatting.SetFormatter("defaultNumberFormatter", null, "{0:#,###.####}");

            #region Prepare the grid columns
            vDataGridView1.ColumnsHierarchy.Fixed = true;

            vDataGridView1.ColumnsHierarchy.Items.Add("Employee Name");
            vDataGridView1.ColumnsHierarchy.Items.Add("Transaction Date");
            vDataGridView1.ColumnsHierarchy.Items.Add("Product Name");
            vDataGridView1.ColumnsHierarchy.Items.Add("Quantity");
            vDataGridView1.ColumnsHierarchy.Items.Add("Unit Price");
            vDataGridView1.ColumnsHierarchy.Items.Add("Transaction Amount");

            // Set the cells data source to Virtual for all columns
            foreach (HierarchyItem column in vDataGridView1.ColumnsHierarchy.Items)
                vDataGridView1.CellsArea.SetCellDataSource(column, GridCellDataSource.Virtual);

            vDataGridView1.CellsArea.CellFormatting.ClearFormatProviders();
            vDataGridView1.CellsArea.CellFormatting.ClearFormatSettings();


            vDataGridView1.CellsArea.CellFormatting.SetFormatter("dateFormatter", new System.Globalization.DateTimeFormatInfo(), "{0:dd-MMM-yyyy}");
            vDataGridView1.CellsArea.CellFormatting.SetFormatter("dollarFormatter", null, "${0:#.##}");

            vDataGridView1.CellsArea.CellFormatting.SetCellFormatting(vDataGridView1.ColumnsHierarchy.Items[1], "dateFormatter");
            vDataGridView1.CellsArea.CellFormatting.SetCellFormatting(vDataGridView1.ColumnsHierarchy.Items[4], "dollarFormatter");
            vDataGridView1.CellsArea.CellFormatting.SetCellFormatting(vDataGridView1.ColumnsHierarchy.Items[5], "dollarFormatter");

            vDataGridView1.ColumnsHierarchy.AutoResize();
            #endregion

            #region Prepare the grid rows
            for (int i = 0; i < lst.Count; i++)
                vDataGridView1.RowsHierarchy.Items.Add((i + 1).ToString());

            vDataGridView1.RowsHierarchy.SetColumnWidth(0, 30);
            vDataGridView1.RowsHierarchy.Fixed = true;
            #endregion

            vDataGridView1.Refresh();

            Cursor.Current = Cursors.Default;

        }
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

GridView | winforms grid

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