VIBlend
Home Products Downloads Purchase Support Forum About Blog

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)
 

DataGrid Mask Column

by viblend 23. January 2011 08:09

The mask editor control ensures a formatted appearance to underlying string values and also validates the user input. This post demonstrates how to create a mask column in VIBlend DataGrid for Silverlight.

1. At first, create the DataGrid control and set up the binding expressions.  We setup the first column to display a string value and the second column with a phone number mask “(###) ###-####″. The MaskEditor’s EditorValue property is bound to the PhoneNumber property in the data source.

  <Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>
        <local:PhoneNumberConverter x:Key="PhoneNumberConverter"></local:PhoneNumberConverter>

        <DataTemplate x:Key="cellEditMaskEditorTemplate">
            <viblendEditors:MaskEditor FontSize="9" Mask="(###) ###-####" EditorValue="{Binding PhoneNumber, Mode=TwoWay, Converter={StaticResource PhoneNumberConverter}}"/>
        </DataTemplate>
    </Grid.Resources>
    <viblendDataGrid:DataGrid ItemsSource="{Binding}" Name="dataGrid1" Margin="10">
        <viblendDataGrid:DataGrid.BoundFields>
            <viblendDataGrid:BoundField Text="Name" DataField="Name" Width="175" SortMode="Automatic"/>
            <viblendDataGrid:BoundField Text="Phone Number" DataField="PhoneNumber" CellEditorActivationFlags="MOUSE_CLICK_SELECTED_CELL" CellEditorDeActivationFlags="MOUSE_CLICK" CellEditTemplate="{StaticResource cellEditMaskEditorTemplate}" Width="175"/>
        </viblendDataGrid:DataGrid.BoundFields>
    </viblendDataGrid:DataGrid>
</Grid>

 

2.. Populate the DataGrid.

C#

public partial class MainPage : UserControl
 {
        public MainPage()
        {

            InitializeComponent();

            List<User> users = new List<User>();

            for (int i = 0; i < 3; i++)
            {
                User user = new User(name[i], phones[i]);
                users.Add(user);
            }

            this.DataContext = users;
        }

        public class User
        {
            public User(string name, string phoneNumber)
            {
                this.Name = name;
                this.PhoneNumber = phoneNumber;
            }

            public string Name
            {
                get;
                set;
            }

            public string PhoneNumber
            {
                get;
                set;
            }
        }
 

        string[] name = new string[]
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                };

        string[] phones = new string[]
                {
                    "(617) 245-9601",
                    "(615) 621-2121",
                    "(612) 555-3251",
                };
    }

 VB .NET

Partial Public Class MainPage
    Inherits UserControl
        Public Sub New()

            InitializeComponent()

            Dim users As List(Of User) = New List(Of User)()

            For i As Integer = 0 To 2
                Dim user As New User(name(i), phones(i))
                users.Add(user)
            Next i

            Me.DataContext = users
        End Sub

        Public Class User
            Public Sub New(ByVal name As String, ByVal phoneNumber As String)
                Me.Name = name
                Me.PhoneNumber = phoneNumber
            End Sub

            Private privateName As String
            Public Property Name() As String
                Get
                    Return privateName
                End Get
                Set(ByVal value As String)
                    privateName = value
                End Set
            End Property

            Private privatePhoneNumber As String
            Public Property PhoneNumber() As String
                Get
                    Return privatePhoneNumber
                End Get
                Set(ByVal value As String)
                    privatePhoneNumber = value
                End Set
            End Property
        End Class


        Private name() As String = { "Andrew", "Nancy", "Shelley" }

        Private phones() As String = { "(617) 245-9601", "(615) 621-2121", "(612) 555-3251" }
End Class

3. In order to format a grid cell value into a MaskEditor.EditorValue, create a class that implements the IValueConverter interface and then implement the Convert and ConvertBack methods. The following code snippet shows the implementation of a phone-number converter that converts the grid cell value passed in so that it displays it in the MaskEditor.

C#

public class PhoneNumberConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string valueString = value.ToString();
        string phoneNumberString = "";
        for (int i = 0; i
<valueString.Length; i++)
        {
            if (Char.IsDigit(valueString[i]))
            {
                phoneNumberString += valueString[i];
            }
        }

        return phoneNumberString;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            string phoneString = value.ToString();

            if (phoneString.Length >0)
            {
                phoneString = phoneString.Insert(0, "(");
            }

            if (phoneString.Length > 4)
            {
                phoneString = phoneString.Insert(4, ")");
            }

            if (phoneString.Length > 8)
            {
                phoneString = phoneString.Insert(8, "-");
            }

            return phoneString;
        }
        else
        {
            return "";
        }
    }
}

VB .NET

 Public Class PhoneNumberConverter
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object
        Dim valueString As String = value.ToString()
        Dim phoneNumberString As String = ""
        For i As Integer = 0 To valueString.Length - 1
            If Char.IsDigit(valueString.Chars(i)) Then
                phoneNumberString &= valueString.Chars(i)
            End If
        Next i

        Return phoneNumberString
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object
        If value IsNot Nothing Then
            Dim phoneString As String = value.ToString()

            If phoneString.Length >0 Then
                phoneString = phoneString.Insert(0, "(")
            End If

            If phoneString.Length > 4 Then
                phoneString = phoneString.Insert(4, ")")
            End If

            If phoneString.Length > 8 Then
                phoneString = phoneString.Insert(8, "-")
            End If

            Return phoneString
        Else
            Return ""
        End If
    End Function
End Class

 

 

 

Scrolling Animation in VIBlend Silverlight DataGrid

by viblend 28. November 2010 10:20

The latest version of VIBlend Controls for Silverlight features significant improvements to the DataGrid’s scrolling capabilities.  DataGrid now offers fast animated smooth scrolling with inertia for impressive UI performance.  In this blog post, we will introduce you how to use the properties that you need to know, in order to set up the DataGrid’s scrolling behavior. The ScrollAnimationMode property allows you to enable or disable the animated scrolling. The DataGrid’s default animation mode is scrolling with intertia,  In order to disable the scrolling, you need to set the ScrollAnimationMode property to ScrollAnimationMode.No_Animation.

C#

dataGrid.ScrollAnimationMode = DataGrid.ScrollAnimationModes.NO_ANIMATION;

VB .NET

dataGrid.ScrollAnimationMode = DataGrid.ScrollAnimationModes.NO_ANIMATION

Use the following code snippet to enable the smooth scrolling mode:

C#

dataGrid.ScrollAnimationMode = DataGrid.ScrollAnimationModes.SMOOTH_SCROLL;

VB .NET

dataGrid.ScrollAnimationMode = DataGrid.ScrollAnimationModes.SMOOTH_SCROLL

VIBlend DataGrid control also supports a beautiful opacity animation while you scroll its content. You can enable this animation by using the ScrollOpacityAnimationEnabled property. The code snippet below enables the scrolling opacity animation:

C#

dataGrid.ScrollOpacityAnimationEnabled = true;

VB .NET

dataGrid.ScrollOpacityAnimationEnabled = True

The ScrollAnimationTime property allows you to change the duration of the scrolling animation. Here is demonstrated how to the set the duration to 1 second:

C#

dataGrid.ScrollAnimationTime = new TimeSpan(0, 0, 1);

VB .NET

dataGrid.ScrollAnimationTime = New TimeSpan(0, 0, 1)

By default, when the user drags the thumb on a scrollbar, the content view continuously updates. In deferred scrolling, the content is updated only when the user releases the thumb. You can enable the deferred scrolling mode by using the DeferredScrollingEnabled property.

For example:

C#

dataGrid.DeferredScrollingEnabled = true;

VB .NET

dataGrid.DeferredScrollingEnabled = True

How to bind the VIBlend DataGrid for Silverlight and WPF to Indexed properties?

by viblend 3. September 2010 19:48

In order to bind the VIBlend DataGrid to indexed properties, you need to do the following:

1. Create DataTemplates that are bound to indexed properties.

        <DataTemplate x:Key="LastNameCellTemplate">
            <Grid>
                <TextBlock Text="{Binding [LastName], Mode=OneWay}"/>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="FirstNameCellTemplate">
            <Grid>
                <TextBlock Text="{Binding [FirstName], Mode=OneWay}"/>
            </Grid>
        </DataTemplate>

2. Create a new DataGrid instance. Set the CellDataTemplate property of the DataGrid’s BoundFields to point to the DataTemplates.

        <viblend:DataGrid x:Name="dataGrid" Width="400" Height="280" AutoGenerateColumns="True">
            <viblend:DataGrid.BoundFields>
                <viblend:BoundField Text="FirstName" Width="150" CellDataTemplate="{StaticResource FirstNameCellTemplate}"/>
                <viblend:BoundField Text="LastName" Width="150" CellDataTemplate="{StaticResource LastNameCellTemplate}"/>
            </viblend:DataGrid.BoundFields>
        </viblend:DataGrid>

3.  Create a new class that will represent a single record of the DataGrid.

CSharp

public class Person : INotifyPropertyChanged
{   
    public Person()
    {
    }

    private Dictionary<string, object> data = new Dictionary<string, object>();

    public object this[string key]
    {
        get
        {
            if (!data.ContainsKey(key))
            {
                data[key] = null;
            }
            return data[key];
        }
        set
        {
            data[key] = value;
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(""));
            }
        }
    }

    public IEnumerable<string> Keys
    {
        get
        {
            return data.Keys;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

VB .NET

Public Class Person
    Implements INotifyPropertyChanged
    Public Sub New()
    End Sub

    Private data As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()

    Default Public Property Item(ByVal key As String) As Object
        Get
            If (Not data.ContainsKey(key)) Then
                data(key) = Nothing
            End If
            Return data(key)
        End Get
        Set(ByVal value As Object)
            data(key) = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(""))
        End Set
    End Property

    Public ReadOnly Property Keys() As IEnumerable(Of String)
        Get
            Return data.Keys
        End Get
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler
End Class

4.  Create a new generic List of Person objects and set the ItemsSource property of the DataGrid, in order to bind it to the list.

CSharp

       List<Person> listOfPersons = new List<Person>();
       for (int i = 0; i < 10; i++)
       {
           Person person = new Person();
           person["FirstName"] = "FirstName" + i;
           person["LastName"] = "LastName" + i;
           listOfPersons.Add(person);
       }
       this.dataGrid.ItemsSource = listOfPersons;

VB .NET

      Dim listOfPersons As List(Of Person) = New List(Of Person)()
      For i As Integer = 0 To 9
         Dim person As New Person()
         person("FirstName") = "FirstName" & i
         person("LastName") = "LastName" & i
         listOfPersons.Add(person)
      Next i
      Me.dataGrid.ItemsSource = listOfPersons

VIBlend Silverlight Controls - Released

by viblend 10. November 2009 13:59
We are proud to announce the official release of VIBlend Controls for Silverlight - professional suite of easy to use and feature complete .NET user interface controls for Microsoft Silverlight. VIBlend Silverlight Controls allow you to develop cross-platform rich internet applications (RIA) for a wide range of business scenarios.
VIBlend Silverlight Controls is the first toolkit to introduce a fully functional OLAP DataGrid control for Silverlight. VIBlend DataGrid for Silverlight brings together the features of traditional data grid controls, hierarchical grids and Excel-like pivot tables, and delivers consistent and powerful end-to-end functionality.

VIBlend Silverlight Controls is available for purchase online at http://www.viblend.com
A full feature trial is available for download at http://www.viblend.com and partner websites.

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