Reputation: 3945
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?
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
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
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
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
Reputation: 2072
Loop through the columns
r.Table.Columns.Item(i)
r.Table.Columns.Item(i).Caption
Upvotes: 0