Click here to Skip to main content
15,877,145 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I would like to place a datagrid in my WPF application, that will be bound to a MYSQL database. Simple enough.

However because there are so many fields, and they will not all display on the screen at once (you will need to scroll bar to see the other fields), I would like to display my fields in a stack of controls including labels and text blocks.

Is this possible? Should I be looking for a different third party tool? If so, any suggestion?

Or is this just to hard to do.

Example of what I mean...
https://i345.photobucket.com/albums/p372/Grant_McConville/Datarow%20test_zps8to1zycb.jpg

What I have tried:

I have spent many hours googling this, but cannot find anybody else asking the same question, or any clue where to start.
Posted
Updated 29-Jan-20 22:10pm
v3

1 solution

You can use so-called DataGridTemplateColumns.

First you must switch off the automatic generation of columns by setting the AutoGenerateColumns property to "False".
Then you must provide a template for every column.

This example however just shows you how to define the templates for the first two columns.

The other columns will be your homework. :-) Good luck!

<Window x:Class="CustomDataRow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid Name="DataGridPersons" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border Background="LightGray">
                                <TextBlock Text="Address"></TextBlock>
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding Name}"></TextBlock>
                                <TextBlock Grid.Row="1" Text="{Binding Street}"></TextBlock>
                                <TextBlock Grid.Row="2" Text="{Binding Location}"></TextBlock>
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

The code behind for this example:
C#
using System.Collections.Generic;
using System.Windows;
using CustomDataRow.Models;

namespace CustomDataRow
{
    public partial class MainWindow
    {
        private List<Person> persons;

        public MainWindow()
        {
            Loaded += MainWindow_Loaded;

            InitializeComponent();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            persons = new List<Person>
            {
                new Person
                {
                    Name = "Frank Sinatra",
                    Street = "26 Music Lane",
                    Location = "New York"
                },
                new Person
                {
                    Name = "Paul Young",
                    Street = "Data",
                    Location = "New York"
                },
                new Person
                {
                    Name = "Jim Beam",
                    Street = "26 Music Lane",
                    Location = "New York"
                }
            };

            DataGridPersons.ItemsSource = persons;
        }
    }
}

and of course the Person model:
namespace CustomDataRow.Models
{
    public class Person
    {
        public string Name { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
    }
}
 
Share this answer
 
v2
Comments
Grant Mc 30-Jan-20 15:54pm    
Thanks for this, looks like a great starting point for me.
Grant Mc 2-Feb-20 4:40am    
Going great so far.

Once again, thank you very much TheRealSteveJudge
TheRealSteveJudge 5-Feb-20 2:08am    
Thank you for accepting the answer. I am glad that it helped you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900