ComponentOne Grid for WPF: Grid for WPF Task-Based Help > Reordering Columns Programmatically

Reordering Columns Programmatically

You can programmatically reorder columns in C1DataGrid's actual column collections to change the visual order of columns. In this topic you'll use the Move method to move columns. You'll create an application with two buttons, enabling the user to move the selected column to the right or left. To implement the Move method, you'll create a separate method that takes a shift direction (left or right) as a parameter and call this method from the buttons' event handlers.

Complete the following steps:

1.   Complete steps 1 and 2 of the Grid for WPF Quick Start to create a new WPF project, add a C1DataGrid control to the project, and bind the project to a data source. (Note that you can complete the following with an unbound C1DataGrid control, but you may need to adapt the following steps.)

2.   From the Toolbox add two Button controls and a Label control to the window.

3.   Resize all the controls and, in the Properties window, set the following properties:

      Set Button1's Content property to "<-".

      Set Label1's Content property to "Move the selected column."

      Set Button2's Content property to "->".

The window will look similar to the following:

4.   Double-click Button1 to create the Button1_Click event handler and switch to the code editor.

5.   Add code to the Button1_Click event handler to call the MoveColumn method, which you'll add in the next step:

      Visual Basic

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

    ' Move the selected column to the left.

    Me.MoveColumn(True)

End Sub

      C#

private void Button1_Click(object sender, System.EventArgs e)

{

    // Move the selected column to the left.

    MoveColumn(true);

}

6.   Add the following code above the Button1_Click event to create the MoveColumn method:

      Visual Basic

' Method that takes a shift direction (left or right) as a parameter.

Private Sub MoveColumn(ByVal toTheLeft As Boolean)

    If C1DataGrid1.SelectedItemCellPresenter Is Nothing Then

        Exit Sub

    End If

    Dim currentIdx As Integer = C1DataGrid1.ActualColumns.IndexOf(C1DataGrid1.SelectedItemCellPresenter.ItemCell.Column)

    Dim newIdx As Integer = If(toTheLeft, currentIdx - 1, currentIdx + 1)

    If newIdx >= 0 AndAlso newIdx < C1DataGrid1.ActualColumns.Count Then

        C1DataGrid1.ActualColumns.Move(currentIdx, newIdx)

    End If

End Sub

      C#

// Method that takes a shift direction (left or right) as a parameter.

private void MoveColumn(bool toTheLeft)

{

    if (c1DataGrid1.SelectedItemCellPresenter == null)

        return;

    int currentIdx =

        c1DataGrid1.ActualColumns.IndexOf(c1DataGrid1.SelectedItemCellPresenter.ItemCell.Column);

    int newIdx = toTheLeft ? currentIdx - 1 : currentIdx + 1;

    if (newIdx >= 0 && newIdx < c1DataGrid1.ActualColumns.Count)

        c1DataGrid1.ActualColumns.Move(currentIdx, newIdx);

}

7.   Return to Design view and double-click Button2 to create the Button2_Click event handler.

8.   Add code to the Button2_Click event to call the MoveColumn method:

      Visual Basic

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

    ' Move the selected column to the right.

    Me.MoveColumn(False)

End Sub

      C#

private void Button2_Click(object sender, System.EventArgs e)

{

    // Move the selected column to the right.

    MoveColumn(false);

}

Run your program and observe:

Select a column and click the left column button – the column will move to the left. Click the right column button; the column will move to the right.


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.