Click here to Skip to main content
12,248,339 members (46,632 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

39.6K views
7 bookmarked
Posted

A method to move rows within a DataTable

, 11 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
A method to move rows within a DataTable
This would be my first time posting something of this sort. I apologize if it is not in the right (or most appropriate) area.

I created this simple inherited class to add a function for moving a row up or down within a DataTable. Sometimes it is desired to do so, and I've seen a few people online looking for a way to do it.

using System.Data;
 
namespace SNS.Tools
{
    public enum SNSDataTableMoveRow
    {
        Up,
        Down
    }
 
    public class SNSDataTable : System.Data.DataTable
    {
        public SNSDataTable()
            : base()
        {
        }
 
        public SNSDataTable(string tableName)
            : base(tableName)
        {
        }
 
        public SNSDataTable(string tableName, string tableNamespace)
            : base(tableName, tableNamespace)
        {
        }
 
        public SNSDataTable(System.Runtime.Serialization.SerializationInfo info,
            System.Runtime.Serialization.StreamingContext context)
            : base(info, context)
        {
        }
 
        /// <summary>
        /// A custom method used to move a row up or down in a DataTable
        /// </summary>
        /// <param name="row">A DataRow which is a member of this DataTable.</param>
        /// <param name="direction">Up or Down.</param>
        /// <returns>The new row index after the move.</returns>
        public int MoveRow(DataRow row, SNSDataTableMoveRow direction)
        {
            DataRow oldRow = row;
            DataRow newRow = this.NewRow();
 
            newRow.ItemArray = oldRow.ItemArray;
 
            int oldRowIndex = this.Rows.IndexOf(row);
 
            if (direction == SNSDataTableMoveRow.Down)
            {
                int newRowIndex = oldRowIndex + 1;
 
                if (oldRowIndex < (this.Rows.Count))
                {
                    this.Rows.Remove(oldRow);
                    this.Rows.InsertAt(newRow, newRowIndex);
                    return this.Rows.IndexOf(newRow);
                }
            }
 
            if (direction == SNSDataTableMoveRow.Up)
            {
                int newRowIndex = oldRowIndex - 1;
 
                if (oldRowIndex > 0)
                {
                    this.Rows.Remove(oldRow);
                    this.Rows.InsertAt(newRow, newRowIndex);
                    return this.Rows.IndexOf(newRow);
                }
            }
 
            return 0;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

agent154
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionSmaller, more flexible VB solution (based on your code) Pin
Hunter Beanland20-Mar-12 14:45
memberHunter Beanland20-Mar-12 14:45 
GeneralYou (they) must be doing something wrong Pin
PIEBALDconsult11-Jan-12 6:58
memberPIEBALDconsult11-Jan-12 6:58 
QuestionWhat issue(s) are you trying to resolve? Pin
spoodygoon11-Jan-12 4:57
memberspoodygoon11-Jan-12 4:57 
AnswerRe: What issue(s) are you trying to resolve? Pin
agent15412-Jan-12 5:19
memberagent15412-Jan-12 5:19 
GeneralRe: What issue(s) are you trying to resolve? Pin
spoodygoon16-Jan-12 3:33
memberspoodygoon16-Jan-12 3:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 11 Jan 2012
Article Copyright 2012 by agent154
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid