.netTiers
Help Wanted! If you are using .netTiers and find it as invaluable as we do, please consider giving back to the .netTiers team by helping with our effort to fully document .netTiers. To help, simply create an account and you will then be able to edit this wiki.
Displaying related data in a Windows GridView is similar to ASP.Net, but because of the way the Windows DataGridView works our approach is slightly different.
Just like with the ASP.Net Gridview we have some choices on how we approach the display of the related data in our DataGridView. There are 2 techniques for this. We can use a stored procedure and specifically call the columns we need or we can use the deepload method of our entity.
(Don't forget to add your references as well as the apropriate config files to your windows application.)


1. Custom Stored Procedure
NetTiers allows you to write custom stored procedures which are translated into methods that return a standard DataSet. The stored procedure must be named _Table_Method (Substituting Table with the name of the table where you want to method to appear). In our example we are going to create a stored procedure with our related data for the Northwinds Orders table. Our stored procedure looks like the following:

1CREATE PROCEDURE [dbo].[_Orders_OrderListJoined] 2 3AS 4 5SELECT{BR} 6Orders.[OrderID],{BR} 7Orders.[CustomerID],{BR} 8Customers.CompanyName,{BR} 9Customers.ContactName,{BR} 10Orders.[EmployeeID],{BR} 11Orders.[OrderDate],{BR} 12Orders.[RequiredDate],{BR} 13Orders.[ShippedDate],{BR} 14Orders.[ShipVia],{BR} 15Orders.[Freight],{BR} 16Orders.[ShipName],{BR} 17Orders.[ShipAddress],{BR} 18Orders.[ShipCity],{BR} 19Orders.[ShipRegion],{BR} 20Orders.[ShipPostalCode],{BR} 21Orders.[ShipCountry]{BR} 22FROM [dbo].[Orders]{BR} 23LEFT OUTER JOIN Customers on [dbo].[Orders].CustomerID = Customers.CustomerID{BR} 24 25GO


Notice the name of our stored procedure "_Orders_OrderListJoined", this will create a method under our "OrdersProvider" which we can access by using "DataRepository.OrdersProvider.OrderListJoined()".
Be sure that when you create the procedure that there are no spaces in front of the procedure and that the word "AS" is on its own line. If you get the error "Custom sp_TABLE_METHOD is not in the expected format." it is likely due to the way the stored procedure is formatted. SQL server may allow it, but NetTiers is a bit more particular.
To create the DataRepository.OrdersProvider.OrderListJoined() method regrnerate your code again. This method, like all custom methods, will return a DataSet which can be assigned to a GridView in the normal way.
Next drag a DataGridView to the form:


1// 2// dataGridView1 from the designer file (generated when you drag the GridView to the form) 3// 4this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; 5this.dataGridView1.Location = new System.Drawing.Point(23, 27); 6this.dataGridView1.Name = "dataGridView1"; 7this.dataGridView1.Size = new System.Drawing.Size(611, 294); 8this.dataGridView1.TabIndex = 0;


Right-click and view the code of your form and add your "using" statements:

1using NetTiersDoc.Data; 2using NetTiersDoc.Entities;


Next create a method for binding your DataGridView:

1private void BindDataGridView() 2{ 3 //Create the Dataset from the custom method 4 DataSet dsOrderList = DataRepository.OrdersProvider.OrderListJoined(); 5 6 //Assign the dataset to the gridview 7 dataGridView1.DataSource = dsOrderList.Tables[0]; 8 9}


Call your method during form load and it wil fill your grid:

1protected void Page_Load(object sender, EventArgs e) 2private void DataGridCustomStoredProcedure_Load(object sender, EventArgs e) 3{ 4 BindDataGridView(); 5}



DatagridView populated by our custom stored procedure

DatagridView populated by our custom stored procedure


2. Adding a property to your base class
This method is not nescessarily easier than using a custom stored procedure, but it might be more repeatable. Every entity generated in NetTeirs has a base class file which is only generated once. Using our Northwinds example, our "Orders" entity has 3 files: Orders.cs, IOrders.cs and OrdersBase.generated.cs. The later 2 of the 3 files are rewritten every time you generate code. The first one "Orders.cs" is only written the first time code is generated. We will be using this file to add in some custom code, in this case a property to hold our related record data.

We open the Orders.cs file and add a using statement for System.ComponentModel at the top

1using System.ComponentModel;


this allows us to use the attributes that mark this property as "Bindable". Next we add our property

1[DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)] 2[DataObjectField(false, false, false)] 3public string ContactName 4{ 5 get 6 { 7 if (this.CustomerIdSource != null) 8 { 9 return this.CustomerIdSource.ContactName; 10 } 11 else 12 { 13 return string.Empty; 14 } 15 } 16}


repeat this for any related data columns that you want to include.
Once you've added the properties to the class drag a dataGridView to the form and bind it to your "Orders" object as usual.
First we add our using statements for NetTiers

1using NetTiersDoc.Data; 2using NetTiersDoc.Entities;


Next we add our binding method calling the deepload function to get the related data:

1private void BindDataGridView() 2{ 3 //get the Orders 4 TList<Orders> orderList = DataRepository.OrdersProvider.GetAll(); 5 6 //Deepload our order list 7 DataRepository.OrdersProvider.DeepLoad(orderList,true,DeepLoadType.IncludeChildren,typeof(Customers)); 8 9 //Bind the order list to the datagridview 10 dataGridView1.DataSource = orderList; 11}


The last parameter of the Deepload method "typeof(Customers)" is simply the list of entities you want to explicity deepload. To add more just seperate the list with commas. Don't forget that if your adding a related collection that you'll need to use the collection type like:
1typeof(TList<Customers>)
. There is also a DeepLoadType.ExcludeChildren for times when it might be easier just to exclude the one or two entitites you don't want to deepload.

Now add the BindDataGridView method to your form load code

1private void DataGridAddedProperty_Load(object sender, EventArgs e) 2{ 3 BindDataGridView(); 4}


You'll see your grid loaded and displaying the related data columns.
DatagridView populated with Deepload and additional properties

DatagridView populated with Deepload and additional properties

)))

ScrewTurn Wiki version 2.0.31.