John
John

Reputation: 3945

Running through a DataRow, can get the column values but not the header?

DataTable currentAttribs = //return dataTable of results;

foreach (DataRow r in currentAttribs.Rows)
 {
    foreach (DataColumn column in r.ItemArray)
       {
         //run through dataRow and access header?????
         {
              tableRow = "<TR><TD>" + column[0].ToString() + "</TD></TR>";
              Literal lc = new Literal();
              lc.Text = tableRow;
              divFeatureInfo.Controls.Add(lc);
          }
       }
  }

Returns all the values in the column, but I can't seem to access the value of column header

I can see the header stepping through but do I need to acces it from the outerloop?

enter image description here

UPDATE

I can view the header title from here - r.Table.Columns.NonPublicMembers.List();..but how do i access each one? Shouldnt it be done inside the r.itemArray and not currentAttribs.rows

Upvotes: 1

Views: 2942

Answers (4)

CANDIMAN
CANDIMAN

Reputation: 139

It's not a perfect answer to your specific question, but it an answer to the question itself of "How can I map up the ColumnHeaders with the DataRow's ItemArray Values.

For me, this is the basic solution.

        private void UpdateButton_Click(object sender, EventArgs e)
        {
            DataRow[] modifiedRows = _dataTable.Select("", "", DataViewRowState.ModifiedCurrent);

            foreach (var row in modifiedRows)
            {
                for (var i = 0; i < row.Table.Columns.Count; i++)
                {
                    var ColumnName = row.Table.Columns[i].ColumnName;
                    var ColumnValue = row.ItemArray[i];
                }

                //... build objects now that we can map the property names and new values...

            }
        }

Upvotes: 0

Nasir Mahmood
Nasir Mahmood

Reputation: 1505

It can be used as

DataTable currentAttribs = //return dataTable of results;    
foreach (DataRow r in currentAttribs.Rows)
 {
    foreach (DataColumn column in currentAttribs.Columns)
       {
         //run through dataRow and access header?????
         {
              tableRow = "<TR><TD>" + column.ColumnName + "</TD></TR>";
              Literal lc = new Literal();
              lc.Text = tableRow;
              divFeatureInfo.Controls.Add(lc);
          }
       }
  }

Upvotes: 0

Yusuf Kapasi
Yusuf Kapasi

Reputation: 587

It can be achieved by using Table property of your DataRow instance.

foreach (DataColumn c in r.Table.Columns)  //loop through the columns. 
{
    MessageBox.Show(c.ColumnName);
}

Upvotes: 0

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

Loop through the columns

r.Table.Columns.Item(i)
r.Table.Columns.Item(i).Caption

Upvotes: 0

Related Questions