How to combine 2 models into 1 for ObjectListView? - c#

I have the following model, which is central model and used in ObjectListView
Cells(List)
Number (same as Cell Number)
Status
Lots of properties
It looks like this:
I also have the following model, which is used in other ObjectListView.
Tests (List)
Cell Number
Enabled
Warning
Passes
Name
It looks like this:
What I want is to modify first model model to feed both ObjectListViews in order to avoid redundancy:
Ideally the new model would look like this:
Cells (List)
Number (same as Cell Number)
Status
Lots of properties
Tests (List)
But then it's not clear how to feed it to second ObjectListView.
Question - how to combine these 2 models in order to be able feed OLV to have the result like on screenshots.

Correct me if I'm wrong, but there is no way for the ObjectListView to generate multiple row from a single model object.
You'll need to generate the IENUMERABLE of Tests at one point.
You can let the OLV do this by iterating the list of Cells and adding (olv.addObjects) each ienumerable of tests to the secondary ObjectListView.

Related

How do I correctly join two or more Datamodels into an another Datamodel as a source for GridControl?

My Goal is to join two models, eg. Customers and their orders into a suitable datasource for my Datagrid in a basic WPF App. They are stored as simple observable Collections with no Database behind it. As a connection, Order has a Customer Id as an Int.
As a simple example, the data should look something like this:
Customer Name
Order Id
Product Name
Price
Paid
Some guy
1
Ice cream
0.6ct
ChckBx
Some guy
2
Ice cream
0.6ct
ChckBx
Me
3
Ice cream
1.8ct
ChckBx
The issue at hand is that the Grid can (and should) be edited and every approach I tried doesn't store the data back into the original seperate models OR doesn't let me see all fields OR is immutable
I created a Record Model with neccesary Fields. Problem: Changes coming from the UI only alter the respective Record, but neither the stored Customer nor the Order.
Tried LINQ Join. Customers join Orders... ...select cust. Now we work with only the Original Models and data is written back thanks to Data Binding TwoWay, but I have to choose if I either see Customer Values or Order Values. I want both
Same LINQ Join, but this time with an anon Type. You guessed it, anon types are immutable.
Also tried to manually write back everything everytime CellValueChangedEvent happens but its just painful and requires some hacky moves that definitely weren't intended
Am I missing something very obvious or should I just convert to a proper Database?

Loading a hierarchy with one hit to a database using Linq

We have a table structure like an order table with an order lines table and we define a hierarchy of items in the table according to the user requirements by storing a string made up of a comma separated list of record id’s at each level. This allows the user to “re-order” their lines as needed.
e.g.
A Hierarchy string on the order could be something like “3,2,4” meaning that its top level children are lines 3,2,4 (in that order).
The first order line might have its own hierarchy string such as “5,6” indicating that it has nested children that should appear below it.
We want to return this data structure as a flat list of objects in order with a marker of how “deep” it is in the hierarchy. For example if the data looks like this:
Header:
Lines:
We want a flat list like this (in this order):
{Line1Object}, 1
{Line3Object}, 2
{Line2Object}, 2
{Line4Object}, 2
{Line5Object}, 3
{Line6Object}, 3
Can this be done in one hit with some kind of joining process in Entity Framework instead of looping through our records and building this up as we go or are we being too ambitious?

How to return multiple database values to textboxes?

I currently have a test database with multiple values. In this example below it is data being returned from a SELECT query.
Barcode Product number Pack_size
95587056212 0100-505621 1.000000
100955870562 0100-505621 0.000000
10095587056219 0100-505621 0.000000
10095587556214 0100-505621 12.000000
What I am trying to do is trying to return values with multiple instances to different textboxes. To explain further, let's say I have one textBox and I run a query that says:
SELECT * FROM bdorf where product number = '0100-505621'
It would return the above mentioned data in a SQL environment because there are multiple instances because there are different "Barcodes" and "Pack_Sizes" but as it relates to my Winforms c# application, am not sure how to do this. Typically that query would be ran and I would let it say return a value to a textBox but as we know it would only pull one data as it is only pointing to one textBox. To give a live scenario of what am saying. Let's say I type in the same product number as above which is "0100-505621" as used previously in my example above. Above we saw it had 3 instances but in my application below you would see it returns only one because it is only pointing to one textBox:
What am still figuring out is how would I make it point to multiple textBoxes regardless of the product number entered?
I had in my mind that there are a maximum of 5 possible instances as that is the data in my test database. So I was thinking of starting off with 5 textBoxes and then tying the multiple instances possible to each one and anyone that misses(having let's say 3 instances) would return 0 to those textBoxes. Am not sure if am thinking about this correctly. I welcome suggestions
Why not use a datagrid instead? This would have you filter multiple results. It would be particularly good because I am guessing you don't have a clear idea on how much data will be passed. So create your datagrid. Then add textboxes to edit the values of that datagrid. Because ultimately it is just database values you are really updating.
You could try using a GridView and list all the items from the database and add some textBoxes as filters at the top and search for each respective column you would want to filter on. If you want to edit the data by way of update,insert delete then you could add some check boxes below that GridView which also has buttons labelled: "insert,update, delete" which would have the necessary queries pointing to your database on what action to take. Am not really knowledgeable on GridView but I think that could work to some extent.

Ms Lightswitch - add dynamic (from code behind) items choice list

I am using Microsoft LightSwitch framework, need to add dynamic items in choice list. So far I found different posts saying that choice list is only for static items and you have to use a separate table if you need to populate it in non-static means.
In my case, table will also not be a good option, I want to add items manually from C# code. Actually I need to populate years combobox, for this I need to add last 3 years, current year, and one next year. This I could not achieve through static data or TABLE will also be not a good idea.
just create a lookup table and add a relationship and LS will do the rest
You can always dynamically populate the new table
Although you won't be able to delete records that have children

How to create LookUp fields in DataGridView?

In my DataGridView I'am displaying a buch of columns from one table. In this table I have a column which points to item in another table. As you may already guessed, I want to display in the grid in one column some text value from the second table instead of and ItemID.
I could not find a right example on the net how to do this.
Lets assume that I have two tables in databes:
Table Users:
UserID UserName UserWorkplaceID
1 Martin 1
2 John 1
3 Susannah 2
4 Jack 3
Table Workplaces:
WorkplaceID WorkplaceName
1 "Factory"
2 "Grocery"
3 "Airport"
I have one untyped dataset dsUsers, one binding source bsUsers, and two DataAdapters for filling dataset (daUsers, daWorkplaces).
Code which I am performing:
daUsers.Fill(dsUsers);
daWorkplaces.Fill(dsUsers);
bsUsers.DataSource = dsUsers.Tables[0];
dgvUsers.DataSource = bsUsers;
At this point I see in my dgvUsers three columns, UserID, UserName and UserWorkplaceID. However, instead of UserWorkplaceID and values 1,2,3 I would like to see "Factory", "Grocery" and so on...
So I've added another column to dgvUsers called "WorkplaceName" and in my code I am trying to bind it to the newly created relation:
dsUsers.Relations.Add("UsersWorkplaces", dsUsers.Tables[1].Columns["WorkplaceID"], dsUsers.Tables[0].Columns["UserWorkplaceID"]);
WorkplaceName.DataPropertyName = "UsersWorkplaces.WorkplaceName";
Unfortunately that doesn't work. Relation is created without errors but fields in this column are empty after running the program.
What I am doing wrong?
I would like to also ask about an example with LookUp combobox in DataGridView which allow me to change the UserWorkplaceID but instead of numeric value it will show a tex value which is under WorkplaceName.
Thanks for your time.
In my opinion, the best decision would be to use the DataGridViewComboBoxColumn column type. If you do it, you should create a data adapter with lookup data beforehand and then set DataSource, DataPropertyName, DisplayMember, and ValueMember properties of the DataGridViewComboBoxColumn. You could also set the DisplayStyle property to Nothing to make the column look like a common data column. That's it.
I don't know if you can do exactly what you want, which seems to be binding the DataGridView to two different DataTable instances simulataneously. I don't think the DataGridView class supports that -- or if it does it's a ninja-style move I haven't seen.
Per MSDN, your best bet is probably using the CellFormatting event on the DataGridView and check for when the cell being formatted is in the lookup column, then you could substitute your value from the other table. Use an unbound column for the WorkplaceName column, hide the UserWorkplaceID column and then implement the CellFormatting event handle to look up the value in the row, e.g.:
private void dgv_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
if (dgv.Columns[e.ColumnIndex].Name.Equals("WorkplaceName")
{
// Use helper method to get the string from lookup table
e.Value = GetWorkplaceNameLookupValue(
dataGridViewScanDetails.Rows[e.RowIndex].Cells["UserWorkplaceID"].Value);
}
}
If you've got a lot of rows visible, this might impact performance but is probably a decent way to get it working.
If this doesn't appeal to you, maybe use the DataTable.Merge() method to merge your lookup table into your main table. A quick glance at one of my ADO.NET books suggests this should work, although I have not tried it. But I'm not sure if this is too close to the idea suggested previously which you shot down.
As for your second question about the lookup combobox, you should really post it in a separate question so it gets proper attention.
You could make SQL do the job instead. Use a join to return a table with Workplace names instead of IDs, output that table into a dataset and use it instead.
eg.
SELECT A.UserID, A.UserName, B.WorkplaceID
FROM Users A
JOIN Workplaces B ON A.UserWorkplaceID = B.WorkplaceID
Then use its output to fill dsUsers.

Categories