Bind ListView to DataTable - why is this a problem?

Discussion in 'ASP .Net' started by Jeremy S, Jun 9, 2009.

  1. Jeremy S

    Jeremy S Guest

    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.
     
    Jeremy S, Jun 9, 2009
    #1
    1. Advertising

  2. Jeremy S

    dotNetDave Guest

    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/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.
    >
    >
    >
     
    dotNetDave, Jun 9, 2009
    #2
    1. Advertising

  3. Jeremy S

    Jeremy S Guest

    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:...
    > 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/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.
    >>
    >>
    >>
     
    Jeremy S, Jun 9, 2009
    #3
  4. Jeremy S

    math Guest

    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.
    >
    >
    >
     
    math, Jul 14, 2009
    #4
  5. Jeremy S

    Jeremy S. Guest

    << 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
     
    Jeremy S., Jul 15, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe
    Replies:
    1
    Views:
    8,425
    Kevin Spencer
    Aug 15, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,213
    Smokey Grindel
    Dec 2, 2006
  3. shapper

    ListView Bind

    shapper, Nov 18, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    353
    shapper
    Nov 18, 2007
  4. artist
    Replies:
    0
    Views:
    722
    artist
    Jul 12, 2010
  5. .
    Replies:
    0
    Views:
    793
Loading...

Share This Page