0

i have C# DataTable in format as below

Name    Value1  Value2  Value3
A          5       5    0
B          6       3    1
C          4       9    9

Expected format in string should be as

string sDataTableOutput = "A,B,C|Value1,5,6,4|Value2,5,3,9|Value3,0,1,9";

I want Column Header with data (except for first column)

i try hard to write below code but couldnt get it done

DataTable dtGraphOutput[0] = //Get DataTable Data ; 

foreach (DataColumn dcOutput in dtGraphOutput[0].Columns)
{
    foreach (DataRow drOutput in dtGraphOutput[0].Rows)
    {                                        
        sTicketTypeCount += ((sTicketTypeCount == "") ? "" : ",") + dcOutput.ColumnName + Convert.ToString(drOutput[dcOutput]);
    }
    sTicketTypeCount += ((sTicketTypeCount == "") ? "" : "|");
}

Its giving me output as

NameA,NameB,NameC|,Value15,Value16,Value14|,Value25,Value23,Value29|,Value30,Value31,Value39|

3 Answers 3

1

This is LINQ and gives you the desired result:

var rows = dt.AsEnumerable();
var firstCol = string.Join(",", rows.Select(r => r.Field<string>(0)));
var otherColumns = dt.Columns.Cast<DataColumn>().Skip(1)
    .Select(dc => string.Format("{0},{1}", 
        dc.ColumnName, 
        string.Join(",", rows.Select(r => r.Field<int>(dc)))));
string sDataTableOutput = string.Format("{0}|{1}", 
    firstCol, 
    string.Join("|", otherColumns));

Result: A,B,C|Value1,5,6,4|Value2,5,3,9|Value3,0,1,9

Sign up to request clarification or add additional context in comments.

7 Comments

error The best overloaded method match for 'string.Join(string, string[])' has some invalid arguments and cannot convert from 'System.Data.EnumerableRowCollection<string>' to 'string[]'
@SagarDumbre: Are you're using .NET 3.5? Then you cannot use String.Join with an IEnumerable<T> and you have to use the one that takes an array. For example: string.Join("|", otherColumns.ToArray()));. The same on the other string.Joins.
Which to use then i have option 3.5 or 4 or 4.5
@SagarDumbre: You need at least .NET 4 if you want to use the overload with an IEnumerable<T> which is more efficient. Then the above code compiles and works directly without any modification.
I have changed it to 4 Now it gives me error in runtime Unable to cast object of type 'System.Int32' to type 'System.String'.
|
1
string sDataTableOutput = string.Join("|", 
                           dtGraphOutput[0].Columns
                                           .OfType<DataColumn>()
                                           .Select((c,i) =>
                                              string.Join(",",
                                              dtGraphOutput[0].Rows
                                                              .OfType<DataRow>()
                                                              .Select((r,j)=>
                                                          (j == 0 && i != 0 ? c.ColumnName + "," : "") + r[c.ColumnName])
                                                              .ToArray())).ToArray());
                                                                             .

4 Comments

why does it give me multiple error some of it are 'System.Data.DataColumn' does not contain a definition for 'Field' and the best extension method overload 'System.Data.DataRowExtensions.Field<T>(System.Data.DataRow, string)' has some invalid arguments
@SagarDumbre sorry, I typed the c instead of r, just updated.
error Unable to cast object of type 'System.Int32' to type 'System.String'
@SagarDumbre that's because your column values are Int32, just edited.
0

Try this:

DataTable dtGraphOutput[0] = //Get DataTable Data ; 

foreach (DataColumn dcOutput in dtGraphOutput[0].Columns)
{
    sTicketTypeCount +=dcOutput.ColumnName
    foreach (DataRow drOutput in dtGraphOutput[0].Rows)
    {                                        
        sTicketTypeCount += ","+ dcOutput.ColumnName + Convert.ToString(drOutput[dcOutput]);
    }
    sTicketTypeCount += ((sTicketTypeCount == "") ? "" : "|");
}

if you have problem with the last "|" you can remove it with a simple if

3 Comments

it doesnt give desired output i want final string as A,B,C|Value1,5,6,4|Value2,5,3,9|Value3,0,1,9
slight change of what i wrote in question i.e. NameA,NameB,NameC|,Value15,Value16,Value14|,Value25,Value23,Value29|,Value30,Value31,Value39|
if you mean that it gives you "NameA" instead of "A". you can change the name of column to "A". if not give the result of my codes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.