C# |
using DevExpress.XtraVerticalGrid.Rows;
using DevExpress.XtraVerticalGrid.Events;
private void vGridControl1_ProcessDragRow(object sender, DragRowEventArgs e) {
VGridControl vGrid = (sender as VGridControl);
if (!((vGrid.CustomizationForm != null) && (vGrid.CustomizationForm.PressedRow != null)))
return;
if (!(e.Row is EditorRow)) return;
VGridHitInfo hitInfo = vGrid.CalcHitInfo(vGrid.PointToClient(e.ScreenLocation));
if (!((hitInfo.HitInfoType != HitInfoTypeEnum.CustomizationForm) && (hitInfo.Row != null) &&
(!(hitInfo.Row is CategoryRow)))) {
e.Effect = RowDragEffect.None;
return;
}
PointInfo pInfo = GetPointInfo(hitInfo.Row, e.ScreenLocation);
if (pInfo.IsContained) e.Effect = RowDragEffect.InsertBefore;
else e.Effect = RowDragEffect.None;
}
private void vGridControl1_EndDragRow(object sender, EndDragRowEventArgs e) {
VGridControl vGrid = (sender as VGridControl);
if (!((vGrid.CustomizationForm != null) && (vGrid.CustomizationForm.PressedRow != null)))
return;
if (!(e.Row is EditorRow)) return;
VGridHitInfo hitInfo = vGrid.CalcHitInfo(vGrid.PointToClient(e.ScreenLocation));
if (!((hitInfo.HitInfoType != HitInfoTypeEnum.CustomizationForm) &&
(hitInfo.Row != null) && (!(hitInfo.Row is CategoryRow)))) {
e.Effect = RowDragEffect.None;
return;
}
BaseRow targetRow = hitInfo.Row;
PointInfo pInfo = GetPointInfo(hitInfo.Row, e.ScreenLocation);
if (!(pInfo.IsContained)) {
e.Effect = RowDragEffect.None;
return;
}
e.Effect = RowDragEffect.InsertBefore;
if (e.Row.HasChildren) PreserveChildren(e.Row);
switch (targetRow.XtraRowTypeID){
case 1:
if (targetRow.HasChildren) PreserveChildren(targetRow);
CreateMERow(e.Row, targetRow, pInfo.DroppedBefore);
break;
case 2:
InsertRowItem(e.Row, (targetRow as MultiEditorRow), pInfo.RowItemIndex, pInfo.DroppedBefore);
break;
}
}
public Rectangle CalcHeaderCellRect(BaseRow row, int cellIndex){
VGridControl grid = row.Grid;
Rectangle headerCellRect = (grid.ViewInfo[row].headerInfo.CaptionsInfo[cellIndex]
as RowCaptionInfo).CaptionRect;
if (cellIndex == 0){
int rowIndentsCount = grid.ViewInfo[row].headerInfo.RowIndents.Count;
if (rowIndentsCount != 0) {
int leftPos = grid.ViewInfo[row].headerInfo.RowIndents[rowIndentsCount - 1].Bounds.Left;
headerCellRect.Width += headerCellRect.X - leftPos;
headerCellRect.X = leftPos;
}
}
return headerCellRect;
}
public struct PointInfo {
public bool IsContained;
public int RowItemIndex;
public bool DroppedBefore;
}
public PointInfo GetPointInfo(BaseRow row, Point point){
PointInfo pInfo = new PointInfo();
Point mouseClientPoint = row.Grid.PointToClient(point);
for (int i = 0; i < row.RowPropertiesCount; i++){
Rectangle headerCellRect = CalcHeaderCellRect(row, i);
if (headerCellRect.Contains(mouseClientPoint)) {
pInfo.IsContained = true;
int headerCellMiddle = (headerCellRect.Left + headerCellRect.Right) / 2;
pInfo.DroppedBefore = (mouseClientPoint.X < headerCellMiddle) ? true : false;
pInfo.RowItemIndex = i;
return pInfo;
}
}
pInfo.IsContained = false;
return pInfo;
}
public void PreserveChildren(BaseRow row){
ArrayList rowList = new ArrayList(row.ChildRows);
foreach (BaseRow childRow in rowList){
childRow.Grid.MoveRow(childRow, childRow.Grid.Rows[0],true);
childRow.Visible = false;
}
}
public void CreateMERow (BaseRow source, BaseRow dest, bool droppedFirst){
MultiEditorRow meRow = new MultiEditorRow();
MultiEditorRowProperties sourceRowItem = new MultiEditorRowProperties();
CopyProperties(source.Properties, (sourceRowItem as RowProperties));
MultiEditorRowProperties destRowItem = new MultiEditorRowProperties();
CopyProperties(dest.Properties, (destRowItem as RowProperties));
if (droppedFirst){
meRow.PropertiesCollection.Add(sourceRowItem);
meRow.PropertiesCollection.Add(destRowItem);
}
else {
meRow.PropertiesCollection.Add(destRowItem);
meRow.PropertiesCollection.Add(sourceRowItem);
}
if (dest.ParentRow != null){
int index = dest.ParentRow.ChildRows.IndexOf(dest);
dest.ParentRow.ChildRows.Insert(meRow, index);
}
else {
int index = dest.Grid.Rows.IndexOf(dest);
dest.Grid.Rows.Insert(meRow, index);
}
dest.Dispose();
source.Dispose();
}
public void InsertRowItem (BaseRow source, MultiEditorRow dest, int itemIndex,
bool droppedBefore){
MultiEditorRowProperties newRowItem = new MultiEditorRowProperties();
CopyProperties(source.Properties, (newRowItem as RowProperties));
if (droppedBefore) dest.PropertiesCollection.Insert(itemIndex, newRowItem);
else {
if (itemIndex == dest.PropertiesCollection.Count)
dest.PropertiesCollection.Add(newRowItem);
else
dest.PropertiesCollection.Insert(itemIndex + 1, newRowItem);
}
source.Dispose();
}
public void CopyProperties(RowProperties source, RowProperties dest){
source.AssignTo(dest);
}
|
VB |
Imports DevExpress.XtraVerticalGrid.Rows
Imports DevExpress.XtraVerticalGrid.Events
Private Sub VGridControl1_ProcessDragRow(ByVal sender As Object, _
ByVal e As DragRowEventArgs) Handles VGridControl1.ProcessDragRow
Dim VGrid As VGridControl = CType(sender, VGridControl)
If Not ((Not VGrid.CustomizationForm Is Nothing) And _
(Not VGrid.CustomizationForm.PressedRow Is Nothing)) Then Return
If Not TypeOf (e.Row) Is EditorRow Then Return
Dim hitInfo As VGridHitInfo = VGrid.CalcHitInfo(VGrid.PointToClient(e.ScreenLocation))
If Not ((hitInfo.HitInfoType <> HitInfoTypeEnum.CustomizationForm) And _
(Not hitInfo.Row Is Nothing) And (Not TypeOf (hitInfo.Row) Is CategoryRow)) Then
e.Effect = RowDragEffect.None
Return
End If
Dim PInfo As PointInfo = GetPointInfo(hitInfo.Row, e.ScreenLocation)
If PInfo.IsContained Then
e.Effect = RowDragEffect.InsertBefore
Else : e.Effect = RowDragEffect.None
End If
End Sub
Private Sub VGridControl1_EndDragRow(ByVal sender As Object, _
ByVal e As EndDragRowEventArgs) Handles VGridControl1.EndDragRow
Dim VGrid As VGridControl = CType(sender, VGridControl)
If Not ((Not VGrid.CustomizationForm Is Nothing) And _
(Not VGrid.CustomizationForm.PressedRow Is Nothing)) Then Return
If Not TypeOf (e.Row) Is EditorRow Then Return
Dim hitInfo As VGridHitInfo = VGrid.CalcHitInfo(VGrid.PointToClient(e.ScreenLocation))
If Not ((hitInfo.HitInfoType <> HitInfoTypeEnum.CustomizationForm) And _
(Not hitInfo.Row Is Nothing) And (Not TypeOf (hitInfo.Row) Is CategoryRow)) Then
e.Effect = RowDragEffect.None
Return
End If
Dim TargetRow As BaseRow = hitInfo.Row
Dim PInfo As PointInfo = GetPointInfo(hitInfo.Row, e.ScreenLocation)
If Not PInfo.IsContained Then
e.Effect = RowDragEffect.None
Return
End If
e.Effect = RowDragEffect.InsertBefore
If e.Row.HasChildren Then PreserveChildren(e.Row)
Select Case TargetRow.XtraRowTypeID
Case 1
If TargetRow.HasChildren Then PreserveChildren(TargetRow)
CreateMERow(e.Row, TargetRow, PInfo.DroppedBefore)
Case 2
InsertRowItem(e.Row, CType(TargetRow, MultiEditorRow), _
PInfo.RowItemIndex, PInfo.DroppedBefore)
End Select
End Sub
Public Function CalcHeaderCellRect(ByVal row As BaseRow, _
ByVal cellIndex As Integer) As Rectangle
Dim Grid As VGridControl = row.Grid
Dim HeaderCellRect As Rectangle = _
CType(Grid.ViewInfo(row).headerInfo.CaptionsInfo(cellIndex), RowCaptionInfo).CaptionRect
If cellIndex = 0 Then
Dim RowIndentsCount As Integer = Grid.ViewInfo(row).headerInfo.RowIndents.Count
If RowIndentsCount <> 0 Then
Dim LeftPos As Integer = Grid.ViewInfo(row).headerInfo.RowIndents( _
RowIndentsCount - 1).Bounds.Left
HeaderCellRect.Width = HeaderCellRect.Width + (HeaderCellRect.X - LeftPos)
HeaderCellRect.X = LeftPos
End If
End If
Return HeaderCellRect
End Function
Public Structure PointInfo
Public IsContained As Boolean
Public RowItemIndex As Integer
Public DroppedBefore As Boolean
End Structure
Public Function GetPointInfo(ByVal row As BaseRow, ByVal point As Point) As PointInfo
Dim PInfo As New PointInfo()
Dim MouseClientPoint As Point = row.Grid.PointToClient(point)
Dim I As Integer
For I = 0 To row.RowPropertiesCount - 1
Dim HeaderCellRect As Rectangle = CalcHeaderCellRect(row, I)
If HeaderCellRect.Contains(MouseClientPoint) Then
PInfo.IsContained = True
Dim headerCellMiddle As Integer = (HeaderCellRect.Left + HeaderCellRect.Right) / 2
PInfo.DroppedBefore = IIf(MouseClientPoint.X < headerCellMiddle, True, False)
PInfo.RowItemIndex = I
Return PInfo
End If
Next
PInfo.IsContained = False
Return PInfo
End Function
Public Sub PreserveChildren(ByVal row As BaseRow)
Dim RowList As New ArrayList(row.ChildRows)
Dim ChildRow As BaseRow
For Each ChildRow In RowList
ChildRow.Grid.MoveRow(ChildRow, ChildRow.Grid.Rows(0), True)
ChildRow.Visible = False
Next
End Sub
Public Sub CreateMERow(ByVal source As BaseRow, _
ByVal dest As BaseRow, ByVal droppedFirst As Boolean)
Dim MeRow As New MultiEditorRow()
Dim SourceRowItem As New MultiEditorRowProperties()
CopyProperties(source.Properties, CType(SourceRowItem, RowProperties))
Dim destRowItem As New MultiEditorRowProperties()
CopyProperties(dest.Properties, CType(destRowItem, RowProperties))
If droppedFirst Then
MeRow.PropertiesCollection.Add(SourceRowItem)
MeRow.PropertiesCollection.Add(destRowItem)
Else
MeRow.PropertiesCollection.Add(destRowItem)
MeRow.PropertiesCollection.Add(SourceRowItem)
End If
If Not dest.ParentRow Is Nothing Then
Dim Index As Integer = dest.ParentRow.ChildRows.IndexOf(dest)
dest.ParentRow.ChildRows.Insert(MeRow, Index)
Else
Dim index As Integer = dest.Grid.Rows.IndexOf(dest)
dest.Grid.Rows.Insert(MeRow, Index)
End If
dest.Dispose()
source.Dispose()
End Sub
Public Sub InsertRowItem(ByVal source As BaseRow, _
ByVal dest As MultiEditorRow, ByVal itemIndex As Integer, _
ByVal droppedBefore As Boolean)
Dim NewRowItem As New MultiEditorRowProperties()
CopyProperties(source.Properties, CType(NewRowItem, RowProperties))
If droppedBefore Then
dest.PropertiesCollection.Insert(itemIndex, NewRowItem)
ElseIf itemIndex = dest.PropertiesCollection.Count Then
dest.PropertiesCollection.Add(NewRowItem)
Else
dest.PropertiesCollection.Insert(itemIndex + 1, NewRowItem)
End If
source.Dispose()
End Sub
Private Sub CopyProperties(ByVal source As RowProperties, ByVal dest As RowProperties)
source.AssignTo(dest);
End Sub
|