VIBlend
Home Products Downloads Purchase Support Forum About Blog

Hide a Grouped Column from the Columns List in WinForms DataGridView

by viblend 10. October 2012 00:29

In this post, we will show you how to hide a column from the DataGridView when the user groups the data by the column. When the user ungroups by the column, we will show the column again.

 The first step is to subscribe to the CollectionChanged event of the vDataGridView's GroupingColumns collection.

C#
 
this.Grid1.GroupingColumns.CollectionChanged += new EventHandler<CollectionChangedEventArgs>(GroupingColumns_CollectionChanged); 
 
VB .NET
 
AddHandler Grid1.GroupingColumns.CollectionChanged, AddressOf GroupingColumns_CollectionChanged
 
2. The next step is to update the DataGridView Columns Visibility in the event handler.
 
C#
 
void GroupingColumns_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
    HierarchyItemsCollection items = Grid1.ColumnsHierarchy.Items;
    foreach (BoundField field in Grid1.GroupingColumns)
    {
        foreach (HierarchyItem item in items)
        {
            if (item.Caption == field.Text)
            {
                item.Hidden = true;
                items.Remove(item);
                break;   
            }
        }
    }
 
VB .NET
 
Private Sub GroupingColumns_CollectionChanged(ByVal sender As Object, ByVal e As CollectionChangedEventArgs)
Dim items As HierarchyItemsCollection = Grid1.ColumnsHierarchy.Items
For Each field As BoundField In Grid1.GroupingColumns
For Each item As HierarchyItem In items
If item.Caption = field.Text Then
item.Hidden = True
items.Remove(item)
Exit For
End If
Next item
Next field
End Sub 

VIBlend Silverlight Controls 6.5

by viblend 19. August 2012 16:54

We are happy to announce the availability of VIBlend Controls for Silverlight ver. 6.5. The new version includes multiple bug fixes and small improvements to the existing features and themes of the package. 

  

Filter Silverlight DataGrid data programmatically

by viblend 10. July 2012 16:22
In order to achieve this, you need to follow the steps below:
 
1. Create DataGrid’s Rows, Columns and fill it with sample data. 
 
C#

for (int i = 0; i < 20; i++)
{
    this.dataGrid1.ColumnsHierarchy.Items.Add("Column " + i);
    this.dataGrid1.RowsHierarchy.Items.Add("Row " + i);
}

this.dataGrid1.AllowContextMenuFiltering = true;
this.dataGrid1.AllowContextMenuSorting = true;
Random random = new Random();

for (int i = 0; i < 20; i++)
{
    HierarchyItem row = this.dataGrid1.RowsHierarchy.Items[i];
    for (int j = 0; j < 20; j++)
    {
        HierarchyItem column = this.dataGrid1.ColumnsHierarchy.Items[j];
        column.AllowFiltering = true;
        this.dataGrid1.CellsArea.SetCellValue(row, column, (double)i);
    }
}

this.dataGrid1.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ALL);
this.dataGrid1.RowsHierarchy.AutoResize(AutoResizeMode.FIT_ALL);

VB .NET

For i As Integer = 0 To 19
    Me.dataGrid1.ColumnsHierarchy.Items.Add("Column " & i)
    Me.dataGrid1.RowsHierarchy.Items.Add("Row " & i)
Next i

Me.dataGrid1.AllowContextMenuFiltering = True
Me.dataGrid1.AllowContextMenuSorting = True
Dim random As New Random()

For i As Integer = 0 To 19
    Dim row As HierarchyItem = Me.dataGrid1.RowsHierarchy.Items(i)
    For j As Integer = 0 To 19
        Dim column As HierarchyItem = Me.dataGrid1.ColumnsHierarchy.Items(j)
        column.AllowFiltering = True
        Me.dataGrid1.CellsArea.SetCellValue(row, column, CDbl(i))
    Next j
Next i

Me.dataGrid1.ColumnsHierarchy.AutoResize(AutoResizeMode.FIT_ALL)
Me.dataGrid1.RowsHierarchy.AutoResize(AutoResizeMode.FIT_ALL)
 
2. Create a new NumericFilter and set its ComparisonOperator and Value properties
 
C#

NumericFilter filter = new NumericFilter();
filter.ComparisonOperator = NumericComparisonOperator.GREATER_THAN;
filter.Value = 5;
FilterGroup<double?> filterGroup = new FilterGroup<double?>();
filterGroup.AddFilter(FilterOperator.AND, filter);

VB .NET

Dim filter As New NumericFilter()
filter.ComparisonOperator = NumericComparisonOperator.GREATER_THAN
filter.Value = 5
Dim filterGroup As FilterGroup(Of Nullable(Of Double)) = New FilterGroup(Of Nullable(Of Double))()
filterGroup.AddFilter(FilterOperator.AND, filter)

3. Create a new HierarchyItemFilter object and add the FilterGroup to it. 
 
C#

HierarchyItemFilter itemFilter = new HierarchyItemFilter();
itemFilter.Item = this.dataGrid1.ColumnsHierarchy.Items[0];
itemFilter.Filter = filterGroup;

VB .NET

Dim itemFilter As New HierarchyItemFilter()
itemFilter.Item = Me.dataGrid1.ColumnsHierarchy.
itemFilter.Filter = filterGroup
 
4. Finally, add the HierarchyItemFilter instance to the Filters collection of the DataGrid’s RowsHierarchy. 
 
C#

this.dataGrid1.RowsHierarchy.Filters.Add(itemFilter);

VB .NET

Me.dataGrid1.RowsHierarchy.Filters.Add(itemFilter)
 

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

 

What's coming with VIBlend WinForms Controls 9.0

by viblend 3. March 2012 21:20
In the next version of VIBlend, we will enhance the vDataGridView control by adding a new Search capabilities to it. Users will be able to choose a search criteria and easily find a Row in the Grid. This new feature can be turned on by drag and drop of a vDataGridViewSearchHeader control and setting its DataGridView property to point to your vDataGridView instance.
 
  

Silverlight TreeView with Radio Button

by viblend 16. February 2012 21:15
This post will show you how to add radio buttons to the TreeView items.

In order to display radio buttons in the VIBlend TreeView for Silverlight, you need to set the EnableCheckboxes property to true and also the ListItemType of each TreeViewItem to "RadioButton".

For example:

<viblend:TreeView EnableCheckBoxes="True" Width="280" Height="300" x:Name="TreeView">
    <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="VIBlend" Value="VIBlend" ImageSource="/Images/user.png">
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Contacts" Value="Contacts" ImageSource="/Images/contacts.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Desktop" Value="Desktop" ImageSource="/Images/desktop.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Documents" Value="Documents" ImageSource="/Images/documents.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Downloads" Value="Downloads" ImageSource="/Images/folder.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Favorites" Value="Favorites" ImageSource="/Images/favorites.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Music" Value="Music" ImageSource="/Images/music.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Pictures" Value="Pictures" ImageSource="/Images/Pictures.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Searches" Value="Searches" ImageSource="/Images/searches.png"/>
        <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Videos" Value="Videos" ImageSource="/Images/videos.png"/>
    </viblend:TreeViewItem>
    <viblend:TreeViewItem ListItemType="RadioButton" DisplayValue="Inbox" Value="Inbox" ImageSource="/Images/folder.png"/>
</viblend:TreeView>

Here's the resulting TreeView:
  

Implementing multi-threading in WinForms using the BackgroundWorker class and the VIBlend WinForms ProgressBar.

by viblend 31. January 2012 04:24
In this post we’ll create a simple multi-threaded WinForms application and will use the VIBlend WinForms ProgressBar control to report the progress to the user. The application will loop through numbers from 0 to 999. The progress bar will be filled during the process.

Let’s start by creating a Windows Application. In designer of the form, add the BackgroundWorker component from the “Components” category. The component is added to the bottom section of the designer.  Click it and set the WorkerReportsProgress property to true - this allows us to send progress notifications. Subscribe to the BackgroundWorker's DoWork and ProgressChanged events. The background task is handled in the DoWork event. The handler for this event will be called from a new worker thread. The ProgressBar will be updated in the ProgressChanged event handler. In order to notify about the progress you should call the ReportProgress method from the DoWork event handler. Please note that the ReportProgress method will work only when the WorkerReportsProgress property of the BackgroundWorker class is set to true.  

The next step is to create the User Interface. Drag and drop vButton and vProgressBar controls from the Visual Studio toolbox. Subscribe to the Button's Click event. To start doing the work we will call the RunWorkerAsync method of the BackgroundWorker in the Button's Click event handler.

In the ProgressChanged event handler, we will update the ProgressBar's value.

C#

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   // update the progress bar
    this.vProgressBar1.Value = e.ProgressPercentage;
    this.vProgressBar1.Refresh();
}

VB .NET

Private Sub worker_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
   ' update the progress bar
Me.vProgressBar1.Value = e.ProgressPercentage
Me.vProgressBar1.Refresh()
End Sub

Report the Progress in the DoWork event handler.

C#

private void worker_DoWork(object sender, DoWorkEventArgs e)
{
              int limit = 999;
for (int currNumber = 0; currNumber < 999; currNumber++)
{
       int progressPercentage = (100 * currNumber) / (limit);
                   worker.ReportProgress(progressPercentage);
}
}

VB .NET

Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
 Dim limit As Integer = 999
For currNumber As Integer = 0 To 998
  Dim progressPercentage As Integer = (100 * currNumber) / (limit)
  worker.ReportProgress(progressPercentage)
Next currNumber
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