Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Bind ListView to DataTable - why is this a problem?

Reply
Thread Tools

Bind ListView to DataTable - why is this a problem?

 
 
Jeremy S
Guest
Posts: n/a
 
      06-09-2009
Why can we NOT fill a ListView by binding it to a DataTable?

When I try this code...

MyListView.DataSource = myDataTable;
MyListView.DataBind();

.... the application chokes with the error:
"The DataSourceID of 'MyListView' must be the ID of a control of type
IDataSource. A control with ID 'ProductsSqlDataSource' could not be found."


Yes, I know google is my friend, and what I was able to find was info
stating that I had to loop through the DataTable and add rows, one at a
time.
example:
http://www.codeguru.com/csharp/cshar...icle.php/c8071


I don't want to use a SqlDataSource - I need the flexibility offered by the
traditional approach I attempted above.

Thanks.


 
Reply With Quote
 
 
 
 
dotNetDave
Guest
Posts: n/a
 
      06-09-2009
Do you have anything in the DataSourceId property? It should be blank if you
are just using your own DataTable.

David


======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''s .NET Coding Standards available at:
http://codingstandards.notlong.com


"Jeremy S" wrote:

> Why can we NOT fill a ListView by binding it to a DataTable?
>
> When I try this code...
>
> MyListView.DataSource = myDataTable;
> MyListView.DataBind();
>
> .... the application chokes with the error:
> "The DataSourceID of 'MyListView' must be the ID of a control of type
> IDataSource. A control with ID 'ProductsSqlDataSource' could not be found."
>
>
> Yes, I know google is my friend, and what I was able to find was info
> stating that I had to loop through the DataTable and add rows, one at a
> time.
> example:
> http://www.codeguru.com/csharp/cshar...icle.php/c8071
>
>
> I don't want to use a SqlDataSource - I need the flexibility offered by the
> traditional approach I attempted above.
>
> Thanks.
>
>
>

 
Reply With Quote
 
 
 
 
Jeremy S
Guest
Posts: n/a
 
      06-09-2009
Good catch Dave - the markup was setting the DataSourceId attribute. I had
copy-n-pasted the markup from an online sample and mistakenly overlooked
this attribute. I removed it and can now bind to a DataTable with no
problem.

Thanks.




"dotNetDave" <> wrote in message
news:0997BDC4-4C3D-4357-9983-...
> Do you have anything in the DataSourceId property? It should be blank if
> you
> are just using your own DataTable.
>
> David
>
>
> ======================================
> David McCarter [Microsoft MVP]
> www.dotNetTips.com
> David McCarter''s .NET Coding Standards available at:
> http://codingstandards.notlong.com
>
>
> "Jeremy S" wrote:
>
>> Why can we NOT fill a ListView by binding it to a DataTable?
>>
>> When I try this code...
>>
>> MyListView.DataSource = myDataTable;
>> MyListView.DataBind();
>>
>> .... the application chokes with the error:
>> "The DataSourceID of 'MyListView' must be the ID of a control of type
>> IDataSource. A control with ID 'ProductsSqlDataSource' could not be
>> found."
>>
>>
>> Yes, I know google is my friend, and what I was able to find was info
>> stating that I had to loop through the DataTable and add rows, one at a
>> time.
>> example:
>> http://www.codeguru.com/csharp/cshar...icle.php/c8071
>>
>>
>> I don't want to use a SqlDataSource - I need the flexibility offered by
>> the
>> traditional approach I attempted above.
>>
>> Thanks.
>>
>>
>>



 
Reply With Quote
 
math
Guest
Posts: n/a
 
      07-14-2009
Hi!

Could you please post your code. i cant make a DataTable returned from a
method populate a ListView, i use

ListView1.DataSource = MyClass.MyDataMethod(); //this returns a DataTable

Thanx in advance!

"Jeremy S" wrote:

> Good catch Dave - the markup was setting the DataSourceId attribute. I had
> copy-n-pasted the markup from an online sample and mistakenly overlooked
> this attribute. I removed it and can now bind to a DataTable with no
> problem.
>
> Thanks.
>
>
>

 
Reply With Quote
 
Jeremy S.
Guest
Posts: n/a
 
      07-15-2009
<< Snip >>

> Could you please post your code. i cant make a DataTable returned from a
> method populate a ListView, i use
>
> ListView1.DataSource = MyClass.MyDataMethod(); //this returns a DataTable
>
> Thanx in advance!




You need to call ListView1.DataBind(). Here is the my code that works. Note
that I have not yet returned to clean it up, added meaningful styling, etc.
This contains remnants from the original sample from which I was working.
But it certainly works.

//-----------------------------------------------------------------------
// in ASPX:
//-----------------------------------------------------------------------

<asp:ListView runat="server" ID="LV_Thumbs" DataKeyNames="PhotoID"
GroupItemCount="3">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblProducts"
style="height:320px">
<tr runat="server" id="groupPlaceholder">
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr runat="server" id="productRow" style="height:80px">
<td runat="server" id="itemPlaceholder">
</td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td id="Td1" valign="top" align="center" style="width:100"
runat="server">
<asp:ImageButton ID="btnImage" runat="server" BorderWidth="1px"
BorderStyle="Solid" BorderColor="#010101"
ImageUrl='<%# Eval("PhotoURL").ToString() %>'
ToolTip='<%# Eval("FileName").ToString() %>'
CommandArgument='<%# Eval("PhotoID").ToString() %>'
OnCommand='PhotoClicked' />
</td>
</ItemTemplate>
</asp:ListView>

//-----------------------------------------------------------------------
// from Code Behind:
//-----------------------------------------------------------------------

private void LoadThumbsList(DataTable thumbsDataTable)
{
LV_Thumbs.DataSource = thumbsDataTable;
LV_Thumbs.DataBind();
}

//-----------------------------------------------------------------------
// this from stored procedure that loads the DataTable:
//-----------------------------------------------------------------------
SELECT @UrlPath + [FileName] AS PhotoURL,
PhotoID,
[FileName],
WidthThumb,
HeightThumb
FROM Photos
WHERE blah blah blah
ORDER BY PhotoID DESC




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
asp.net ListView control - Configure ListView option missing? . ASP .Net 0 07-24-2010 12:20 AM
How can I bind the results of Web service to a ListView? artist ASP .Net 0 07-12-2010 09:25 PM
ListView Bind shapper ASP .Net 0 11-18-2007 12:47 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Adding DataTable Rows To another DataTable J. Babe ASP .Net 1 08-15-2003 05:04 PM



Advertisments