GridView1.DataSource == null ???

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
 
T

Teemu Keiski

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...
 
T

Teemu Keiski

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top