none
Re-arranging Dataset rows to our needs. RRS feed

  • Question

  • Hi,

    I have a dataset with a table and the table looks like this, binded with a grid.

    Col 1	Col 2
    	
    A	1
    B	2
    C	3
    D	4
    E	5
    F	6
    
    I wll be re-arranging the col's using buttons in my UI, like 
    Col 1	Col 2
    	
    C	3
    D	4
    F	6
    A	1
    E	5
    B	2
    

    I have  a refresh button which will fetch the new values for those 6 rows. But when I fetch it gives me in A,B,C..F order. How can I re-arrange it as shown in the second table. 

    Thanks,

    Sanjay.

    • Moved by CoolDadTx Friday, January 3, 2014 3:52 PM ADO.NET related
    Thursday, January 2, 2014 5:36 AM

Answers

  • Hi Sanjay

    I think in your case the DisplayIndex column (as suggested in my previous response) is not required.

    Here I've designed a sample code for your reference for re-arranging rows in UI and DataTable.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace Rearrange
    {
        public partial class frmRearrange : Form
        {
            DataTable dt;
    
            public frmRearrange()
            {
                InitializeComponent();
            }
    
            private void frmRearrange_Load(object sender, EventArgs e)
            {
                initializeDataTable();
                populateDGV();
            }
    
            private void initializeDataTable()
            {
                dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                
                dt.Rows.Add("A", "1");
                dt.Rows.Add("B", "2");
                dt.Rows.Add("C", "3");
                dt.Rows.Add("D", "4");
                dt.Rows.Add("E", "5");
            }
    
            private void populateDGV()
            {
                dataGridView1.Rows.Clear();
                foreach (DataRow r in dt.Rows)
                {
                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[Column1.Name].Value = r["Col1"].ToString();
                    dataGridView1.Rows[index].Cells[Column2.Name].Value = r["Col2"].ToString();
                }
            }
    
            private void moveRow(int currentIndex, int newIndex)
            {
                if (newIndex >= 0 && newIndex < dataGridView1.RowCount)
                {
                    string col1 = dataGridView1.Rows[currentIndex].Cells[Column1.Name].Value.ToString();
                    string col2 = dataGridView1.Rows[currentIndex].Cells[Column2.Name].Value.ToString();
    
                    if (newIndex > currentIndex)
                    {
                        while (currentIndex < newIndex)
                        {
                            swapRows(currentIndex, currentIndex + 1);
                            currentIndex++;
                        }
                    }
                    else if (newIndex < currentIndex)
                    {
                        while (currentIndex > newIndex)
                        {
                            swapRows(currentIndex, currentIndex - 1);
                            currentIndex--;
                        }
                    }
                    dataGridView1.Rows[newIndex].Cells[Column1.Name].Value = col1;
                    dataGridView1.Rows[newIndex].Cells[Column2.Name].Value = col2;
    
                    dataGridView1.Rows[newIndex].Cells[Column1.Name].Selected = true;
    
                    dt.Rows[newIndex]["Col1"] = col1;
                    dt.Rows[newIndex]["Col2"] = col2;
                }
            }
    
            private void swapRows(int index1, int index2)
            {
                dataGridView1.Rows[index1].Cells[Column1.Name].Value = dataGridView1.Rows[index2].Cells[Column1.Name].Value;
                dataGridView1.Rows[index1].Cells[Column2.Name].Value = dataGridView1.Rows[index2].Cells[Column2.Name].Value;
    
                dt.Rows[index1]["Col1"] = dt.Rows[index2]["Col1"];
                dt.Rows[index1]["Col2"] = dt.Rows[index2]["Col2"];
            }
    
            private void btUp_Click(object sender, EventArgs e)
            {
                moveRow(dataGridView1.CurrentRow.Index, dataGridView1.CurrentRow.Index - 1);
            }
    
            private void btDown_Click(object sender, EventArgs e)
            {
                moveRow(dataGridView1.CurrentRow.Index, dataGridView1.CurrentRow.Index + 1);
            }
    
            private void btRefresh_Click(object sender, EventArgs e)
            {
                populateDGV();
            }
        }
    }

    I hope this sample code will help you solve your issue.

    Saturday, January 4, 2014 9:18 AM

All replies

  • Hi msanjayv,

    Why do you want to do that? I do not see the second table there a special place, random arrangement?

    Here is a similar question, you can refer to:

    http://stackoverflow.com/questions/9107916/sorting-rows-in-a-data-table

    http://www.codeproject.com/Questions/219205/How-To-Sort-Datatable-in-csharp-net


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 2, 2014 8:18 AM
  • Hi Sanjay

    Since you are rearranging rows only in UI and not in DataSet, the refresh button will re-populate rows in UI in the order they are in DataSet. So you need to re-arrange the rows even in the DataSet.

    You can do this by adding an additional column in the DataSet.DataTable called DisplayIndex, where you can store the Index at which the particular row is displayed.

    If you find any difficulty in implementing the logic then plz let me know.

    Thursday, January 2, 2014 9:01 AM
  • Hi Sanjay,

    You can sort the rows in the dataset in any order, but the 2nd sample you gave doesn't seem to have the data sorted in any order.

    Example:

    objDataTable.DefaultView.Sort = "Col1 desc";
    In above example "Col1" column of datatable is sorted in descending order.


    SRIRAM

    Thursday, January 2, 2014 9:10 AM
  • HI,

        @sriram,

    There is no sorting involved in the second column. the items will be moved in any order as I am using button to navigate. 

      @Jeetendra,

    The UI alone is not changed, Aslo the values in the dataset  is changed. Can u brief ur suggestion.

    Thanks,

    sanjay.

    Friday, January 3, 2014 1:57 PM
  • Hi Sanjay

    I think in your case the DisplayIndex column (as suggested in my previous response) is not required.

    Here I've designed a sample code for your reference for re-arranging rows in UI and DataTable.

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    namespace Rearrange
    {
        public partial class frmRearrange : Form
        {
            DataTable dt;
    
            public frmRearrange()
            {
                InitializeComponent();
            }
    
            private void frmRearrange_Load(object sender, EventArgs e)
            {
                initializeDataTable();
                populateDGV();
            }
    
            private void initializeDataTable()
            {
                dt = new DataTable();
                dt.Columns.Add("Col1");
                dt.Columns.Add("Col2");
                
                dt.Rows.Add("A", "1");
                dt.Rows.Add("B", "2");
                dt.Rows.Add("C", "3");
                dt.Rows.Add("D", "4");
                dt.Rows.Add("E", "5");
            }
    
            private void populateDGV()
            {
                dataGridView1.Rows.Clear();
                foreach (DataRow r in dt.Rows)
                {
                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[Column1.Name].Value = r["Col1"].ToString();
                    dataGridView1.Rows[index].Cells[Column2.Name].Value = r["Col2"].ToString();
                }
            }
    
            private void moveRow(int currentIndex, int newIndex)
            {
                if (newIndex >= 0 && newIndex < dataGridView1.RowCount)
                {
                    string col1 = dataGridView1.Rows[currentIndex].Cells[Column1.Name].Value.ToString();
                    string col2 = dataGridView1.Rows[currentIndex].Cells[Column2.Name].Value.ToString();
    
                    if (newIndex > currentIndex)
                    {
                        while (currentIndex < newIndex)
                        {
                            swapRows(currentIndex, currentIndex + 1);
                            currentIndex++;
                        }
                    }
                    else if (newIndex < currentIndex)
                    {
                        while (currentIndex > newIndex)
                        {
                            swapRows(currentIndex, currentIndex - 1);
                            currentIndex--;
                        }
                    }
                    dataGridView1.Rows[newIndex].Cells[Column1.Name].Value = col1;
                    dataGridView1.Rows[newIndex].Cells[Column2.Name].Value = col2;
    
                    dataGridView1.Rows[newIndex].Cells[Column1.Name].Selected = true;
    
                    dt.Rows[newIndex]["Col1"] = col1;
                    dt.Rows[newIndex]["Col2"] = col2;
                }
            }
    
            private void swapRows(int index1, int index2)
            {
                dataGridView1.Rows[index1].Cells[Column1.Name].Value = dataGridView1.Rows[index2].Cells[Column1.Name].Value;
                dataGridView1.Rows[index1].Cells[Column2.Name].Value = dataGridView1.Rows[index2].Cells[Column2.Name].Value;
    
                dt.Rows[index1]["Col1"] = dt.Rows[index2]["Col1"];
                dt.Rows[index1]["Col2"] = dt.Rows[index2]["Col2"];
            }
    
            private void btUp_Click(object sender, EventArgs e)
            {
                moveRow(dataGridView1.CurrentRow.Index, dataGridView1.CurrentRow.Index - 1);
            }
    
            private void btDown_Click(object sender, EventArgs e)
            {
                moveRow(dataGridView1.CurrentRow.Index, dataGridView1.CurrentRow.Index + 1);
            }
    
            private void btRefresh_Click(object sender, EventArgs e)
            {
                populateDGV();
            }
        }
    }

    I hope this sample code will help you solve your issue.

    Saturday, January 4, 2014 9:18 AM
  • @Jeetendra -- I think you were correct in your first post that a new column, DisplayIndex, is necessary if we assume that Sanjay is refreshing the data from some external source, such as a database (and the new columns would need to be saved to that external source). If he's not, then your example looks like it would work.

    @Sanjay -- if you can post the code you use to rearrange the rows in the UI, we can better suggest how to implement the DisplayIndex idea. Unless you're good with Jeetendra's example above ... which is only workable if all your data is in memory and not being retrieved, saved and refreshed from an external source (database or file).


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, January 4, 2014 4:26 PM
  • Sanjay,

    In fact your question is a presentation question which highly is depending from the UI you use (ASP, Windows Forms, XAML, SharePoint, other Office).

    But you are already wrongly moved from another forum so I will spare you that.

    With a DataSet is the most easiest way if we don't know what UI you are using simply to add a column to the datatable and use a dataview to sort it according that column. (Be aware that column will not be used in an update and you can not use any autocolumn then of your Grid whatever that is)

    http://msdn.microsoft.com/en-us/library/hfx3s9wd(v=vs.110).aspx

    On our website we have also a very old sample how actually do what you ask, it is in VB but as I seen many questions here answered from VB people with C# code that should be no problem in the opposite way.

    http://www.vb-tips.com/DataTableColumns.aspx

     


    Success
    Cor

    Sunday, January 5, 2014 10:17 AM