Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wpf DataGrid Repeated Columns

Tags:

c#

wpf

datagrid

I want a datagrid which has repeated columns in it as you can see in the picture below.

enter image description here

This is just one datagrid and colums are repeated. How can we do this with wpf windows datagrid?

Thanks,

like image 703
Seçkin Durgay Avatar asked Jul 10 '13 07:07

Seçkin Durgay


People also ask

How many types of custom columns are there in WPF DataGrid?

WPF DataGrid provides 5 types of custom columns types: CheckBoxColumn, ComboBoxColumn, HyperlinkColumn, TemplateColumn, TextColumn. Menu Home C# Versions and Features C# Params

How to add or remove columns in WPF DataGrid (sfdatagrid)?

WPF DataGrid (SfDataGrid) allows you to add or remove columns using SfDataGrid.Columns property. You can choose the columns to be added from built-in column types or you can create your own column and add to the SfDataGrid.Columns. Below are the built-in column types supported in SfDataGrid.

What is the difference between datagridcolumn and datagridboundcolumn?

Columns in the collection must derive from DataGridColumn. Note that DataGridBoundColumn, which adds support for binding, derives from DataGridColumn and is the base for several of the defined column types. All columns in the collection use the ItemsSource property defined by the DataGrid.

How to modify the columns collection in a DataGrid?

All columns in the collection use the ItemsSource property defined by the DataGrid. You can modify the Columns collection at run time regardless of whether it contains generated columns. Gets the list of cells that are currently selected. Represents a DataGrid column.


1 Answers

I tried to implement your problem in the following way.

There is a certain amount of data that must be defined in the columns. But these few, and we want them to define a repeating column in the DataGrid.

Suppose it is known, at least approximately the number of columns and rows. Then be the data cube to be set in the DataGrid. Below is a my example with my comments.

XAML

<Window x:Class="DataGridDynamicAddCol.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" 
    WindowStartupLocation="CenterScreen" ContentRendered="Window_ContentRendered">

    <Grid>
        <DataGrid Name="DynamicColumnDataGrid" Loaded="DynamicColumnDataGrid_Loaded" AutoGenerateColumns="False"/>
    </Grid>
</Window>

Code behind

public partial class MainWindow : Window
{        
    // Create class with data
    // Note: Each parameter contains a list of values for it.
    public class Person
    {
        public Person() 
        {
            FirstName = new List<string>();
            SecondName = new List<string>();
            Sample = new List<string>();
        }

        public List<string> FirstName { get; set; }

        public List<string> SecondName { get; set; }

        public List<string> Sample { get; set; }
    }

    // Number of columns
    const Int32 COLS = 3;

    // Number of rows
    const Int32 ROWS = 3;

    // Number repeats
    const Int32 RPTS = 3;

    // Array of headers
    string[] HeadersArray = new string[COLS] 
    { 
        "FirstName", 
        "SecondName", 
        "Sample"
    };

    // Array of values: Depends on the number of columns and rows
    // Note: The number of repetitions you can specify smaller amounts of data
    // If you specify more, then this place will be empty cells
    string[, ,] ValuesArray = new string[, ,] 
    {
        {
            { "Andy", "Caufmann", "Artist"},
            { "Sam", "Fisher", "Spy"},
            { "Ben", "Affleck", "Actor"}
        },
        {
            { "Jim", "Gordon", "Sniper"},
            { "Maria", "Gray", "Cleaner"},
            { "Katy", "Parry", "Artist"}
        },
        {
            { "Jack", "Rider", "Hunter"},
            { "Sven", "Vath", "DJ"},
            { "Paul", "Kalkbrenner", "Super DJ"}
        }
    };

    private List<Person> OnePerson;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        OnePerson = new List<Person>();

        // Create the columns
        for (int cols = 0; cols < COLS; cols++)
        {
            OnePerson.Add(new Person());
        }

        // Create the rows
        for (int cols = 0; cols < COLS; cols++)
        {
            for (int rows = 0; rows < ROWS; rows++)
            {
                OnePerson[rows].FirstName.Add(ValuesArray[cols, rows, 0]);
                OnePerson[rows].SecondName.Add(ValuesArray[cols, rows, 1]);
                OnePerson[rows].Sample.Add(ValuesArray[cols, rows, 2]);
            }
        }

        DynamicColumnDataGrid.ItemsSource = OnePerson;
    }

    private void DynamicColumnDataGrid_Loaded(object sender, RoutedEventArgs e)
    {
        // Create dynamically the columns and rows
        for (int rpts = 0; rpts < RPTS; rpts++ )
        {
            for (int cols = 0; cols < COLS; cols++)
            {
                DataGridTextColumn MyTextColumn = new DataGridTextColumn();
                MyTextColumn.Header = HeadersArray[cols];

                // Binding values from HeadersArray
                MyTextColumn.Binding = new Binding(String.Format("{0}[{1}]", 
                    new object[]
                    {
                        HeadersArray[cols], rpts
                    }
                ));

                // Add column in DataGrid
                DynamicColumnDataGrid.Columns.Add(MyTextColumn);
            }
        }
    }
}

Output

The number of repetitions - 3:

enter image description here

The number of repetitions - 2:

enter image description here

If you specify the number of repetitions is greater than it is in the cube, you will see a picture:

enter image description here

That is quite logical, since the relevant data are not available.

Note: To use the Binding a ViewModel, you need to determine the appropriate lists / collection, set them to the elements and assemble them into a single data cube. In my example, static data, but you need to determine the source of the data, the amount of data, then set them aside and put them in a data cube. I think, without a data cube, it will be difficult to implement something of this kind (I tried different options, the use of the Converter, Binding directly from the array, etc).

like image 182
Anatoliy Nikolaev Avatar answered Sep 22 '22 01:09

Anatoliy Nikolaev