0

Ok I am changing entire question. I want to generate n*2 rows datatable i.e if primary datatable contains 3 rows , newly created datatable will contain 6 rows (1 row is for True option and 1 row for False option.)

so lets say I have a primary/main table as follows

question_text|right_option
-------------|------------
Question  1  |  True
-------------|------------
Question  2  |  False   
-------------|------------
Question 3   |  True   

So question 1's right option is true,question 2's right option is False and so on..

Now based on this table I want to create a table as follows..

question_text|option_text * | right_option
-------------|--------------|-------------
question 1   |  True        | True
-------------|--------------|-------------
question 1   | False        |  True
-------------|--------------|-------------
question 2   |  True        |  False
-------------|--------------|-------------
question 2   |  False       |  False     

here option_text is hard coded column whose value will be True and False only..

table (nx2) goes as follows...

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);
    }

after creating I will have this ..

question_text|option_text   | right_option
-------------|--------------|-------------
question 1   |  True        | 
-------------|--------------|-------------
question 1   | False        |  
-------------|--------------|-------------
question 2   |  True        |  
-------------|--------------|-------------
question 2   |  False       |  

Now, I wanna know how to insert value into right_option column?Please help me out..

1 Answer 1

1

You did not post the creation of your data table...

I assume, that there is not yet any column 'right_option' - so you would need to add it at first by using

dtTrueFalse.Columns.Add("right_option");

Afterwards you can access this column in each row like you did with 'option_text':

dr["right_option"] = "False";

EDIT

Based on your comments, is the following code what you really need?

            // 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);
            }
Sign up to request clarification or add additional context in comments.

6 Comments

DataTable dtOptions = new DataTable(); dtOptions.Columns.AddRange(new []{new DataColumn("Right_Option"),new DataColumn("Option_Text")});
I have not posted that because I have already created n x 2 table
I am doing it because I can easily pass entire table to db using tvp.
and with using dr["right_option"] you cannot access the columns? Checked in debugging if the datatable has got the column? Not that you mixed up the datatables
I can acess , but I want algo say 0,1 row of newly created table mapped to 0 th row of primary table, 2,3 row of newly created table mapped to 1 st row of primary table and so on..
|

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.