Home > SOA Tips > .NET Developer > Exploiting DataRelations in a DataGrid
SOA Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

.NET DEVELOPER

Exploiting DataRelations in a DataGrid


Bob Tabor
10.12.2002
Rating: -3.76- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


This tip discusses using a DataGrid to build and employ DataRelations for an application. The Web site contains illustrations for this tip, and you can download the necessary tables to follow along with this tip.


DataSets allow you to maintain related DataTables inside a single data structure. To accomplish this, you can create DataRelations between the tables at runtime in code, or at design time using the XML Schema Designer (I'll demonstrate this technique later in this tip). But let's suppose you have two tables of information, such as an Employee table that has an employee's name, address, etc. and it has a foreign key relationship with the Jobs table that contains the possible positions (president, janitor, etc.) at a given company, like so:

[Illustration #1]

AND you want to create an ASP.NET Web Form that displays information from the Employee table, as well as the position from the EmployeeType table (the actual EmployeeTypeDescription, not the EmployeeTypeID). So, your desired result would look something like this:

[Illustration #2]

To accomplish this, you'll need to:

* Actually, this is just one way to do it...I'll explain why I take this approach later in this tip.

If you would like to see this technique demonstrated in a narrated screen cam video, it is available in the Supporter's Area at LearnVisualStudio.NET in a video called "2415: Binding Related Tables in the DataGrid". Please visit www.LearnVisualStudio.NET for more details.

STEP 0: Creating an ASP.NET Web Forms Project

STEP 1: Create a DataSet

[Illustration #3]

STEP 2: Create a DataRelation

[Illustration #4] [Illustration #5] [Illustration #6]

STEP 3: Add and DataBind a DataGrid to your DataSource

[Illustration #7] [Illustration #8]

Notice that I fill the jobs table first. You must do that because you have a foreign key constr


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Microsoft .NET Web services
How do I balance throughput requirements and interoperability?
APM software traces transactions across tiers, technologies
How you can learn M Grammar for Oslo modeling
Legacy modernization opens Windows for publisher
Former .NET Web developers ride Ruby and Rails application framework
Microsoft Oslo at PDC: Dial 'M' for modeling language
Yahoo proxy fight looms
New Microsoft site for architects
LAMP coders go hybrid route
Silverlight shines on bank RIAs
Microsoft .NET Web services Research

.NET Developer
Programming Indigo
DataSets and Web services don't mix
Security in .NET 2.0
Tracking down managed memory leaks
Handling exceptions in .NET
.NET Compact Framework graphics
The Data Access Application Block
A great .NET resource: .Net2TheMax
Delegates vs. interfaces in .NET
Project structure best practices

Web Services Basics
Gartner Summit round-up
Denmark is first to adopt Universal Business Language
XML-based financial standard gets new support
SOAP test tool adds WS-Security, MIME support
Survey: IT spending back from the dead
WASP Server adds to XML Schema support
IBM, Microsoft on opposite sides of standards fence
WS orchestration 'power struggle' under way, IDC says
Management tools raise privacy alarms
Internet millionaire gives integration a whirl

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
Common Language Infrastructure  (SearchSOA.com)
Visual J#  (SearchSOA.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


aint defined via the relationship you added between the two tables. If you reverse the order of the DataAdapter Fills, then you'll get an error alluding to this fact.

STEP 4: Modify the DataGrid Column that will display the related field.

This is where things get interesting.

First, it's important that the Function must be Public, so that the WebForm can see the new Function! Second, we get a generic object reference, then cast it to a DataRowView. We then use the DataRowView (drv) to get a reference to the current row . . ., which can be referenced as a strongly typed DataRow from the Employee's table. Then, in the last line of code, we return the current DataRow's related Job row, and specifically the Job Description field in that related row. This value is returned to the .aspx file, and is rendered in the grid.

The finished result should look something like this:

[Illustration #9]

You might be wondering if all this effort was worth it. After all, you could have much more easily just created a stored procedure that did an INNER JOIN and returned the results as a single set of data, rather than having two DataTables, a DataRelation and some extra code to manage. But the dividends of this approach pay off in the next tip, which will show how to edit the values in this grid using "in line editing". When we go into edit mode, instead of a standard text box, we'll use a drop down list box containing all of the values from the Jobs DataTable in the list, with the correct item selected from the list of values. Stay tuned.

Download the code for this tip here.

About the Author

[IMAGE]Robert Tabor is a Microsoft Certified Professional in Visual Basic with over six years of experience developing n-tier Microsoft-centric applications for some of the world's most prestigious companies and consulting organizations, such as Ernst & Young, KPMG, Cambridge Technology Partners, Sprint, American Heart Association, and the Mary Kay Corporation. Bob is the author of Microsoft .NET XML Web services by Sams Publishing, and contributes to SoapWebservices.com and LearnVisualStudio.NET. He is currently working on initiatives within Mary Kay, the second largest eCommerce site in retail volume on the net, of how to utilize .NET within their e-business group.


Rate this Tip
To rate tips, you must be a member of SearchSOA.com.
Register now to start rating these tips. Log in if you are already a member.




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SOA Trends and Strategy - SOA Education, SOA Development, SOA Implementations
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2001 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts