views:

2137

answers:

3

I have a DevExpress grid (DevExpress.XtraGrid.GridControl 8.2) with a datasource set at runtime like so:

private DataContext db = new DataContext("connection string");
gridControl.DataSource = from t in db.sometable
                          select new
                          {
                              Field1 = t.Name,
                              Field2 = t.Email,
                              Field3 = t.City
                          };

This means that the view has no idea what the data is going to look like at design time. I like being able to set a LINQ query as the datasource, but I'd also like to specify what the view will look like at design time.

  • Is there a way that I can tell the view that it will be using this query?
  • Would the best solution be to create a small object for holding the contents of what gets returned from this query?
A: 

I haven't worked with the DevExpress grid, but I've done a lot with the .NET DataGridView.

Does the DevExpress grid have the same functionality as the .NET DataGridView that auto generates columns?

If so, then it should display whatever fields are found in your query and will use Field1, Field2 and Field3 (from your example code) as column names.

Or just turn off the auto generate column feature and add the columns at design time. As long as they match what your query returns it should work fine.

Dennis Palmer
+1  A: 

IIRC, the xtragrid requires that the datasource implement a databinding interface (ie IBindingList(T)) for it to auto-generate columns and the items should implement INotifyPropertyChanged.

With that in mind: if you do create columns via the wizard at design time or in code at runtime, as long as you set the FieldName property of the columns, they will show the data from the datasource with a property of that name.

Notes:

  • I think it must be a property, auto or not, as I've found that it sometimes won't bind to public variables.
  • The property must be assigned something (default or otherwise).
  • There must be a parameterless constructor for the item.
SnOrfus
+1  A: 

You will have to define a class for the return type of your LINQ query if you want the DevExpress grid to automatically pick up the columns for the data source. At design time, the WinForm binding engine is using reflection or ICustomTypeDescriptor if the source implements it to automatically discover the properties, their types, etc of the data source. The DevExpress grid is using this underlying binding mechanism and automatically generating the columns for you at design time based on the property information. In your case however, you're creating an anonymous type in your LINQ query which is not known or available at design time. Therefore, DevExress Grid cannot generate the columns automatically. As @Dennis mentioned, you can manually add columns to the grid in designer. You need to make sure that 'FieldName', I believe, on the column matches the property name on your data source.

If you go with a class, you may also want to implement INotifyPropertyChanged to make the grid aware of data changes in the data source.

Mehmet Aras