VIBlend
Home Products Downloads Purchase Support Forum About Blog

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

 

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

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