SubSonic 3. Get results as DataTable - subsonic

How can I get .All() method's result as a DataTable?
Currently it returns IQueryable, which can't be used as datasource for the WinForms DataGridView control.
dataGridView1.DataSource = Product.All(); // not working

You can bind a List to a DataGridView control so just use the ToList() method on the IQueryable e.g.
MyDataGridView.DataSource = MyObject.All().ToList();

Unless your Product class implements one of the following: IList, IListSource, IBindingList, IBindingListView; you won't be able to bind the result to your DataGridView.

For two way binding you can use a BindingList
dataGridView1.DataSource = new BindingList<Product>(Product.All().ToList());
The BindingList will update automatically when you add/remove rows from the DataGridView and the DataGridView will update automatically whan you add items to the binding list.
If you want to automatically update the DataGridView when modifying a Product, Product must implement INotifyPropertyChaged

Related

ClosedXML insertTable formatting and order

I have a .NET Core 3.1 WebApp using ClosedXML to export data to Excel.
So far I'm very impressed.
I use EFCore to map my query result to a class and that data gets exported:
worksheet.Cell(1, 1).InsertTable(data, false);
I already figured out I can use the XLColumn attribute to control the headers in the Excel sheet.
But I have about 100 columns, most of them can have the name of the property but some need to change.
I've added the properties of the class in the order I want to export them to Excel but if I add the XLColumn attribute the order is changed. It looks like first all properties with this attribute are exported and next the remaining properties.
Can I change that, because now I need to add the XLColumn attribute to all of my properties?
A second related question.
Most of my decimal properties need to be exported as a currency. How to tell ClosedXML that?
I tried adding [DataType(DataType.Currency)] to my property but that didn't change anything.

Get TreeListView cell value programmatically in ObjectListView

How can I programmatically obtain the value of a cell in OLV's treeListView using the row and column index?
treeListView.GetItem(0).GetSubItemAt(6,e.rowIndex).Text
for example does not appear to work, it just gives me the string value of the first cell of the first row. Also I do not understand the answer here:
ObjectListView: select subitem programmatically
Working with the sub items is not recommended and you should never have to access them when using the ObjectListView/TreeListView controls.
You should retrieve the model object of the required row instead, and then access the property that is related to the column/cell in question.
You can retrieve the model with
MyModel model = objectListView1.GetModelObject(rowIndex) as MyModel
where MyModel is your underlying model class.

With JPA, EclipseLink, JSF and reporting dynamically

I have 3 database tables and each table is represented by an Entity Class. What I want to do is to Join these three tables with a special condition and select maximum one column from each table wrap them in an object and display this object on the JSF tier using a data table.
Is it possible to do this using dynamic entity class without playing with my entity classes? Has anyone got a solution for this problem?
Will appreciate any help
Thanks a lot
You can use a constructor query, or just simply select multiple values and get an Object[] back.
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Constructors
You could also just select the objects, then extract what you need from them in Java.

string;# in dropdownlist

I made a custom dropdownfield. I want to display the full names of my contacts. I made a calculated column because not every user filled in their full name, only first and last. If I wanna show the items in my dropdown there is always string;# before my value. Does somebody know how to remove that?
Here is the code where i fill my dropdown
SPWeb web = site.OpenWeb();
SPDataSource dataSource = new SPDataSource();
dataSource.List = web.Lists["Contacts"];
this.testing.DataSource = dataSource;
this.testing.DataTextField = "Full Name";
this.testing.DataValueField = "Full Name";
this.testing.DataBind();
thx
The field type is probably "User" (I think shows as "People" in the UI). This and other lookup fields are always stored by SharePoint in this manner. The value component is the user ID and the string component is the user name.
You could use a Regex to split the components. You can also cast the value to SPFieldUserValue which is the 'proper' way, and use the properties on that class to retrieve info.
Assuming full name is a text field, it looks like you have an error in your calculated column definition related to one of your inputs being a lookup field.
If you can't get rid of it in a cleaner way, there's always substr (or maybe right, I can never remember the syntax for excel functions)

SharePoint list.items.GetDataTable column names not match field names

I am binding an SPGridView to a SPList. As code samples suggest, I am using the following code to create a dataview based on the list.
dim data as DataView = myList.Items.GetDataTable.DefaultView
grid.DataSource = data
etc...
What I am finding is that the column names in the resulting dataview do not always match the source fields defined in the SPList. For example I have columns named
Description
ReportItem
ReportStatus
these show up in the resulting dataview with column names like
ReportType0
ReportStatus1
This leads me to think that I have duplicate field names defined, but that does not seem to be the case.
Seems like I am missing something fundamental here?
Thanks.
The GetDataTable method is returning the internalName (or staticName -- I can't remember for sure which but they are frequently the same) representation of the columns, rather than the Title representation, which is what you see in the Web interface. I believe GetDataTable does a CAML query under the covers, and you have to use that internalName for field references in CAML.
This blog talks about it in a little more detail.
So I posted about this on my blog, but I wrote a little utility method that you can use, right after you get the data table it basically remaps the column names in the DataTable to their friendly names.
DataTable table = list.GetItems(list.DefaultView).GetDataTable();
foreach(DataColumn column in table.Columns)
{
column.ColumnName = list.Fields.GetFieldByInternalName(column.ColumnName).Title;
}
Hope that helps!
What you also could do (if you´re using .NET 3.5) is to use an anonymous type and bind against that. If you´re doing this you might wanna go with a Linq DataSource as well.
I have made a post that explains this here.

Resources