0
Title| 11/09/2015 | 23/01/2015
----  -------------  ----------------
 A        1             12  
 B        1             13

I want to store the column name as Datarow in DataTable.

Tried :

DataRow firstRow = table.NewRow();
for (var i = 0; i < table.Columns.Count; i++)
{        
     firstRow[i] = header.Rows[0][i].ToString();
}
table.Rows.InsertAt(firstRow, 0);

Error:

Incorrect String 11/09/2015 need double.

Expected Output:

 Title| 11/09/2015 | 23/01/2015
----  -------------  ----------------
Title   11/09/2015   23/01/2015
 A        1             12  
 B        1             13
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
vinodh
  • 714
  • 1
  • 7
  • 20
  • Try for (var i = 0; i < table.Columns.Count - 1; i++) and declare your column as string so it can hold any value – Oscar Dec 11 '15 at 08:57
  • Yes tried , i know , the problem in casting , DataRow doesnot accept column name as string i guess :) – vinodh Dec 11 '15 at 08:58
  • 4
    As your table has columns of different types, you simply cannot do this. What is the need to do this? It sounds senseless. – Yeldar Kurmangaliyev Dec 11 '15 at 08:59
  • 1
    You can use [`DataColumn.ColumnName property`](https://msdn.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx). Check [How do I get column names to print in this C# program?](http://stackoverflow.com/q/2557937/447156) as well. – Soner Gönül Dec 11 '15 at 08:59
  • Can i ask why you want to add the columns to the first `DataRow`? You have the informations already in the table. – Tim Schmelter Dec 11 '15 at 09:00
  • yeah i know @yeldar i dont know how to do that that's why i posted :) – vinodh Dec 11 '15 at 09:02
  • Hey @tim it is the requirement :( – vinodh Dec 11 '15 at 09:02
  • @vinodh: your expected output is not what you describe. I thought you want the column-names in the first row. Can you explain better what you want? Where do you get that error? – Tim Schmelter Dec 11 '15 at 09:03
  • Sorry for Confusion @Tim . I need first row as my columnname .. Thats it. But problem is format , Datarow couldn't understand the dateformat . Even i tried to change to string i unable store the Columnname as row. – vinodh Dec 11 '15 at 09:06
  • First row as your column name? You know that a `DataRow` can have n-field values. So why do you use singular " column name" instead of " column names"? – Tim Schmelter Dec 11 '15 at 09:08
  • 1
    @vinodh: now i understand the problem. The second and third columns are `double`-columns. Of course you can'd add values like `11/09/2015`. How would you want to convert that to double? You etiehr have to tell us how you want toconvert it or use `String` as DataColumn-type – Tim Schmelter Dec 11 '15 at 09:13
  • Exactly @TIm .. String is fine – vinodh Dec 11 '15 at 09:14
  • @vinodh: then go with the third approach in my answer. – Tim Schmelter Dec 11 '15 at 09:26

2 Answers2

1

Try this

DataRow dr = table.NewRow();
foreach (DataColumn dc in table.Columns)
 {

  dr[dc] = dc.ColumnName;

 }
table.Rows.InsertAt(dr, 0);
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
1

Now i understand the problem. The second and third columns are double-columns. Of course you can'd add values like 11/09/2015. How would you want to convert that to double? You either have to tell us how you want to convert it or use String as DataColumn-type.

One option would be to use DateTime.ToOADate() which is the ole automation date:

DataRow firstRow = table.NewRow();
for (var i = 0; i < table.Columns.Count; i++)
{
    string colName = table.Columns[i].ColumnName;
    DateTime dt;
    double d;
    if(DateTime.TryParse(colName, out dt))
        firstRow.SetField(i, dt.ToOADate());
    else if(double.TryParse(colName, out d))
        firstRow.SetField(i, d);
}
table.Rows.InsertAt(firstRow, 0);

Another way would be to check the column-type:

DataRow firstRow = table.NewRow();
for (var i = 0; i < table.Columns.Count; i++)
{
    DataColumn col = table.Columns[i];
    if (col.DataType == typeof(string))
    {
        firstRow.SetField(i, col.ColumnName);
    }
    else if (col.DataType == typeof(DateTime))
    {
        DateTime dt;
        if (DateTime.TryParse(col.ColumnName, out dt))
            firstRow.SetField(i, dt);
    }
    else if (col.DataType == typeof(double))
    {
        double d;
        if (double.TryParse(col.ColumnName, out d))
            firstRow.SetField(i, d);
    }
}
table.Rows.InsertAt(firstRow, 0);

But this natural approach would not solve your issue since you have a DateTime value that has to be converted to a double.


The third and easiest approach would be use use String as column-type.

DataTable table = new DataTable();
table.Columns.Add("Title");
table.Columns.Add("11/09/2015"); // string is default
table.Columns.Add("23/01/2015");
DataRow firstRow = table.NewRow();
for (var i = 0; i < table.Columns.Count; i++)
{
    string colName = table.Columns[i].ColumnName;
    firstRow.SetField(i, colName);
}
table.Rows.InsertAt(firstRow, 0);

That works always.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • i hope it will works, i miss the setField property... :) I ll check – vinodh Dec 11 '15 at 09:28
  • 1
    @vinodh: `SetField` is just another method to assign a value. You could also use firstRow[i] = new value. The difference is that `SetField` also supports nullable types. – Tim Schmelter Dec 11 '15 at 09:31
  • Thanks for your detailed answer.. i can debug now ... final question **Input string was not in a correct format.Couldn't store <1/2/2015> in 1/2/2015 Column. Expected type is Double.** Its giving this error i can debug... but you can easily found the error thats y i posted here. – vinodh Dec 11 '15 at 09:37
  • @vinodh: with what approach? Since expected type is double i think you haven't used the third. How do you create the table and the columns? – Tim Schmelter Dec 11 '15 at 09:39
  • second and third approach – vinodh Dec 11 '15 at 09:40
  • @vinodh: How do you create the table? The last way uses `string` as column-type, so it's not clear how it can expect double. The second approach also checks if the column-type mateches the value, so if it can't be parsed it will use `DBNull.Value` – Tim Schmelter Dec 11 '15 at 09:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97603/discussion-between-vinodh-and-tim-schmelter). – vinodh Dec 11 '15 at 09:43