Home Forums WinForms controls Xceed Grid for WinForms Move row up and down

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #13072 |

    Hi ,
    I have a grid with a set of rows from dataset. I also have a toolbar with 2 buttons up and down. The requirement is to move the grid’s current row when the up button is clicked and move the current highlighted row down on the down button click. Its basically to sequence a set of rows in numerical oder. Is there any easier way to acheive this using the grid.

    Thanks in advance for any help
    Sharad

    Imported from legacy forums. Posted by Sharad (had 3467 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Sharad,

    You can use the grid’s <a href=”http://doc.xceedsoft.com/products/gridNet/ref/xceed.grid.gridcontrol.movecurrentrow.html”>MoveCurrentRow</a&gt; method. This will allow you to move the current row in various directions and grid sections.

    I hope this helps!

    Imported from legacy forums. Posted by Jenny [Xceed] (had 321 views)

    User (Old forums)
    Member
    Post count: 23064

    I’m trying to do something similar to Sharad. I have a grid control with 2 buttons on the form. One is to move the selected row(s) up a position and the other is to move the selected row(s) down a position. I’ve tried using the MoveCurrentRow method, but it’s not moving my selected row at all.

    here’s what i’m doing in the button’s click event:

    <code>
    if (gcEntries.SelectedRows.Count > 0)
    {
    try
    {
    gcEntries.MoveCurrentRow(Xceed.Grid.VerticalDirection.Up);

    }
    catch (Exception ex)
    {
    }
    }
    </code>

    Does anyone have any advice and suggestions on how to accomplish this?

    thank you

    Imported from legacy forums. Posted by Nathan (had 426 views)

    User (Old forums)
    Member
    Post count: 23064

    I found a work around and it involves using a hidden column in order to sort. You will need to add an unbound column to your grid like this:

    <code>
    col = new Xceed.Grid.Column(“SORT_ORDER”, typeof(double));
    col.SortDirection = Xceed.Grid.SortDirection.Ascending;
    col.Visible = false;
    gcEntries.Columns.Add(col);
    </code>

    Now I have 2 buttons on the form…one to move a row up. Here is the button’s click event:

    <code>
    private void btnUp_Click(object sender, System.EventArgs e)
    {
    double dCurr = 0;

    if (gcEntries.SelectedRows.Count > 0)
    {
    try
    {
    gcEntries.BeginInit();

    foreach (Xceed.Grid.DataRow r in gcEntries.SelectedRows)
    {
    dCurr = double.Parse(r.Cells[“SORT_ORDER”].Value.ToString());
    if (dCurr > 0)
    {
    foreach (Xceed.Grid.DataRow row in gcEntries.DataRows)
    {
    if (double.Parse(row.Cells[“SORT_ORDER”].Value.ToString()) == dCurr – 1)
    {
    row.Cells[“SORT_ORDER”].Value = (double)Convert.ToDouble((double)Convert.ToDouble(row.Cells[“SORT_ORDER”].Value) + 1);
    r.Cells[“SORT_ORDER”].Value = (double)Convert.ToDouble((double)Convert.ToDouble(r.Cells[“SORT_ORDER”].Value) – 1);
    }
    }
    }
    }

    gcEntries.EndInit();
    }
    catch (Exception ex)
    {
    }
    }
    }
    </code>

    Also here is the click event for the down button:

    <code>
    private void btnDown_Click(object sender, System.EventArgs e)
    {
    double dCurr = 0;

    if (gcEntries.SelectedRows.Count > 0)
    {
    try
    {
    gcEntries.BeginInit();

    foreach (Xceed.Grid.DataRow r in gcEntries.SelectedRows)
    {
    dCurr = double.Parse(r.Cells[“SORT_ORDER”].Value.ToString());
    if (dCurr < gcEntries.DataRows.Count – 1)
    {
    foreach (Xceed.Grid.DataRow row in gcEntries.DataRows)
    {
    if (double.Parse(row.Cells[“SORT_ORDER”].Value.ToString()) == dCurr + 1)
    {
    row.Cells[“SORT_ORDER”].Value = (double)Convert.ToDouble((double)Convert.ToDouble(row.Cells[“SORT_ORDER”].Value) – 1);
    r.Cells[“SORT_ORDER”].Value = (double)Convert.ToDouble((double)Convert.ToDouble(r.Cells[“SORT_ORDER”].Value) + 1);
    }
    }

    }
    }

    gcEntries.EndInit();

    }
    catch (Exception ex)
    {
    }
    }
    }
    </code>

    I hope this helps for anyone else who needs to accomplish this. I looked all over for a solution and have even emailed support, but have yet to get a response. This will work to simulate moving your rows up and down in a grid by changing the value of the SORT_ORDER column that is an unbound hidden column that I use to hold the row’s index value.

    Nathan

    Imported from legacy forums. Posted by Nathan (had 4250 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.