LINQ and Devexpress Grid Data Sources

I have a DevExpress grid (DevExpress.XtraGrid.GridControl 8.2) with a data source set at runtime like this:

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 how the data will look at design time. I like to set up a LINQ query as a data source, but I would also like to specify how the view will look at design time.

  • Is there a way to show that it will use this query?
  • A better solution would be to create a small object to hold the content returned from this request?
0


a source to share


4 answers


You will need to define a class for the return type of your LINQ query if you want the DevExpress grid to automatically collect columns for the data source. At design time, the WinForm binding engine uses reflection or ICustomTypeDescriptor if the source implements it to automatically detect properties, their types, etc. Data source. DevExpress Grid uses this basic binding mechanism and automatically generates columns for you at design time based on property information. However, in your case, you are creating an anonymous type in a LINQ query that is unknown or available at design time. Therefore DevExress Grid cannot generate columns automatically. As @Dennis pointed out, you can manually add columns to the grid in the designer. You have to make sure that the "FieldName" field, I believein the column matches the name of the property in your data source.



If you go with a class, you can also implement INotifyPropertyChanged to let the network know about data changes in the data source.

+2


a source


IIRC, xtragrid requires the datasource to implement the data binding interface (i.e. IBindingList (T)) in order for it to generate columns automatically, and the items must implement INotifyPropertyChanged.

With this in mind, if you create columns using a wizard at design time or code at runtime, while you set the FieldName property for the columns, they will display data from a data source with a property of that name.



Notes:

  • I think it should be a property, auto or not, as I found that it sometimes doesn't bind to public variables.
  • The property must be assigned something (default or otherwise).
  • The element must have a parameterless constructor.
+1


a source


The fields are known at design time (Field1, Field2, Field3).

In accordance with the DevExpress you can use IList

, IListSource

, ITypedList

or IBindingList

. The difference between the two is whether you can add new lines or the changes will edit the control.

So, you can use ToList ():

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
                         }).ToList();

      

Note . I tested it with DevExpress 10.1 but if it uses WinForms binding it should still work according to MSDN .

+1


a source


I didn't work with DevExpress network, but I did a lot with .NET DataGridView.

Does DevExpress Grid have the same functionality as .NET DataGridView which automatically generates columns?

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

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

0


a source







All Articles