This is a migrated thread and some comments may be shown as answers.

Filter and grouping not working on GridView

0 Answers 120 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chirag
Top achievements
Rank 1
Chirag asked on 14 May 2011, 04:38 AM
hello 
i am face one problem regarding a filter and grouping  in gridView
in my application grid column will generate dynamically as per the XML which is return from WCF Service 
base on XML generate Column for grid and one list for assign as Source in radDataPager control
Please Help me as soon as possible it's urgent 

My code 
<UserControl x:Class="RadControlsSilverlightApp1.MainPage"
        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <Grid x:Name="LayoutRoot">
        <StackPanel Orientation="Vertical">
            <telerik:RadGridView x:Name="FormsGrid"
                                 Margin="5,0,10,0"
                                 CanUserFreezeColumns="False" 
                                 AutoGenerateColumns="False"  
                                 ScrollMode="RealTime" MinHeight="300"  
                                 BorderThickness="1,6,1,1"
                                 telerik:StyleManager.Theme="Office_Blue"
                                 ScrollViewer.HorizontalScrollBarVisibility="Visible" 
                                 ItemsSource="{Binding PagedSource, ElementName=radDataPager1}" 
                                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                                 SelectionChanged="FormsGrid_SelectionChanged"
                                 DataLoaded="FormsGrid_DataLoaded"    
                                 LayoutUpdated="FormsGrid_LayoutUpdated"
                                 CanUserDeleteRows="False"
                                 CanUserInsertRows="False"
                                 IsFilteringAllowed="True">
            </telerik:RadGridView>
            <telerik:RadDataPager Grid.Row="1" x:Name="radDataPager1" telerik:StyleManager.Theme="Office_Blue"
                                   PageSize="15"
                                   DisplayMode="All"                                 
                                   IsTotalItemCountFixed="True"
                                   Margin="5,0,10,0"/>
        </StackPanel>
    </Grid>
</UserControl>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml;
using System.Xml.Linq;
using Telerik.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
 
namespace RadControlsSilverlightApp1
{
    public partial class MainPage : UserControl
    {
        Dictionary<string, string> selectedEntity = new Dictionary<string, string>();
 
        public MainPage()
        {
            InitializeComponent();
            this.FormsGrid.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
            BindGrid();
        }
        private void BindGrid()
        {
            string myxml = @"<NewDataSet>
                            <Table><ID>1</ID><CheckboxCol>0</CheckboxCol><First_x0020_Name>ABC</First_x0020_Name><LastName>MNO</LastName><Contact_x0020_Number>1234567890</Contact_x0020_Number></Table>
                            <Table><ID>2</ID><CheckboxCol>0</CheckboxCol><First_x0020_Name>DEF</First_x0020_Name><LastName>PQR</LastName><Contact_x0020_Number>1234567890</Contact_x0020_Number></Table>
                            <Table><ID>6</ID><CheckboxCol>0</CheckboxCol><First_x0020_Name>GHI</First_x0020_Name><LastName>STU</LastName><Contact_x0020_Number>1234567890</Contact_x0020_Number></Table>
                            <Table><ID>9</ID><CheckboxCol>0</CheckboxCol><First_x0020_Name>JKL</First_x0020_Name><LastName>VWX</LastName><Contact_x0020_Number>4646464</Contact_x0020_Number></Table></NewDataSet>";
 
            StringReader stream = new StringReader(myxml.ToString());
            XmlReader reader = XmlReader.Create(stream);
 
            XDocument myDoc = new XDocument();
            myDoc = XDocument.Load(reader);
 
            List<Dictionary<string, string>> listdict = new List<Dictionary<string, string>>();
            Dictionary<string, string> elem = new Dictionary<string, string>();
            FormsGrid.Columns.Clear();
            FormsGrid.AutoGenerateColumns = false;
            FormsGrid.IsFilteringAllowed = true;
            if (myDoc != null && myDoc.Elements().Count() > 0 && myDoc.Element("NewDataSet").Elements().Count() > 0)
            {
                int columnCount = myDoc.Element("NewDataSet").Element("Table").Elements().Count();
                string columnName = string.Empty;
                GridViewDataColumn dgtc = new GridViewDataColumn();
 
                for (int i = 0; i < columnCount; i++)
                {
                    XElement element = myDoc.Element("NewDataSet").Element("Table").Elements().ElementAt(i);
 
                    dgtc = new GridViewDataColumn();
                    dgtc.MinWidth = 100;
                    dgtc.IsReadOnly = true;
                    dgtc.Width = GridViewLength.SizeToHeader;
 
                    if (element.Name == "ID")
                    {
                        dgtc.IsVisible = false;
                    }
                    if (element.Name == "CheckboxCol")
                    {
                        dgtc.IsVisible = false;
 
                    }
 
                    dgtc.Header = element.Name.ToString().Replace("_x0020_", " ").Replace("_x0028_", " (").Replace("_x0029_", ") ");
 
                    dgtc.DataMemberBinding = new Binding(element.Name.ToString().Replace("-", " "));
                    dgtc.DataMemberBinding.Source = element.Name;
 
                    indexingConverter convert = new indexingConverter();
                    Binding bind = new Binding { Converter = convert, ConverterParameter = element.Name };
 
                    dgtc.DataMemberBinding = bind;
                    dgtc.IsFilterable = true;
                    FormsGrid.Columns.Add(dgtc);
                }
 
                int rowCount = myDoc.Element("NewDataSet").Elements().Count();
 
                for (int i = 0; i < rowCount; i++)
                {
                    XElement element = myDoc.Element("NewDataSet").Elements().ElementAt(i);
                    string[] values = element.Value.Trim().Split('\n');
 
                    elem = new Dictionary<string, string>();
                    for (int j = 0; j < element.Elements().Count(); j++)
                    {
                        XElement ele = element.Elements().ElementAt(j);
                        elem.Add(ele.Name.ToString(), ele.Value.ToString());
 
                    }
 
                    listdict.Add(elem);
                }
                radDataPager1.Source = null;
                radDataPager1.Source = listdict;
            }
            else
            {
                radDataPager1.Source = null;
                radDataPager1.Source = listdict;
 
            }
        }
        private void FormsGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
        }
        private void FormsGrid_DataLoaded(object sender, EventArgs e)
        {
 
        }
        private void FormsGrid_LayoutUpdated(object sender, EventArgs e)
        {
            if (selectedEntity != null && selectedEntity.Count > 0)
            {
                FormsGrid.SelectedItem = selectedEntity;
                foreach (Dictionary<string, string> item in this.FormsGrid.Items)
                {
                    if (item["ID"] == selectedEntity["ID"])
                    {
 
                        var row = (GridViewRow)(this.FormsGrid.ItemContainerGenerator.ContainerFromItem(item));
                        if (row != null)
                        {
                            row.IsSelected = true;
 
                        }
                    }
                }
            }
        }
        private void OnCellDoubleClick(object sender, RadRoutedEventArgs args)
        {
            if (FormsGrid.SelectedItem != null)
            {
                Dictionary<string, string> dict = (Dictionary<string, string>)FormsGrid.SelectedItem;
                selectedEntity = dict;
            }
        }
    }
    class indexingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Dictionary<string, string> columnData = (Dictionary<string, string>)value;
            return columnData[parameter.ToString()];
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Chirag
Top achievements
Rank 1
Share this question
or