Search This Blog

Monday, July 18, 2011

WPF toolkit datagrid usage; WPF datagrid column size;

The datagrid for old WPF (from Visual Studio 2008) is available as an addon from WPF toolkit project. WPF toolkit is available here. Source code for this example is available here. In this example one creates a class holding data in the grid (GridRow). Instances of this class are held in an ObservableCollection. It has to be ObservableCollection because only then changes in the collection will be reflected in the grid. You could use a simple List but the Grid will not update new information then.

Later on you set the grid’s ItemsSource as the ObservableCollection created earlier on.


        In XAML file:
    <dg:DataGrid AutoGenerateColumns="True"  Height="433" Name="dataGrid1" Width="809" Canvas.Left="4.999" Canvas.Top="55"  Margin="5" SelectionChanged="dataGrid1_SelectionChanged" AutoGeneratedColumns="dataGrid1_AutoGeneratedColumns">
        In cs file:
        public class GridRow
            public string Nr { get; set; }

            public string Lp { get; set; }

            public string Name { get; set; }

        public static ObservableCollection<GridRow> GetSampleCustomerList()
            ObservableCollection<GridRow> aaaa = new ObservableCollection<GridRow>();
            GridRow gr = new GridRow();
            gr.Nr = "10345";
            gr.Lp = "1";
            gr.Name = "Janina";
        private void constructDataGrid()
            dataGrid1.ItemsSource = GetSampleCustomerList();
        private void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
            dataGrid1.Columns[0].Width = 55; //Nr
            dataGrid1.Columns[1].Width = 46; //Lp
            dataGrid1.Columns[2].Width = 140; //Name

1 comment:

  1. Best implementation of wpf extended datagrid can be found here WPF Extended DataGrid

    Project Description

    This is extended version of WPF toolkit DataGrid control.

    This grid has features like

    1:Column Choosers.
    2:AutoFilter Control.
    3:Export To Excel Feature.
    4:Copy Paste To Excel and Copy Paste From Excel 5:To DataGrid.
    6:Three State Sorting.
    7:Displaying Sort Order If Multiple Sort is done


If you like this post, please leave a comment :)