0

With reference to question Modify rows added into dataset, what should i do if i need to include the Dataset 2 rows somewhere between dataset 1 rows?

Say for example;

Dataset 1

Row 1
Row 2
Row 3
Row 4

DataSet 2

Row A
Row B
Row C

hence, this should be like

Row 1
Row A
Row 2
Row 3
Row B
Row 4
Row C

4
  • Rows in a Table are unordered. Try to think of a solution based on Sorting (in a DataView) Aug 2, 2011 at 7:22
  • 1
    if im completely honest; this issue could be resolved by improving your database query. Aug 2, 2011 at 7:23
  • there is no such query. I am using data-table-row-column.
    – xorpower
    Aug 2, 2011 at 7:25
  • What is the criteria to use for determining where to insert row x from table 2 into table 1? The answers below are correct, all that needs to be added is logic for determining where to insert the row.
    – Tim
    Aug 2, 2011 at 8:09

3 Answers 3

2

You can use InsertAt methos

var row = dataSet2.Tables[0].NewRow();
// Copy rowA to row
dt.Rows.InsertAt(row, position);
9
  • i tried that. It throw an error mentioning that "This row already belongs to another table"
    – xorpower
    Aug 2, 2011 at 7:18
  • @Xor power: You should create new Row instance, and then add it by InsertAt
    – Peyman
    Aug 2, 2011 at 7:19
  • Plus, the position is not fixed as to where i would be inserting dataset 2 rows., It is purely based on dataset 1 conditions
    – xorpower
    Aug 2, 2011 at 7:19
  • can i get the row position from the dataset 1?
    – xorpower
    Aug 2, 2011 at 8:55
  • I am not able to find the index of the dataset-1 rows. This is needed so as to i could find where to insert the rows of dataset2. Similar to position in InsertAt argument
    – xorpower
    Aug 2, 2011 at 9:07
1

First of all datatable has rows and dataset holds datatables. To insert row to specific index you can use InsertAt method.

myDataTable.Rows.InsertAt(myNewRow, indexToInsertTo);
3
  • What was the difference between your answer and the one given before ? please dont repeat same answers.
    – Karan Shah
    Aug 2, 2011 at 7:27
  • 2
    @Karan the difference is TIME, when I posted my answer I saw that Peyman also posted it - I was too slow thats it. Just wanted to check (by starting VS) if I'am correct about that method before clicking "Post your Answer".
    – Renatas M.
    Aug 2, 2011 at 7:34
  • ok i thought you just copied it same answer so its of no use thats y i just voted it down sorry for misunderstandings.
    – Karan Shah
    Aug 2, 2011 at 8:48
0

Ok, based on the comments in the answer by Peyman, here is a brute force method, based on the following assumption:

If a given row in table 1 has "Y" in column "A", insert a row from table 2 after the current row in table 1. Take the next unused row from table 2 every time this condition is met.

I will say up front that this is ugly, and prone to a lot of problems, and there's probably a better way to do it (LINQ?), and perhaps an explanation of what Xor is trying to accomplish (i.e., the concept/rules/logic behind it) might lead to better or alternative solutions.

Here goes:

int tbl1Index = 0;
int tbl1Rows = dataset1.Tables[0].Rows.Count;
int tbl2Index = 0;
int tbl2Rows = dataset2.Tables[0].Rows.Count;

DataRow row;

// Do this loop until either tbl1 has been gone through completely or table 2 has been gone 
// through completely, whichever comes first.
while (tbl1Index < tbl1Rows && tbl2Index < tbl2Rows)
{
    if (dataset1.Tables[0].Rows[tbl1Index]["A"].ToString() == "Y")
    {
        // Copy the next available row from table 2
        row = dataset2.Tables[0].Rows[tbl2Index];
        // Insert this row after the current row in table 1
        dataset1.Tables[0].Rows.InsertAt(row, tbl1Index + 1);

        // Increment the tbl1Index.  We increment it here because we added a row, even 
        // though we'll increment tbl1Index again before the next iteration of the while loop
        tbl1Index++;
        // Since we just inserted a row, we need to increase the count of rows in table 1 to
        // avoid premature exit of the while loop
        tbl1Rows++;
        // Increment tbl2Index so the next time a match occurs, the next row will be grabbed.
        tbl2Index++;
    }

    // Increment tbl1Index.  If there was a match above, we'll still need to increment to
    // account for the new row.  
    tbl1Index++;
}

Wow...that's really, really, really ugly....

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.