CollectionView for WinForms
現在のレコードの管理
CollectionView の操作 > 現在のレコードの管理

CollectionView manages current record by implementing the ICollectionView interface. It allows you to obtain the current position of a record in the collection using CurrentPosition property of the C1CollectionView class. This class provides various methods to change the current position of an item in a view, which are listed below:

The following GIF displays how the current record management is implemented using MoveCurrentTo method.

record-management

In the following example, we used DataGridView and ComboBox controls wherein item selected from the ComboBox is set as the current item in DataGridView using the MoveCurrentTo method. By default, invoking the MoveCurrentTo method sets specified item in the view as the current item. However, you can select and move the current row to the top of the grid by handling the CurrentChanged event as implemented in the code below:

Private cv As C1CollectionView(Of Customer)

Public Sub New()
    cv = New C1CollectionView(Of Customer)(Customer.GetCustomerList(100))
    gridview.DataSource = New C1CollectionViewBindingList(cv)
    ComboBox1.DisplayMember = "Country"
    ComboBox1.DataSource = New C1CollectionViewBindingList(cv)
    cv.CurrentChanged += cv_CurrentChanged()
End Sub

Private Sub cv_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
    gridview.FirstDisplayedScrollingRowIndex = cv.CurrentPosition
    gridview.ClearSelection()
    gridview.Rows(cv.CurrentPosition).Selected = True
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object,
            e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    cv.MoveCurrentTo(ComboBox1.SelectedItem)
End Sub
C1CollectionView<Customer> cv;
public RecordManagement()
{
    InitializeComponent();

    cv = new C1CollectionView<Customer>(Customer.GetCustomerList(100));
    gridview.DataSource = new C1CollectionViewBindingList(cv);
    //cbCustomer はコンボボックスを指します
    cbCustomer.DisplayMember = "Country";
    cbCustomer.DataSource = new C1CollectionViewBindingList(cv);
    
    cv.CurrentChanged += cv_CurrentChanged;
} 

private void cv_CurrentChanged(object sender, EventArgs e)
{
    gridview.FirstDisplayedScrollingRowIndex = cv.CurrentPosition;
    gridview.ClearSelection();
    gridview.Rows[cv.CurrentPosition].Selected = true;
}

private void cbCustomer_SelectedIndexChanged(object sender, EventArgs e)
{
    cv.MoveCurrentTo(cbCustomer.SelectedItem);
}