Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

asked on

Filter a DataTable on a DataList

This returns a DataTable:

list.DataSource = GetMemberEvents(memID);

I have a field "Active".  I only want to display records where "Active" is "True".

Can this be done using a DataList?  thanks
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

DataTable table = GetMemberEvents(memID);
list.DataSource = table.Tables[0].Select("Active = True");
It will depend on what type of field "Active" is though. Is it a string or a Boolean?
Avatar of MikeMCSD

ASKER

thanks Zach,
it's a boolean . ., I tried
list.DataSource = table.Tables[0].Select("Active = 1");
but got an error:
   'System.Data.DataTable' does not contain a definition for 'Tables'

I tried this,  list.DataSource = table.Select("Active = 1");
worked, but it gives an error saying it can't find the field in the DataList:

DataBinding: 'System.Data.DataRow' does not contain a property with the name 'EventID'.

Line 12: <ItemTemplate>&nbsp;
Line 13: <asp:HyperLink ID="HyperLink1" Runat="server"
Line 14: NavigateUrl='<%# "GiftList.aspx?EventID=" + Eval("EventID")  %>'

any ideas?
this works:
   list.DataSource = table;        doesn't give that DataList error.
Is the Select method returning anything? Do this and see if you get any results in the rows variable:

DataRow[] rows = table.Select("Active = 1");
LOL, I tried this before I saw your last post:

DataRow[] matchingRows = table.Select("Active = 1", "EventName DESC");

Yeah, the Select is returning records.
I tried this:
Label.Text = matchingRows;

and the error was:
Cannot implicitly convert type 'System.Data.DataRow[]' to 'string'

So it's some kind of an array . . why the hell do they make this so hard, lol.

ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
gave the error:

 'System.Data.DataRowCollection' does not contain a definition for 'AddRange'

These DataTables are a pain in my a**

Sorry, I'm flying blind since I don't have Visual Studio on this machine. Replace the line:
newTable.Rows.AddRange(rows);

With this:
foreach(DataRow row in rows)
{
     newTable.Rows.Add(row);
}

Open in new window

I'm going to have to come back to this but
I appreciate your help.
Did that not work? I was sure it would... hrm..