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:


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

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