Datagrid usage sourced from SQL server - c#

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.

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.

Programmatically inherit a table in C# windows form application

Things are graceful in DataGridView but i wanted to learn how i can inherit a table and do re-factor it for each data item fetched from the SQLite database?
I have designed a table(System.Windows.Forms.Table) using the Visual Studio 2013 Designer, pretty much it was a drag-n-drop design.
The table has two column, left most column show's the data fetched from the SQLite database also it has a child table(System.Windows.Forms.Table) which has two row (table row) where each row (table row) has a textField which actually show to different database row (this is the database row) value. The right one has some buttons to trigger few events.
Here is how it looks like: http://i.stack.imgur.com/Q8Lfd.png
Now my question is, in this environment how i can inherit this table design and create multiple instances foreach data rows comes from its SQLite database file?
Demonstration: Here is a quick demonstration of what i wanted to achieve.Assume that the database table called "lists" has four items, so this application would create four instances of this table(Windows form table), insert all the data in appropriate fields in the table(Windows form table) and list them in the "Form1" form, something look like this [ did it on photoshop ;) ]
Please direct me in the right path by giving your idea and references
It's been a while since I worked in WinForms, but the concept is similar in all of the DotNet UI methodologies based upon the question from GER on the original post.
If you define a UserControl that renders the data to look like a single instance of what you have in your photo-shopped item, you can then reuse that control several times.
In your main form, you can now do a loop to add this control as many times as you need programmatically (psuedo code follows at the end).
You will also need a TableLayoutPanel (https://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.tablelayoutpanel%28v=vs.100%29.aspx) or similar for your primary form to serve as the layoutPanel.
foreach(DataTable table in Tables)
{
MyChildControl childControl = new MyChildControl();
childControl.Confiure(table); // Sets the data for the control.
layoutPanel.Controls.Add(childControl); // This call varies by UI method and what layoutPanel you are using
}
Caveat: I can't remember enough about WinForms to remember how to set the grid row/column settings on the control. You get to do some research on that one if this works for you.

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.

WPF Datagrid bound to Linq to SQL DataContext but initialized by data external to database

I have a case in which data external to a database is displayed in a WPF DataGrid. Currently, the data comes in as an ObservableCollection of type T, where each T is a class that models a database table row.
Some of the data may already exist in the database table, but some may be new and will not be stored in the database. The DataGrid provides a way of editing/adding new items to the table and viewing those items that have already been analyzed (they exist in the database).
So, here's my problem - I would like to use Linq to SQL to create a dbml datacontext on the table for CRUD operations. However, the data coming in when the grid is loaded is not from the database. I check the database for items that match what's in the ObservableCollection and highlight them in the grid to indicate that they already exist, and to highlight those items in the ObservableCollection that are not in the database. There is a primary key on the database table.
My first inclination is to bind the DataGrid to the ObservableCollection on the load operation, and then switch the context to the dbml datacontext after the grid is loaded. From that point forward, I would expect the CRUD operations to make their way to the database.
Is this a reasonable approach? I bet there's something better and I would appreciate feedback on how best to tackle this problem. Thanks
Let's split things up for clarity... we have a UI and we have some data in a database. First, we need to create some kind of data object class that contains all of the properties that the users want to edit in the UI.
Then we need a data object that reflects the columns and tables in the database... this can be an object oriented object that contains values from multiple tables, or it can be one or more flat object(s), each mapping one table.
Usually, these two data type objects are the same object, eg. we can usually create one data type class to fulfil both requirements. However, it is perfectly fine to have different objects and copy the data from one to the other in a view model.
Now if you have a data type class that has more properties than there are related columns in the database tables, that's not a problem. It is common to have 'empty properties' after filling these objects from the database... the empty properties can then be filled in a view model with data from anywhere else and displayed along with the database data.
Once you have a data type, or model class, then you can declare an ObservableCollection of that type and once filled, Bind that to the ItemsSource property of any collection control. The main thing is that you fulfil your data and UI requirements; you save the right data in the database; you show the right information in the UI.
Thanks for your response. I understand all that; however, I wish to take advantage of the CRUD functionality obtained from the dbml plumbing. Ideally the DataGrid would be hooked up to the DataContext of the dbml from the start.
So, here's where I'm headed: bring in the external data as a LINQ table having the exact same schema as the dbml database table. Then, do a left outer join, with the incoming table as the first table, against the database table via a linq query on the context and use the results set for initializing the DataGrid ItemsSource. No context changed needed. I'll let you know if that works.
It's much simpler than I had envisioned. Initially I had the misconception that the DataGrid had to be bound to the dbml datacontext in order to take advantage of the CRUD features of the datacontext. Not so. The DataGrid can be bound to the incoming data, which is not in the database, via the ObserveableCollection. Edits, inserts, deletes at the DataGrid can be passed on to a dbml datacontext through the appropriate event handlers.
The item in the datacontext is matched with the edited item in the grid through a primary key on the db table - the incoming ObservableCollection (OC) items are assigned the primary key prior to the binding of the OC to the datagrid.
When this current task of mine is wrapped up, I'll post some sample code (give me a few days).
Bill

LightSwitch - Query based on a SelectedItem from another table

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.

Categories