好的我正在改变整个问题。我想生成n * 2行数据表,即如果主数据表包含3行,则新创建的数据表将包含6行(1行用于True选项,1行用于False选项。)
所以我想说我有一个主要/主要表如下
question_text|right_option
-------------|------------
Question 1 | True
-------------|------------
Question 2 | False
-------------|------------
Question 3 | True
所以问题1的正确选项是正确的,问题2&#39s的正确选项是假的等等..
现在基于此表我想创建一个表格如下..
question_text|option_text * | right_option
-------------|--------------|-------------
question 1 | True | True
-------------|--------------|-------------
question 1 | False | True
-------------|--------------|-------------
question 2 | True | False
-------------|--------------|-------------
question 2 | False | False
这里的option_text是硬编码列,其值仅为True和False ..
表(nx2)如下......
DataTable dtTrueFalse = Import_To_Grid(FilePath, Extension, "Yes"); // primary table where only right option exist
for (int i = 0; i < dtTrueFalse.Rows.Count * 2; i++)
{
DataRow dr;
if (i % 2 == 0)
{
dr = dtOptions.NewRow();
dr["Option_Text"] = "True";
}
else
{
dr = dtOptions.NewRow();
dr["Option_Text"] = "False";
}
dtOptions.Rows.Add(dr);
}
创建后我会有这个..
question_text|option_text | right_option
-------------|--------------|-------------
question 1 | True |
-------------|--------------|-------------
question 1 | False |
-------------|--------------|-------------
question 2 | True |
-------------|--------------|-------------
question 2 | False |
现在,我想知道如何在right_option列中插入值?请帮帮我..
答案 0 :(得分:1)
您没有发布数据表的创建...
我认为,还没有任何专栏&#39; right_option&#39; - 所以你需要先使用
添加它dtTrueFalse.Columns.Add("right_option");
之后,您可以像使用&#39; option_text&#39;一样访问每一行中的此列:
dr["right_option"] = "False";
<强> 修改 强>
根据您的评论,以下代码是您真正需要的吗?
// build source datatable
DataTable dtTrueFalse = new DataTable();
dtTrueFalse.Columns.AddRange(new[] { new DataColumn("question_text"), new DataColumn("Right_Option") });
// add dummy data
var dummyRow = dtTrueFalse.NewRow();
dummyRow["question_text"] = "qst 1";
dummyRow["Right_Option"] = "False";
dtTrueFalse.Rows.Add(dummyRow);
// build final datattable
DataTable dtOptions = new DataTable();
dtOptions.Columns.AddRange(new[] {new DataColumn("question_text"), new DataColumn("Right_Option"), new DataColumn("Option_Text")});
// add rows....
foreach (DataRow row in dtTrueFalse.Rows)
{
var newRowTrue = dtOptions.NewRow();
newRowTrue["question_text"] = row["question_text"];
newRowTrue["Right_Option"] = row["Right_Option"];
newRowTrue["Option_Text"] = "True";
dtOptions.Rows.Add(newRowTrue);
var newRowFalse = dtOptions.NewRow();
newRowFalse["question_text"] = row["question_text"];
newRowFalse["Right_Option"] = row["Right_Option"];
newRowFalse["Option_Text"] = "False";
dtOptions.Rows.Add(newRowFalse);
}