5

I have a DataTable with columns of different types. What I want is a DataTable that has the same column names but all values are strings. That is, if this is the first:

Name   Age
-----------
John   31
Alice  27
Marge  45

where Name is a String column and Age is an Int32 column, what I want is:

Name   Age
-----------
John   31
Alice  27
Marge  45

where Name and Age are both string columns. The output table must contain the same values as the input table but every value must be converted to a string. Can anyone provide any insight on how one might go about doing this? I thought about maybe doing something like

foreach (DataColumn col in inputTable.Columns)
{
    outputTable.Columns.Add(col.ColumnName, typeof(string));

    foreach (DataRow row in inputTable.Rows)
    {
        ...??
    }
}

Or perhaps there is a better or more efficient approach? Any guidance would be greatly appreciated.

Alex A.
  • 5,466
  • 4
  • 26
  • 56

3 Answers3

11

You can't modify a column type in DataTable if already has records. You can Clone DataTable and then modify column type in each column of cloned table. Later you can import rows.

DataTable dtClone = dt.Clone(); //just copy structure, no data
for (int i = 0; i < dtClone.Columns.Count; i++)
{
    if (dtClone.Columns[i].DataType != typeof(string))
        dtClone.Columns[i].DataType = typeof(string);
}

foreach (DataRow dr in dt.Rows)
{
    dtClone.ImportRow(dr);
}

dtClone will have every column as of string and all the data from original table dt

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks for your response. This this like calling `ToString()` on the values behind the scenes? I thought `ImportRow` preserved the original data types, in which case wouldn't this give a type error? – Alex A. Apr 09 '14 at 18:40
  • @Alex, no, ImportRow just import data and fits it in the available container. I have tried the above code in VS and it works – Habib Apr 09 '14 at 18:42
  • I'm getting that all of the columns are now string type but the `MaxLength` property for each `DataColumn` is -1. Shouldn't that always be >=0 for string columns? – Alex A. Apr 09 '14 at 19:27
  • @Alex, `MaxLength -1` indicates that the column can store any length of string. It is usually used for typed datasets, where you can specify its length to be same as database column length. – Habib Apr 09 '14 at 19:32
  • It doesn't specify the maximum length of current string in the column – Habib Apr 09 '14 at 19:33
  • Thanks for all of your help, @Habib. Your method of getting the `DataTable` works great and I appreciate your clarification of the `MaxLength` property. – Alex A. Apr 09 '14 at 19:34
0

You could use a built in method called toString().

foreach (DataColumn col in inputTable.Columns)
{
outputTable.Columns.Add(col.ColumnName, typeof(string));

foreach (DataRow row in inputTable.Rows)
{
outputTable.Columns.Add(row.toString());
}
}
loadedjd
  • 97
  • 2
  • 6
0

in this code, you can convert whole dataset in to one string.

string ConvertDatasetToString(DataSet Ds)
{
    string OUT = "";
    for (int t = 0; t < Ds.Tables.Count; t++)
    {
        for (int r = 0; r < Ds.Tables[t].Rows.Count; r++)
        {
            for (int c = 0; c < Ds.Tables[t].Columns.Count; c++)
            {
                string s = Ds.Tables[t].Rows[r][c].ToString();
                OUT += s;
            }
        }
    }
    return OUT;
}

if you want to convert one datatable into one string, you can omit the first for

Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40