ardal
ardal

Reputation: 1617

Filling a DataTable based on row values in a DataSet

I want to show some news posts on the front page of a website. The news have different categories but are stored in the same mysql database. All news items are loaded into a dataset but from there I just want to extract some specific news and show them in a ListView control.

At the moment I'am using the workaround solution of using a foreach loop on the dataset and formatting the result with html in the code behind. It works but I want all html formatting to be done in the aspx file, thus I'am looking for a better solution.

        DataSet articles = db.loadAllArticles();
        foreach (DataRow item in articles.Tables["articles"].Select("category = 1"))
        {
            result += "<h1 class='headline'>" + item["headline"] + "</h1><h2 class='introduction'>" + item["introduction"] + "</h2><p class='content'>" + item["content"] + "</p><p class='authorAndDate'>" + item["author"] + " " + item["datePosted"].ToString().Substring(0,10) + "</p><br/>";                  
        }
        lblDisplay.Text = result;

What I was hoping I could do was simply something like this:

DataSet articles = db.loadAllArticles();
ListView1.DataSource = articles.Tables["articles"].Select("category = 1");
ListView1.DataBind();

But the ListView control is not too happy about trying to bind DataRow to it or something. The best workaround I can think of is to create a new Table within the "articles" DataSet which contains only articles of the chosen category, so something like this:

DataSet articles = db.loadAllArticles();
articles.Tables.Add("frontPageArticles");
articles.Tables["frontPageArticles"] = ???

And then thats where it stops. How can I fill the new datatable with rows from the other datatable where the value of a column is x?

-Eric.

Upvotes: 1

Views: 1671

Answers (2)

Jits
Jits

Reputation: 722

You can design your web form using control like repeater, datalist etc. and can bind these control to your datatable on codebehind.

An example can be found here

Upvotes: -1

Lloyd
Lloyd

Reputation: 2932

You should be looking at binding you ListView to a DataView, it is filterable and sortable.

Upvotes: 2

Related Questions