This is a migrated thread and some comments may be shown as answers.

Drag & drop functionalities to swap column.

1 Answer 125 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Johny
Top achievements
Rank 2
Johny asked on 13 Jul 2010, 07:09 AM

Hi Guys:

I’m using telerik almost one year…. this is really an excellent tools for rapid development. However I’m facing a little issues and I’m stuck I can’t resolved this. The issues is I have a grid with five column, the first column contain the serial number of each record, and now I want to add drag & drop functionalities to swap column. For example I select the column number 5 and drag and drop it into the column 2 so I the column will swap and reorder the serial number, the column 5 will be column 2 and column will be column 5. How can I achieve this? I need your help / any suggestion.

Regards,

Md. Marufuzzaman

1 Answer, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 16 Jul 2010, 08:32 AM
Hi Johny,

Thank you for writing.

The described behavior is not supported in RadGridView. However, in the new version Q2 2010 we have added a new way to access the drag and drop services in RadGridView that allows you to implement your requirement. Please consider the following code as an example:
RadGridViewDragDropService _ddService;
GridViewColumn _draggedColumn;
private void Form1_Load(object sender, EventArgs e)
{
    _ddService = this.radGridView1.GridViewElement.GetService<RadGridViewDragDropService>();
    _ddService.PreviewDragStart += new EventHandler<Telerik.WinControls.PreviewDragStartEventArgs>(_ddService_PreviewDragStart);
    _ddService.Stopping += new EventHandler<Telerik.WinControls.RadServiceStoppingEventArgs>(ddService_Stopping);
}
void _ddService_PreviewDragStart(object sender, Telerik.WinControls.PreviewDragStartEventArgs e)
{
    GridCellElement draggedCell = e.DragInstance as GridCellElement;
    if (draggedCell != null)
    {
        _draggedColumn = draggedCell.ColumnInfo;
    }            
}
void ddService_Stopping(object sender, Telerik.WinControls.RadServiceStoppingEventArgs e)
{
    GridCellElement dropCell = _ddService.DropTarget as GridCellElement;
    if (_draggedColumn != null && dropCell != null)
    {
        GridViewDataColumn dropColumn = dropCell.ColumnInfo as GridViewDataColumn;
        SwapColumns(_draggedColumn as GridViewDataColumn, dropColumn);
          
    }
    e.Commit = false;
    _draggedColumn = null;
}
private void SwapColumns(GridViewDataColumn col1, GridViewDataColumn col2)
{
    this.radGridView1.BeginUpdate();
    int index1 = this.radGridView1.Columns.IndexOf(col1);
    int index2 = this.radGridView1.Columns.IndexOf(col2);
    this.radGridView1.Columns.Remove(col1);
    this.radGridView1.Columns.Remove(col2);
    this.radGridView1.Columns.Insert(index1, col2);
    this.radGridView1.Columns.Insert(index2, col1);
    this.radGridView1.EndUpdate();
}

Let me know if you have any additional questions.

Best wishes,
Martin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Johny
Top achievements
Rank 2
Answers by
Martin Vasilev
Telerik team
Share this question
or