Hello,
I am using RadControls for WinForms 2011 Q3
RadGridView, it is in unbound mode.
I want to reorder the row. Say I drag the row 1 and drop it to row 4.
but it does not work. I guess somehow I must miss something, please help. thanks !
public init()
{
InitializeComponent();
InitializePreviewGrid();
SubscribeForGridEvents(radGridViewPreview);
}
List<PreviewItem> _previewLists = new List<PreviewItem>();
private void InitializePreviewGrid()
{
radGridViewPreview.AutoGenerateColumns = false;
var imageCol = new GridViewImageColumn("DeleteIcon", "DeleteIcon");
imageCol.HeaderText = "";
imageCol.Width = 30;
radGridViewPreview.Columns.Add(imageCol);
var col = new GridViewTextBoxColumn("DisplayStr", "DisplayStr");
col.Width = 350;
radGridViewPreview.Columns.Add(col);
col = new GridViewTextBoxColumn("ValueStr", "ValueStr");
col.IsVisible = false;
radGridViewPreview.Columns.Add(col);
radGridViewPreview.AllowDeleteRow = false;
radGridViewPreview.AllowAddNewRow = false;
radGridViewPreview.AllowEditRow = false;
radGridViewPreview.AutoScroll = true;
radGridViewPreview.AllowAutoSizeColumns = true;
radGridViewPreview.MasterTemplate.AllowRowReorder = true;
radGridViewPreview.ReadOnly = true;
radGridViewPreview.MultiSelect = true;
radGridViewPreview.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridViewPreview.GridBehavior = new CustomGridBehavior();
}
private void SubscribeForGridEvents(RadGridView grid)
{
dragDropService = grid.GridViewElement.GetService<RadDragDropService>();
dragDropService.PreviewDragOver += dragDropService_PreviewDragOver;
dragDropService.PreviewDragDrop += dragDropService_PreviewDragDrop;
dragDropService.PreviewDragHint += dragDropService_PreviewDragHint;
dragDropService.PreviewDropTarget += dragDropService_PreviewDropTarget;
dragDropService.PreviewDragStart += dragDropService_PreviewDragStart;
}
void dragDropService_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
{
//throw new NotImplementedException();
}
void dragDropService_PreviewDropTarget(object sender, PreviewDropTargetEventArgs e)
{
//throw new NotImplementedException();
}
private void dragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)
{
var rowElement = e.DragInstance as GridDataRowElement;
if (rowElement == null)
{
return;
}
var dropTarget = e.HitTarget as RadItem;
var targetGrid = dropTarget.ElementTree.Control as RadGridView;
if (targetGrid == null)
{
return;
}
var dragGrid = rowElement.ElementTree.Control as RadGridView;
if (targetGrid != dragGrid)
{
e.Handled = true;
var behavior = (CustomGridBehavior)dragGrid.GridBehavior;
var dropTargetRow = dropTarget as GridDataRowElement;
int index = dropTargetRow != null ? GetTargetRowIndex(dropTargetRow, e.DropLocation) : targetGrid.RowCount;
MoveRows(targetGrid, dragGrid, behavior.SelectedRows, index);
}
}
private void dragDropService_PreviewDragOver(object sender, RadDragOverEventArgs e)
{
if (e.DragInstance is GridDataRowElement)
{
e.CanDrop = e.HitTarget is GridDataRowElement || e.HitTarget is GridTableElement || e.HitTarget is GridSummaryRowElement;
}
}
private void dragDropService_PreviewDragHint(object sender, PreviewDragHintEventArgs e)
{
GridDataRowElement dataRowElement = e.DragInstance as GridDataRowElement;
if (dataRowElement != null && dataRowElement.ViewTemplate.MasterTemplate.SelectedRows.Count > 1)
{
//e.DragHint = new Bitmap(this.imageList1.Images[6]);
e.UseDefaultHint = false;
}
}
private void MoveRows(RadGridView targetGrid, RadGridView dragGrid, IList<GridViewRowInfo> dragRows, int index)
{
for (int i = dragRows.Count - 1; i >= 0; i--)
{
GridViewRowInfo row = dragRows[i];
if (row is GridViewSummaryRowInfo)
{
continue;
}
GridViewRowInfo newRow = targetGrid.Rows.NewRow();
this.InitializeRow(newRow, row);
targetGrid.Rows.Insert(index, newRow);
row.IsSelected = false;
dragGrid.Rows.Remove(row);
index++;
}
}
private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
{
destRow.Cells["DisplayStr"].Value = sourceRow.Cells["DisplayStr"].Value;
destRow.Cells["ValueStr"].Value = sourceRow.Cells["ValueStr"].Value;
}
private int GetTargetRowIndex(GridDataRowElement row, Point dropLocation)
{
int halfHeight = row.Size.Height / 2;
int index = row.RowInfo.Index;
if (dropLocation.Y > halfHeight)
{
index++;
}
return index;
}
I am using RadControls for WinForms 2011 Q3
RadGridView, it is in unbound mode.
I want to reorder the row. Say I drag the row 1 and drop it to row 4.
but it does not work. I guess somehow I must miss something, please help. thanks !
public init()
{
InitializeComponent();
InitializePreviewGrid();
SubscribeForGridEvents(radGridViewPreview);
}
List<PreviewItem> _previewLists = new List<PreviewItem>();
private void InitializePreviewGrid()
{
radGridViewPreview.AutoGenerateColumns = false;
var imageCol = new GridViewImageColumn("DeleteIcon", "DeleteIcon");
imageCol.HeaderText = "";
imageCol.Width = 30;
radGridViewPreview.Columns.Add(imageCol);
var col = new GridViewTextBoxColumn("DisplayStr", "DisplayStr");
col.Width = 350;
radGridViewPreview.Columns.Add(col);
col = new GridViewTextBoxColumn("ValueStr", "ValueStr");
col.IsVisible = false;
radGridViewPreview.Columns.Add(col);
radGridViewPreview.AllowDeleteRow = false;
radGridViewPreview.AllowAddNewRow = false;
radGridViewPreview.AllowEditRow = false;
radGridViewPreview.AutoScroll = true;
radGridViewPreview.AllowAutoSizeColumns = true;
radGridViewPreview.MasterTemplate.AllowRowReorder = true;
radGridViewPreview.ReadOnly = true;
radGridViewPreview.MultiSelect = true;
radGridViewPreview.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridViewPreview.GridBehavior = new CustomGridBehavior();
}
private void SubscribeForGridEvents(RadGridView grid)
{
dragDropService = grid.GridViewElement.GetService<RadDragDropService>();
dragDropService.PreviewDragOver += dragDropService_PreviewDragOver;
dragDropService.PreviewDragDrop += dragDropService_PreviewDragDrop;
dragDropService.PreviewDragHint += dragDropService_PreviewDragHint;
dragDropService.PreviewDropTarget += dragDropService_PreviewDropTarget;
dragDropService.PreviewDragStart += dragDropService_PreviewDragStart;
}
void dragDropService_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
{
//throw new NotImplementedException();
}
void dragDropService_PreviewDropTarget(object sender, PreviewDropTargetEventArgs e)
{
//throw new NotImplementedException();
}
private void dragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)
{
var rowElement = e.DragInstance as GridDataRowElement;
if (rowElement == null)
{
return;
}
var dropTarget = e.HitTarget as RadItem;
var targetGrid = dropTarget.ElementTree.Control as RadGridView;
if (targetGrid == null)
{
return;
}
var dragGrid = rowElement.ElementTree.Control as RadGridView;
if (targetGrid != dragGrid)
{
e.Handled = true;
var behavior = (CustomGridBehavior)dragGrid.GridBehavior;
var dropTargetRow = dropTarget as GridDataRowElement;
int index = dropTargetRow != null ? GetTargetRowIndex(dropTargetRow, e.DropLocation) : targetGrid.RowCount;
MoveRows(targetGrid, dragGrid, behavior.SelectedRows, index);
}
}
private void dragDropService_PreviewDragOver(object sender, RadDragOverEventArgs e)
{
if (e.DragInstance is GridDataRowElement)
{
e.CanDrop = e.HitTarget is GridDataRowElement || e.HitTarget is GridTableElement || e.HitTarget is GridSummaryRowElement;
}
}
private void dragDropService_PreviewDragHint(object sender, PreviewDragHintEventArgs e)
{
GridDataRowElement dataRowElement = e.DragInstance as GridDataRowElement;
if (dataRowElement != null && dataRowElement.ViewTemplate.MasterTemplate.SelectedRows.Count > 1)
{
//e.DragHint = new Bitmap(this.imageList1.Images[6]);
e.UseDefaultHint = false;
}
}
private void MoveRows(RadGridView targetGrid, RadGridView dragGrid, IList<GridViewRowInfo> dragRows, int index)
{
for (int i = dragRows.Count - 1; i >= 0; i--)
{
GridViewRowInfo row = dragRows[i];
if (row is GridViewSummaryRowInfo)
{
continue;
}
GridViewRowInfo newRow = targetGrid.Rows.NewRow();
this.InitializeRow(newRow, row);
targetGrid.Rows.Insert(index, newRow);
row.IsSelected = false;
dragGrid.Rows.Remove(row);
index++;
}
}
private void InitializeRow(GridViewRowInfo destRow, GridViewRowInfo sourceRow)
{
destRow.Cells["DisplayStr"].Value = sourceRow.Cells["DisplayStr"].Value;
destRow.Cells["ValueStr"].Value = sourceRow.Cells["ValueStr"].Value;
}
private int GetTargetRowIndex(GridDataRowElement row, Point dropLocation)
{
int halfHeight = row.Size.Height / 2;
int index = row.RowInfo.Index;
if (dropLocation.Y > halfHeight)
{
index++;
}
return index;
}