GridView DataSource Is Nothing

S

SAL

I am trying to cast my Gridview in my Code behide as a Dataset so I can
manipulate the data. Here is the code snippet:

Dim FilteredDataSet As New System.Data.DataSet

Dim gv As New GridView
gv = CType(tbl_ReportData.FindControl("GridView_1"), GridView)
gv.GridLines = GridLines.Both
gv.DataBind()
FilteredDataSet = DirectCast(gv.DataSource, System.Data.DataSet)


Now the issue I'm running into is the gv.DataSource is Nothing. My
GridView_1 control which I assign my gv to is bound to a <asp:SqlDataSource
ID="SqlDataSource1> on the HTML side and loads GridView_1 fine. Even if I
assign the gv.DataSource as "gv.DataSource =
CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSource", the
DataSource still shows nothing. When I check
CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSourceID the is
shows SqlDataSource1.

Can anyone tell why my GridView_1 gets populated just fine but the
DataSource shows NOTHING, which is preventing me from creating a DataSet from
my GridView? If my syntax is wrong can you show me how to fix it?

Thanks,
 
M

Munna

Hi,

First assign a dataset as a data source of the grid..
then try to cast...

Regards

Munna
 
B

Ben Schwehn

Dim gv As New GridView

I believe this should better be

Dim gv As GridView

gv = CType(tbl_ReportData.FindControl("GridView_1"),
Can anyone tell why my GridView_1 gets populated just fine but the
DataSource shows NOTHING, which is preventing me from creating a DataSet
from my GridView?

Possibly because you're trying to access the DataSource property before it
has been set. Is it still nothing in say Page_PreRender or
GridView_1_OnDataBound?
 
S

SAL

Yes, the PreRender shows it as NOTHING. I Changed the declaration to Dim gv
as GridView as you suggested but it didn't work.
 
S

SAL

The problem is, I can't assign the DataSet to the GridView_1 DataSource
because CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSource
also returns NOTHING. GridView_1 gets rendered because it's DataSourceID is
assigned to <asp:SqlDataSource ID="SqlDataSource1>.
 
S

SAL

I'm not 100% certain of this, but is it possible that when you set the
DataSourceID on your Gridview to a <asp:SqlDataSource> or
<asp:ObjectDataSource> that the your DataSource of your Gridview will return
NOTHING?
 
B

Ben Schwehn

Yes, the PreRender shows it as NOTHING.

Right, sorry.
When you're using a SqlDataSource and bind it via the DataSourceId, the
DataSource property just is not set. Also there isn't even a DataSet
involved, that you could get.

But you can get a *DataView* by calling the Select method on your
SqlDataSource like

TheSqlDataSource.Select(DataSourceSelectArguments.Empty);

As an alternative you could always just use old style databinding w/o
DataSourceId.

I Changed the declaration to
Dim gv as GridView as you suggested but it didn't work.

That was an unrelated problem, your code just created a new GridView
object for no good reason.
 
S

SAL

Thanks for the info Ben it makes sense now. You are right, having "As New
GridView" was overkill but I had it there because I thought maybe I was
missing something which it's clear that I wasn't.

What did you mean by "As an alternative you could always just use old style
databinding w/o DataSourceId"? I am assuming you mean to set the DataSource
of the GridView in the Code behind prior to Rendering the Gridview.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top