Ron's Sandbox

Data Iteration

Wednesday, 3 June 2009 23:16 by hagermanr

I remember back in the days of asp, looping through data arrays and displaying it on web pages. Short of it is, I want to display a table of data and I don’t need to manipulate the data in any way so I just want to display it.

What I did in asp was, assign the data row to an array and then assign each column to an array then loop through each row and display each column. This worked really well.

Now it is ASP.NET and C# but the concept is close to the same. Instead of an array though, I’m using a dataset. Take a look at this code snippet.

    bool bFirstRow = true;
    string sMessage = "<table border=\"1\">";

    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        sMessage += "<tr>";

        if (bFirstRow)
        {
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                sMessage += "<th>" + dc.ColumnName.ToString() + "</th>";
            }

            bFirstRow = false;
        }
            sMessage += "<tr>";

            foreach (DataColumn dc in ds.Tables["ReturnData"].Columns)
            {
                sMessage += "<td>" + dr[dc.ToString()].ToString() + "</td>";
            }

        sMessage += "</tr>";
    }
    sMessage += "</table>";

    Label1.Text = sMessage;

}

What I’m doing here is, I created a Boolean value to tell me if I am looking at the first row. I need to do this so I can get just the headers using a <th> tag. I may not know the columns in the table so this is the easiest way to handle it.

Next, create my table and then  begin to iterate through the data. For each of our data rows, create the row tag in our table. If it is the first row, get the column names and put them in <th> tags. Set firstrow false then loop through each column and display the data for that column in a <td> tag. Once we have all the columns, close the row. Finally, we close our table and display the data on the webpage with a label.

Be careful not to use an else statement here because if you do, you eliminate the first row. (Yeah, I wasn’t thinking and did that.)

So what can we do with this? What if we want to use our own headers, i.e. instead of a column name like host_name, we want Host Name. Well, we can remove the code to get the headers and place our own header text in it and use the rest of the code as is. This allows us to display the data without having a lot of dr[“column_one”].ToString(), etc. We are, in essence, letting the code define the columns. Of course, if the table is changed, you may find yourself with a column on your webpage that has no header or a header with no data in the column so be careful with that.

It is just that simple.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   C Sharp | General
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading