0

I am building datatable dynamically and the format is like following,

DataTable dt = new DataTable();
DataColumn dc;

dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);

dr["Question"] = "2D";
dr["User1"] = "1";
dr["Question"] = "3D";
dr["User1"] = "4";
dr["Question"] = "2D";
dr["User2"] = "2";
dr["Question"] = "3D";
dr["User2"] = "5";

How can I arrange is it like following,

DataTable dt = new DataTable();
DataColumn dc;

dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);

dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);

dr["Question"] = "2D";
dr["User1"] = "1";
dr["User2"] = "2";
dr["Question"] = "3D";
dr["User1"] = "4";
dr["User2"] = "5";
7
  • 1
    dr means new DataRow()? Commented Mar 28, 2013 at 10:35
  • Execuse me ..., I cannot see what's the difference .. Commented Mar 28, 2013 at 10:38
  • Can you please explain in details what type of arrangement are you looking for ? Commented Mar 28, 2013 at 10:43
  • Yes dr is new datarow. @KenKin, Difference is there when I want to use dt as MSChart datasource. Commented Mar 28, 2013 at 10:44
  • But something like dr["Question"] = "2D"; then dr["Question"] = "3D"; just resulting dr["Question"] = "3D"; after all. Commented Mar 28, 2013 at 10:46

3 Answers 3

1

You can make linq cross join and filter it to list (you can bind list to your msChart)

var result = (from dr1 in dt.Select()
                  join dr2 in dt.Select() on dr1["Question"].ToString() equals dr2["Question"].ToString()
                  select new { q = dr1["question"], u1 = dr1["User1"], u2 = dr2["User2"] }
                 ).Where(row => row.u1.ToString().Length > 0 && row.u2.ToString().Length > 0).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

This looks interesting. Do you mean to use this linq code to filter entire data table and store the new result in "result" and use result as data source for MSChart ?
1

You could use code similar to this;

DataRow dr = dt.NewRow();
dr["Question"] = "2D";
dr["User1"] = "1";
dr["User2"] = "2";

dr = dt.NewRow();
dr["Question"] = "3D";
dr["User1"] = "4";
dr["User2"] = "5";

1 Comment

I am building datatable from multiple sources dynamically so it's coming in the format I've mentioned above but that datatable is not working for MSChart so wanted in new format after creation.
1

Supposing that dr is a variable of type DataRow obtained calling the DataTable method NewRow, then you can add the values to the row ItemArray property in just one line.
Of course you need to be absolutely sure of your columns order and type

    DataRow dr = dt.NewRow();
    dr.ItemArray = new object[] {"Q1", "1", "2"};
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr.ItemArray = new object[] {"Q2", "3", "4"};
    dt.Rows.Add(dr);

Comments

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.