3

for this example , my computer generate automatic columns, but in others machines, columns are not generating if source is Queryable or Enumrable .

what can be the different

   public MainWindow()
    {
        InitializeComponent();
        dg.DataContext = GetPaople();
    }

    public object GetPaople()
    {
        List<Person> list = new List<Person>();
        for (int i = 0; i < 15; i++)
        {
            list.Add(new Person() { FirstName = "F" +  i, LastName = "L" + i, Id = i });
        }
        var res = from p in list select p;
        return res.AsQueryable();//not Genrating Columns
        return list;//Genrating Columns
    }
H.B.
  • 166,899
  • 29
  • 327
  • 400
mordechai
  • 829
  • 1
  • 7
  • 23
  • 1
    The columns are generated based on the properties in Person. The .AsQueryable method makes it an IEnumarble which does not return a result if it is not accessed. I guess the DataGrid does not trigger the enumeration for you and the only way of autogenerating the columns is to return a collection. – Silvermind Mar 13 '12 at 23:17
  • 1
    What is the functional problem you are trying to solve? What does List not provide? – paparazzo Mar 13 '12 at 23:21
  • 1
    Agree with @Silvermind and @Blam, a LINQ query must be **accessed** using extensions like .Count(), .ToList(), .ToArray() etc., only then it will evaluate into a collection. Why dont you use your `List` as it is? Do you want to make datagrid's itemssource `IQueryable` so that it performs sorting, filtering and grouping fast using expressions? Thats a good suggestion but sadly not implemented by microsoft for its inbuilt WPF datagrid. I am upvoting your question for an interesting discussion. – WPF-it Mar 14 '12 at 05:39
  • Strange thing is that I can see the rows using AsQueryable but indeed no columns. So I would agree with @Blam. Or you could try creating the columns manually. – Silvermind Mar 14 '12 at 08:34
  • IQueryable get me dynamic query on my base object, i won't display all properties always. if the propble is accessible , why it's work on my computer? only on others it's dosen't work. – mordechai Mar 14 '12 at 18:53
  • LINQ supports List. What is your query problem? You may not be approaching this from the best direction. Casting from Var to Object just leaves for a lot casting. – paparazzo Mar 15 '12 at 16:02

1 Answers1

1

I don't think the WPF DataGrid plays well with IQueryable collections. Instead of res.AsQueryable(), try res.ToList().

By using ToList(), you force the query to be evaluated and the results will be dumped into a list, which in this case, will be strongly typed and can be inspected by the DataGrid to generate the columns.

Brandon Baker
  • 1,232
  • 12
  • 16