Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Monday, September 17, 2007

GridView table headers and iterating over a GridView cells in the row and columns.

Here is a function to get the GridView's table header columns:


protected string getTableHeaders()
{
string retVal = "";
int TotalColumns = GridView1.Columns.Count;
int col = 0;
for (col = 0; col < TotalColumns; col++)
{
retVal += GridView1.Columns[col].HeaderText;
if ((col + 1) < TotalColumns) retVal += ", ";
}
return retVal;
}


Here is how to iterate over the GridView cells to extract row and column data into a string. The cells can be custom delimited. Notice how this does not include Column.HeaderText :


protected string getDataCells()
{
string retVal = "";
int TotalColumns = GridView1.Columns.Count;
int TotalRows = GridView1.Rows.Count;
int col = 0;
int row = 0;
if (TotalColumns > 0 && TotalRows > 0)
{
for (row =0; row<TotalRows; row++)
{
for (col = 0; col < TotalColumns; col++)
{ // uses arbitrary ',' between column cells
retVal += GridView1.Rows[row].Cells[col].Text;
if ((col + 1) < TotalColumns) retVal += ", ";
}
// uses arbitrary ';' for end of row
retVal += ";";
}
}
return retVal;
}

4 comments:

Anonymous said...

What entertaining question

Anonymous said...

Hi,

I begin on internet with a directory

Anonymous said...

top [url=http://www.c-online-casino.co.uk/]online casino[/url] hinder the latest [url=http://www.casinolasvegass.com/]casinolasvegass.com[/url] free no set aside reward at the leading [url=http://www.baywatchcasino.com/]casino online
[/url].

Anonymous said...

Iterating GridView cells in C#.NET