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

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)