0

I have the following situation in a Visual Studio 2010 (C#) Silverlight 4 project using the DataGrid (this is pseudocode for brevity sake):

public class BaseClass {
    public string str1;
    public string str2;
}

public class DerivedClass : BaseClass {
    public string str3;
    public string str4;
}

public List<DerivedClass> SetItemSource(List<DerivedClass> list) {
    dataGrid.ItemSource = list;
}

When I run the code, the columns are in the order:

str3 str4 str1 str2

I would like them to display as:

str1 str2 str3 str4

Is there any way of doing this? I am finding the Silverlight DataGrid to be very inflexible.

Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
JayH
  • 1
  • 1

1 Answers1

0

When I have just a standard class & I want to change the ordering of the columns, I use annotations:

[Display(Order=2)]
public string str1 { get; set }

[Display(Order=1)]
public string str2 { get; set; }

I just tried this with derived classes:

 public class BaseClass
    {
        [Display(Order=1)]
        public string str1 { get; set; }
        [Display(Order=3)]
        public string str2 { get; set; }
    }

    public class DerivedClass : BaseClass
    {
        [Display(Order=4)]
        public string str3 { get; set; }
        [Display(Order=2)]
        public string str4 { get; set; }
    }

My grid columns were: str1 str4 str2 str3 as expected.

[EDIT] Needed to include a using declaration: using System.ComponentModel.DataAnnotations; to use [Display...] I really enjoy working with both the gridview & the dataform. Most people hate hate hate the dataform. Its taken many frustrating hours to understand how to make it fit/work within our LOB apps, but now it's incredibly powerful and blazing fast for UI dev.

Also - if you're not familiar with Data Annotations, they're absolutely rad. You can set attributes like [Required(true)], [Display(Name="String 3")], [StringLength(3,ErrorMessage="This field can not exceed 3 characters in length")], Range(0,10,ErrorMessage="Must be 0-10)")] etc etc. The validation errors auto bubble up to the UI. They've saved seriously crazy amounts of time in our dev process. [/EDIT]

Scott Silvi
  • 3,059
  • 5
  • 40
  • 63