Datagrid in 2.0 preview, column order sorted?

Please post public support tickets here. Note: for private support tickets, please send an email to support@cshtml5.com instead.
ScottM
Posts: 24
Joined: Wed Mar 27, 2019 7:04 am

Datagrid in 2.0 preview, column order sorted?

Postby ScottM » Wed Apr 03, 2019 8:09 am

I've used a DataGrid in WPF before, never seen this. The XAML:

Code: Select all

<DataGrid
                    x:Name="MainDataGrid"
                    Margin="10,4,10,10"
                    HorizontalAlignment="Stretch"
                    AutoGenerateColumns="True" >
                    <DataGrid.Resources>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="Background" Value="Azure"/>
                            <Setter Property="BorderThickness" Value="1"/>
                            <Setter Property="BorderBrush" Value="SeaGreen"/>
                        </Style>
                    </DataGrid.Resources>
                </DataGrid>

The class to be populated as columns/rows:

Code: Select all

public class Prospects
    {
        public string Prospect { get; set; }
        public string Company { get; set; }
        public string Title { get; set; }
        public string Seller { get; set; }
        public string Delivered { get; set; }
        public string LinkedInConnectionRequest { get; set; }
        public string Email_LinkedIn_1 { get; set; }
        public string Call { get; set; }
        public string Email_LinkedIn_2    { get; set; }
        public string Email_LinkedIn_3    { get; set; }
        public string Email_LinkedIn_Break_Up  { get; set; }

    }

and the code to populate the ItemSource:

Code: Select all

       private List<Prospects> LoadData()
        {
            List<Prospects> prospects = new List<Prospects>();

            prospects.Add(new Prospects()
            {   Prospect = "name",
                Company = "company name",
                Title = "title",
                Seller = "salesperson"
            });
           
            <snip>
           
            prospects.Add(new Prospects()
            {
                Prospect = "another name",
                Company = "another company",
                Title = "another title",
                Seller = "another salesperson"
            });
            return prospects;
        }

and the result:

DataGrid.png
DataGrid.png (15.69 KiB) Viewed 135 times

which presents each column in alphabetical order. I don't think I asked for that, and would very much like to turn it off, but I don't see where I can do that. Is this a feature, bug, or blunder on my part?

fangeles
Posts: 26
Joined: Wed Jan 16, 2019 12:48 am

Re: Datagrid in 2.0 preview, column order sorted?

Postby fangeles » Thu Apr 04, 2019 9:22 pm

I think you need to specify each DataGrid Columns.

For example:

Code: Select all

  <DataGrid
                    x:Name="MainDataGrid"
                    Margin="10,4,10,10"
                    HorizontalAlignment="Stretch"
                    AutoGenerateColumns="False" >
            <DataGrid.Resources>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="Background" Value="Azure"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="BorderBrush" Value="SeaGreen"/>
                </Style>
            </DataGrid.Resources>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Prospect" Binding="{Binding Prospect }"/>
                <DataGridTextColumn Header="Company" Binding="{Binding Company }"/>
                <DataGridTextColumn Header="Title" Binding="{Binding Title }"/>
                <DataGridTextColumn Header="Seller" Binding="{Binding Seller }"/>
                <DataGridTextColumn Header="LinkedIn Connection Request" Binding="{Binding LinkedInConnectionRequest }"/>
                <DataGridTextColumn Header="Email LinkedIn 1" Binding="{Binding Email_LinkedIn_1 }"/>
                <DataGridTextColumn Header="Call" Binding="{Binding Call }"/>
                <DataGridTextColumn Header="Email LinkedIn 2" Binding="{Binding Email_LinkedIn_2 }"/>
                <DataGridTextColumn Header="Email LinkedIn 3" Binding="{Binding Email_LinkedIn_3 }"/>
                <DataGridTextColumn Header="Email LinkedIn Break Up" Binding="{Binding Email_LinkedIn_Break_Up }"/>
                <DataGridTextColumn Header="Prospect" Binding="{Binding Prospect }"/>
            </DataGrid.Columns>

        </DataGrid>


Make sure to set the AutoGenerateColumns to False

ScottM
Posts: 24
Joined: Wed Mar 27, 2019 7:04 am

Re: Datagrid in 2.0 preview, column order sorted?

Postby ScottM » Fri Apr 05, 2019 8:54 am

Thanks for the reply.

Yes, I tried defining my own columns with autogenerate false. Loading the ItemsSource didn't populate the correct columns.

It just struck me that there was some kind of default alphabetical column sort was magically going on when loading the ItemsSource. I suppose that could be useful, but it didn't seem to be a reasonable default.

Honestly, I just decided to punt and go with a general grid instead, for this particular test case.


Return to “Technical Support”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests