tag:blogger.com,1999:blog-3930375442497799514.post-82172314857094583602008-05-06T09:10:00.002-07:002008-05-06T09:17:57.361-07:002008-05-06T09:17:57.361-07:00Using the ObjectDataSource<div class="itemBodyStyle"> <p>I've become quite fond of the ObjectDataSource control for binding to grids, etc. It's a beautiful thing, the ObjectDataSource control, as long as you're only dealing with one DataTable. If you hook it to a DataSet, it only grabs the first table (d'oh!), and makes a dataview of that. Now my queries may pull back one result set, but may also pull back more for certain reports. So, for a moment I figured I was back to setting the grid's .DataSource property and calling .DataBind()--yuck, thus losing the magical functionality of the ObjectDataSource control...but I found a way around it.</p> <p>First off, my GridView and ObjectDataSource are now in their own .ascx control. In the report page, I call my query. I for-each through every table in the retured DataSet, dynamically loading the user control and setting a property on it.</p><blockquote><pre><span style="font-size:85%;"><span style="font-size:100%;">DataSet ds = (new Report()).GetData(); foreach(DataTable dt in ds.Tables) { controls_reportGrid rptGrid = (controls_reportGrid)LoadControl( "controls/reportGrid.ascx"); rptGrid.Table = dt; phGrids.Controls.Add(rptGrid); }</span> </span></pre></blockquote><p>I created a Table property in the user control hoping to set the ObjectDataSource to a method that returns that table. Oops. The ObjectDataSource only looks in the App_Code folder (or bin folder) for objects. Now, to get around that, I've written the most obscure class that appears to do nothing useful, but it really does. It lives in the App_Code folder.</p> <p>Here it is...</p> <blockquote><pre><span style="font-size:85%;"><span style="font-size:100%;">public class ReportData { public ReportData(){} public DataTable GetData(DataTable dt){return dt;} }</span> </span></pre></blockquote><p>This lets me give my user control a DataTable, then the ObjectDataSource in the control calls ReportData.GetData(_table) in "TheDataSource_Selecting()" which just turns around and gives it right back...crazy, right?</p> <p>I think the tehnical term is "jumping through hoops", but hey, it works.</p></div>Tom Puleohttp://www.blogger.com/profile/10697521511530638404noreply@blogger.com0