Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Shuffle DataRow in DataTable

0.00/5 (No votes)
20 May 2009 1  
We all know that we can sort the rows in DataTable by making a simple use of DefaultView. But now if i want a few conditional rows to be shuffled in

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.


We all know that we can sort the rows in DataTable by making a simple use of DefaultView. But now if i want a few conditional rows to be shuffled in the table; i dont have anything inbuilt in .net.
Now if I want to select a few conditional rows in the table and move them to the top of the table.
Let us do it by going through a simple example as shown below:


//We create a test dataTable and its columns

            DataTable dtTestData = new DataTable();
            dtTestData.Columns.Add("id");
            dtTestData.Columns.Add("FirstName");
            dtTestData.Columns.Add("LastName");

//We add rows to the created Test DataTable


            DataRow drw = dtTestData.NewRow();
            drw["id"] = 1;
            drw["FirstName"] = "Steve";
            drw["LastName"] = "Waugh";
            dtTestData.Rows.Add(drw);

            drw = dtTestData.NewRow();
            drw["id"] = 2;
            drw["FirstName"] = "Mark";
            drw["LastName"] = "Waugh";
            dtTestData.Rows.Add(drw);
            dtTestData.AcceptChanges();

            drw = dtTestData.NewRow();
            drw["id"] = 3;
            drw["FirstName"] = "Jhon";
            drw["LastName"] = "Smith";
            dtTestData.Rows.Add(drw);
            dtTestData.AcceptChanges();

            drw = dtTestData.NewRow();
            drw["id"] = 2;
            drw["FirstName"] = "Shane";
            drw["LastName"] = "Warne";
            dtTestData.Rows.Add(drw);
            dtTestData.AcceptChanges();

//Select a group of rows at specified condition (here we have condition id=3)

            DataRow[] selectedRow = dtTestData.Select("id=3");
                         
            if (selectedRow.Length > 0)
            {
                DataRow drNewRow = dtTestData.NewRow();

//Iterate through the Resultant DataRows

                foreach (DataRow rowSelected in selectedRow)
                {

//Assign the resultant row's ItemArray to NewRow's Item Array.
                    drNewRow = dtTestData.NewRow();
                    drNewRow.ItemArray = rowSelected.ItemArray;

//Removing the resultant row from the Table and adding the new row to the Table.
                    dtTestData.Rows.Remove(rowSelected);
                    dtTestData.Rows.InsertAt(drNewRow, 0);               
                }                              
            } 


 Now when you view this dtTestData; this DataTable will contain the selected resultant rows on the top.
Here we simply select a specific rows at a specified condition. Then we take a new DataRow for that particular table. Assign the resultant datarow's itemarray to the newdatarow's item array. Then after we delete the resultant data row from the table and add the New data row at the start index of the table ( i.e. 0th index).

Iterating in this way, moves all the resultant rows at top of the Table.
Similarly we can move any of the conditional resultant row to any row index of the DataTable.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here