Bind ListView to DataTable - why is this a problem?

  • Thread starter Thread starter Jeremy S
  • Start date Start date
J

Jeremy S

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/csharp/cs_controls/tutorials/article.php/c8071


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

Thanks.
 
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 said:
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 said:
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/csharp/cs_controls/tutorials/article.php/c8071


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

Thanks.
 
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!
 
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
 
Back
Top