LightSwitch - Query based on a SelectedItem from another table - c#

I have two tables, Customer and Address. One customer can have one or more addresses.
My view is a ListDetail with all my customers on the left as a list and the edition on the right.
Under the edition I have the "address area" with a list of addresses and an edition on the selected.
My problem is all my addresses are listed. I just want addresses with the matching customerId (selected on the first list).
Here is a drawing to help you see what I am talking about:
I can create a button on the first list that show a popup with the selected Id but I don't know how to put a parameter on my address collection.
Please tell me if you need more details.
Edit : A good example of what I want is the 'Roles' view created by default. I haven't found how to edit this view to see how it works but if you select a 'role' the list of users is updated to show only those that have this role.

If your two tables are related (meaning you've created a relationship between them in the table designer), then what you describe should happen automatically. Using the Add Screen wizard, you can tick the "related data" checkboxes for any related tables that you want to display for the selected item.
If you didn't tick the checkbox for a table, you can still drag the navigation property (created when you added the relationship), which is on the left side of the screen designer (with a + next to it).
To do it manually, you need to create a modeled query (a query based on a table, or on another query), to which you add an integer parameter, then add a filter based on that parameter.

Related

How to display information from joined tables in a DataGridView

I would like to write a software which shows database contents in a Forms application. To make it easier to explain, I'll use a shopping list as an example.
I have a number of lists in one table tLists
I have a number of tProductPackages in another table
A list can consist of many productPackages and one productPackage can be in many lists. This m:n relationship is reflected in a separate table named tListProdPacks.
A productPackage contains a product. A product can have several productPackages. In every tProductPackage table entry, there's a foreign key FK_ProductID which references an entry in a table tProducts.
My forms application should navigate through all the lists using a BindingNavigator. In some bound controls, it should display the details of the selected list. This is working fine. (Just bound controls.)
All the list contents (entries in tListProdPacks for the selected tLists element) should be shown in a DataGridView. This is also working fine. Also the details of the productPackages (tProductPackages items i.e. one hop more to the next table) are displayed correctly. In this table he product value is available as ID (FK_ProductID) (foreign key to tProducts).
Is there a way to show the product name (contained only in tProducts) in the DataGridView whose DataSource is tListProdPacks?
(It's easy to display tProductPackages members, as the primary key from tProductPackages is referenced it tListProdPacks and using this ID I can show all other details from tProductPackages by just keep using the same valueMember but use different DisplayMember values for the single DataGridView columns [using dropdown controls in the DGV].)
But when jumping one table further (i.e. when jumping to the tProducts table), this does not work anymore.
I know that I could just create a custom query (join, view) to get exactly what I want, but then I cannot update the database from the dataset anymore.
Does anyone konw how to accomplish that?
(I guess that there's some easy way provided by the framework and I just don't see it, right?)
I'm not sure if I was able to describe what I would like to accomplish good enough. If not, please help me what to provide in addition.
Best regards,
Tom
u can relate the tables using joins if they contain primary key foreign key relation . using joins u can retrieve a single result set containing all the fields which u might need.

Is two data sources for a DataGridView.ComboBoxColumn possible?

I have a DataGridView with several TextBoxColumns and one ComboBox column called 'combo' that holds the client type. The problem is that I'd like to show both the currently selected client-type value along with the dropdown client-type list to validate future changes by the user. In SQL Server, I have a DB with two table columns, 'client_type_dropdown.name' and 'clients.client_type'. The 'client_type_dropdown.name' column is a validation list. The 'clients.client_type' column contains the current client type for clients in the database. Is there a way to show in 'combo' both 'client_type_dropdown.name' and 'clients.client_type' , i.e., one source for the ComboBoxColumn dropdown and a different source for the textbox part of 'combo'? Or do I need to have two columns in my grid?
I appreciate your help.
I'm using a third party grid, but I usually handle this by setting the combo drop down style to DropDown instead of DropDownList. This will allow your original database value to display, even if it isn't in the list.
This also allows free typing of values into the combo field, so the trick after that is to validate the user input to make sure it matches a value in the list before you allow them to save updated values. You could play around with the LimitToList property of the combo to possibly save you doing the validation manually, but with most controls I have worked with it will give you more grief than help.

Binding gridview from sql table but this binding not for particular table it keeps on changing depending on user input

I have a table called TableExplorer which contains other table's names and their respective column names.
For example: table Customer may have 5 columns but in TableExplorer I may mention only 2 column names out of 5.
User send me the table name in query string, my job is to find that table name whether it is present or not in TableExplorer which I mentioned initially. If the user mentioned table is present then I should bind that table to a gridview and that gridview should have functionality like edit, delete, update. Finally any operation like edit, delete or update made by user should reflect in the respective table as mentioned by the user.
Database used : SQL Server 2008 R2
Programming language : C#, ASP.NET
Thanks in advance for helping.
Are you using WebForms? I'm far from being a WebForms expert but this is what I'd do:
You create different pages with GridView control, one for each table that you might want to bind, eg. Customer table. You create the CRUD operation for this GridView, Insert, Update, Delete. You create parameters for all the columns that the user might need, some of them could be invisible.
The user goes to his selection page, he choses a table and a list of columns and press Submit his request. Whenever you receive the query string you do 2 things:
Identify what table your user is asking and load the right page, if available. Maybe you can load only the specific GridView control in a specific using Ajax. Or maybe you can simple load another page.
Get the list of columns that the user wants to see in his grid from the querystring. Before showing the grid to him you keep these columns visible and hide/remove from the gridview every other column. You must pass this parameter to your insert/update methods as well.
I think this is a good solution to start, assuming you don't have a list of hundreds of tables, I wouldn't try to create something completely dynamic.

How to persist/store values at runtime

I have a scenario which is as follows:-
A form containing 2 grids. The grid on the left contains a list of groups. When a group is selected the grid on the right populates with another list with check boxes.
I would like to be able to select group A and select some random check boxes and then switch to group B and select some other check boxes. However when I select group A again I would like to be able to restore the previously selected check boxes.
This would allow me to preload the settings from the database and also update the changes in one go rather than expecting the user to select apply after the changes for each group.
I'm unsure of the best way to approach this problem. Any feedback is appreciated.
Thanks
Sean.
I've done something like this using DataTables and DataViews to implement client-side filtering.
You have a dataTable with the tickboxes that contains all groups of data.
Then when you click grid 1, you update the DataSOurce of the grid to a new DataView(DataTable,"GroupID=1")
,"",CurrentRows) (i.e. Sets the filter proprety of the DataView to filter on the selected group
THen when you're done, the DataTable has all the tick boxes you just saev the DataTable to the database.
Hope that makes sense.
I'd start with coming up with a database table to store your data - let's say MyTable(UserId, GroupId, ItemId, Selected). When the app starts I'd read the data from the table based on the UserId. I'd process this data into a couple of collections that I could associated with the grids - one having unique groups, the other items associated with a group and the selection status. Set up some event handlers to keep the collections in sync with the user input. Implement some save routine upon hitting a button or form closing and you should be good !

Datagrid usage sourced from SQL server

I would like to know the best way to use a datagrid control linked to a combination (join) of data tables in a way that both simply allows both display, creation of new rows in underlying tables and deletion.
The datagrid appears to offer the latter capabilities but I have not found a way that I am happy with to do more than just display on one grid and offer specific separate edit create and delete facilities.
Suppose for the sake of illustration that the database contains:-
Customer Table
* CustomerID
* CustomerName
Order Table
* CustomerID
* OrderLineItem
* OrderLineQuanity
And that I want to lose the CustomerID for display purposes but would like to be able to create new customers and delete existing ones, perhaps with a confirmatory dialog.
CSharpAtl is correct, use a Master-Detail control. An example of using one in a WinForm app is at http://msdn.microsoft.com/en-us/library/y8c0cxey.aspx.
WinForm DataGrids support add, edit, and delete of both Master and Detail records. As for your question about what happens if you change a Detail record so it matches a new Master; that is not possible. By design a Detail row only contains records that match the Master, you cannot (for example) change an order to belong to a new customer because the Detail row does not contain any customer information.
If you want to move a Detail row to another Master, you have to create a new Detail row for the new Master, copy the data from the old Detail row, and delete the old Detail row. If you're ambitious you could support Cut and Paste or Drag and Drop of Detail rows, but internally you have to Create/Copy/Delete.
If the relationship is 1 to many you can go the route of using Master Detail. [link text][1]
[1]: http://msdn.microsoft.com/en-us/library/aa479344.aspx/"Master Detail"
If I understand your question correctly, you have a query that performs a join of several tables which you display on a single grid. You’d like the user to be able to manipulate that grid and have the underlying tables reflect the changes.
An approach is to solve this problem is to implement stored procedures to perform the CRUD operations. The stored procedures will contain the logic to insert, update and delete records from all of the required tables. Each procedure will need to have a parameter for each bound field on the grid. Set the procedures to be the insert, update and delete commands on your data source.
So imagine if you are adding a new record to the grid. The grid calls the insert command, passes the parameters to the stored procedure. Then within the stored procedure you’ll create the logic to determine if the new line in the grid requires a new customer or if it’s an existing customer then adjust the operation accordingly.

Categories