DataBinder.Eval prob using DataTable.Select() as datasource

S

Stu Carter

Hi,

ENV: .Net 1.1, VS.Net 2003, ASP.Net

When using the DataBinder.Eval directive in a webform within a Repeater
control, it throws an exception when the datasource is from a
DataTable.Select():

"DataBinder.Eval: 'System.Data.DataRow' does not contain a property
with the name MenuItem."

I am using this directive in the webform:
<%#DataBinder.Eval(Container.DataItem, "MenuItem")%>

It all works fine when the repeater data source is a DataSet table, but when
I set the data source to be generated from a DataTable.Select() (in order to
sort the rows), the exception is thrown.

Now, I can fix it by using this binding method: <%#
((System.Data.DataRow)Container.DataItem)["MenuItem"]%>, but can someone
please explain why this doesn't work when using DataBinder.Eval? Is it a
bug?

Many thanks,
Stu

PS. The binding code that causes the problem is:
ReviewerMenu.DataSource=ReviewerDS.Tables["Menu"].Select("",
"MenuItem");
ReviewerMenu.DataBind();
 
S

Steven Cheng[MSFT]

Hi Stu,

As for the problem when we bind the DataGrid or other controls with
DataRow array rather than the DAtaTable, I 've also done some tests and
here're my suggestions:

When we use DataTable to bind a DataGrid, when the calling the DataBind
method, the ASP.NET will loop through the
DataTable and use a DataRowView to bind with each DataGridRow, it will
expose each column in the DataTable with a strong property rather than the
DataRow which dosn't expose each column as a property. So I think that's
the cause of the problem. You can use
<%# Container.DataItem.GetType().ToString()%>

To print out the DataItem's Type to confirm this.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
G

Guest

Hi, St

Can you try this
DataRow[] drReviewers = ReviewerDS.Tables["Menu"].Select("","MenuItem")
ReviewerMenu.DataSource = drReviewers
ReviewerMenu.DataBind()
If not working, You might try the following because you are sorting only instead of filtering and sorting
DataView dvReviewer = ReviewerDS.Tables["Menu"].DefaultVie
dvReviewer.Sort = "MenuItem
ReviewerMenu.DataSource = dvReviewer
ReviewerMenu.DataBind()
Hope this help

Bin Song, MC
 
S

Stu Carter

Hi Bin,

The first method (array of DataRows) doesn't work (see Steven's last post as
to why), so I will use the DataView method.

Thanks for your help!
Stu

Bin Song said:
Hi, Stu

Can you try this:
DataRow[] drReviewers = ReviewerDS.Tables["Menu"].Select("","MenuItem");
ReviewerMenu.DataSource = drReviewers;
ReviewerMenu.DataBind();
If not working, You might try the following because you are sorting only
instead of filtering and sorting:
DataView dvReviewer = ReviewerDS.Tables["Menu"].DefaultView
dvReviewer.Sort = "MenuItem"
ReviewerMenu.DataSource = dvReviewer;
ReviewerMenu.DataBind();
Hope this helps

Bin Song, MCP
 

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

Top