GridView1.DataSource == null ???

  • Thread starter Thread starter cmrchs
  • Start date Start date
C

cmrchs

Hello,

I have a GridView and an objectDataSource

<asp:GridView ID="GridView1" runat="server"
DataSourceID="ObjectDataSource1" AllowPaging="True" />

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
TypeName="ProductsLib.Business.ProductsBLO"
SelectMethod="SelectProducts"
SortParameterName="sortExpression"></asp:ObjectDataSource>

When I run the app i see a list of products in the gridView. Ok!

Now, i have a button, when I click on it I'd like to display all the
products whose ProductName starts with the letter 'c'

My implementation in the clickevent handler:

DataTable dataTable = GridView1.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "productname like 'c%'";
dataView.Sort = "productname";
GridView1.DataSource = dataView;

GridView1.DataBind();
}

but my problem is that GridView1.DataSource is null ???

how do i obtain acces to the dataTable?

thank you
Chris
 
Associating data source control to the GridView does not populate DataSource
property at that point. I don't remember does it set the data source or does
it access the result set (by the data source control) directly when
DataBind() is called. BUT In anycase, you can get the result set by calling
data source control's methods e.g ObjectDataSource.Select()
See:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.select.aspx

Note that the result set cannot possibly be a data table because you are
using a custom business object & ObjectDataSource...
 
Unless of course the method returns such, to correct.

Other thing to point out is that GridView does not keep DataSource property
populated over the postbacks (for performance feasons since it would need to
serialize and roundtrip the entire data source)
 
Back
Top