I´m trying to set the first row of a DataTable to be the column names. I´m using a GenericParsing library from Code Project. The problem is that the parser sets a default name for the columns.
Thanks.
use Datatable. rows(0) this will give you the first row in the datatable.
A table header is a row at the top of a table used to label each column.
I think you need the following:
foreach (DataColumn column in table.Columns)
{
string cName = table.Rows[0][column.ColumnName].ToString();
if (!table.Columns.Contains(cName) && cName != "")
{
column.ColumnName = cName;
}
}
table.Rows[0].Delete(); //If you don't need that row any more
This should do what you want:
DataRow firstRow = table.NewRow();
List<string> names = new List<string>();
foreach (DataColumn column in table.Columns)
{
names.Add(column.ColumnName);
}
firstRow.ItemArray = names.ToArray();
table.Rows.InsertAt(firstRow, 0);
If the first row already exists and you want to "overwrite" this change the first line to:
DataRow firstRow = table.Rows[0];
And remove the last line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With