RadControls for WinForms

Checkboxes

RadListViews' items have built-in checkboxes which can be shown by setting the ShowCheckBoxes property of RadListView to true.

When checkboxes are enabled, you have several options to handle the checked items:

  • You can check or uncheck items via the checkbox that is shown in each item

  • You can get or set the checked state of the ListViewDataItem directly via its CheckedState property

  • You can get the checked items in RadListView via its CheckedItems collection

  • You can listen for changes in the check state of the items via the ItemCheckedChanging and ItemCheckedChanged events.

Editors

By default RadListView allows editing of items. If the AllowEditing property is set to true, the user may select a item and press F2 key to initiate editing. By default a text editor is invoked and allows the editing of the items' label. When the edit process ends the entered value is assigned to the items' Value property. If the user cancels editing by pressing Escape key the value is not persisted. Editing can also be initiated and canceled programmatically.

  • Use the BeginEdit() method to initiate editing on the selected item

  • Use the EndEdit() method to end editing and save the edited value.

  • Use the CancelEdit() method to end editing and discard the edited value.

The sample code below shows how to start editing using the API:

Copy[C#] Start editing
radListView1.AllowEdit = true;
// set the SelectedItem - this item will be edited  
// in DetailsView you might also want to set the CurrentColumn property – the value of the selected item in this column will be edited in DetailsView
radListView1.SelectedItem = radListView1.Items[0];
radListView1.CurrentColumn = radListView1.Columns[0];
// this will start edit on selected item
radListView1.BeginEdit();
Copy[VB.NET] Start editing
RadListView1.AllowEdit = True
' set the SelectedItem - this node will be edited  
' in DetailsView you might also want to set the CurrentColumn property – the value of the selected item in this column will be edited in DetailsView
RadListView1.SelectedItem = RadListView1.Items(0)
RadListView1.CurrentColumn = RadListView1.Columns(0)
' this will start edit on selected item
RadListView1.BeginEdit()

Editing lifecycle

When an item is displayed in RadListView and the user presses F2 key to enter edit mode, the following steps are performed:

  • The BeginEdit() method is called internally

  • The ItemEditing event is fired. This event is cancelable and you can prevent the edit operation from continuing by setting the Cancel property from the arguments to false.

  • .A text box editor appears in the selected item.

When an item is brought out of edit mode, the following steps are performed:

  • The editor determines if it wants to handle the keystroke - for example Esc - cancels editing, Enter ends editing and submits changes.

  • The editor instance performs the action it has defined for the Enter key. Typically this indicates that edit mode should be exited and any changes made during the edit session should be saved.

  • In response to the action described in the previous step the EndEdit() method is called internally and the ItemValidating event is fired.

  • The ValueValidating event allows the user to hook up custom logic for verification. If the ValueValidating event does not succeed (e.Cancel is true), ValidationError event is fired to notify all listeners that the validation has failed.

  • Follows the ItemValueChanging event via which you can cancel assigning a new value to the item.

  • If the previous event was not canceled, the new value is assigned to the item and the ItemValueChanged event is fired.

The Following example demonstrates the usage of ItemValidating event to edit integer values:

Copy[C#] Validation
void radListView1_ItemValidating(object sender, ListViewItemValidatingEventArgs e)
{
    int newInt = 0;
    if (int.TryParse(Convert.ToString(e.NewValue), out newInt))
    {
        e.NewValue = newInt;
    }
    else
    {
        e.Cancel = true;
    }
}

void radListView1_ValidationError(object sender, EventArgs e)
{
    MessageBox.Show("Invalid Value");
}
Copy[VB.NET] Validation
Private Sub radListView1_ItemValidating(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.ListViewItemValidatingEventArgs) Handles RadListView1.ItemValidating
    Dim newInt As Integer = 0
    If Integer.TryParse(Convert.ToString(e.NewValue), newInt) Then
        e.NewValue = newInt
    Else
        e.Cancel = True
    End If
End Sub

Private Sub radListView1_ValidationError(ByVal sender As Object, ByVal e As EventArgs) Handles RadListView1.ValidationError
    MessageBox.Show("Invalid Value")
End Sub

Switching Editors

When edit operation is about to begin, the EditorRequired event is fired. By using this event, you can replace the default text box editor with one of the four built-in editors that RadListView provides: ListViewTextBoxEditor; ListViewDropDownListEditor; ListViewSpinEditor; ListViewDateTimeEditor. You can also provide a custom instance as an editor.

The following example shows how you can use the predefined editors:

Copy[C#] Start editing
void radListView1_EditorRequired(object sender, Telerik.WinControls.UI.ListViewItemEditorRequiredEventArgs e)
{
    if (e.ListViewElement.CurrentColumn.FieldName == "CustomerName")
    {
        e.EditorType = typeof(ListViewTextBoxEditor);
    }
    else if (e.ListViewElement.CurrentColumn.FieldName == "ProductName")
    {
        ListViewDropDownListEditor editor = new ListViewDropDownListEditor();
        (editor.EditorElement as BaseDropDownListEditorElement).Items.Add("Product1");
        (editor.EditorElement as BaseDropDownListEditorElement).Items.Add("Product2");
        (editor.EditorElement as BaseDropDownListEditorElement).Items.Add("Product3");

        e.Editor = editor;
    }
    else if (e.ListViewElement.CurrentColumn.FieldName == "Quantity")
    {
        e.EditorType = typeof(ListViewSpinEditor);
    }
    else if (e.ListViewElement.CurrentColumn.FieldName == "OrderDate")
    {
        e.EditorType = typeof(ListViewDateTimeEditor);
    }
}
Copy[VB.NET] Start editing
Private Sub radListView1_EditorRequired(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.ListViewItemEditorRequiredEventArgs)
    If e.ListViewElement.CurrentColumn.FieldName = "CustomerName" Then
        e.EditorType = GetType(ListViewTextBoxEditor)
    ElseIf e.ListViewElement.CurrentColumn.FieldName = "ProductName" Then
        Dim editor As New ListViewDropDownListEditor()
        TryCast(editor.EditorElement, BaseDropDownListEditorElement).Items.Add("Product1")
        TryCast(editor.EditorElement, BaseDropDownListEditorElement).Items.Add("Product2")
        TryCast(editor.EditorElement, BaseDropDownListEditorElement).Items.Add("Product3")

        e.Editor = editor
    ElseIf e.ListViewElement.CurrentColumn.FieldName = "Quantity" Then
        e.EditorType = GetType(ListViewSpinEditor)
    ElseIf e.ListViewElement.CurrentColumn.FieldName = "OrderDate" Then
        e.EditorType = GetType(ListViewDateTimeEditor)
    End If
End Sub