ObjectListView - delete a row by clicking on the selected column with fixed content / text

I have a simple question that I cannot solve on my own.

I have an ObjectListView filled with some of my objects. But in addition to this, I want to have another column with the default text “Delete”. When you click this column, the selected row should be deleted. How to do it?

+5
source share
2 answers

You can achieve this by making the desired line editable and using the CellEditActivation event. Initialize your OLV and delete-column as follows:

// fire cell edit event on single click
objectListView1.CellEditActivation = ObjectListView.CellEditActivateMode.SingleClick;
objectListView1.CellEditStarting += ObjectListView1OnCellEditStarting;

// enable cell edit and always set cell text to "Delete"
deleteColumn.IsEditable = true;
deleteColumn.AspectGetter = delegate {
    return "Delete";
};

Then you can delete the row in the CellEditStarting handler right after clicking on the column:

private void ObjectListView1OnCellEditStarting(object sender, CellEditEventArgs e) {
    // special cell edit handling for our delete-row
    if (e.Column == deleteColumn) {
        e.Cancel = true;        // we don't want to edit anything
        objectListView1.RemoveObject(e.RowObject); // remove object
    }
}

, .

// assign an ImageList containing at least one image to SmallImageList
objectListView1.SmallImageList = imageList1;

// always display image from index 0 as default image for deleteColumn
deleteColumn.ImageGetter = delegate {
    return 0;
};

:

enter image description here

,

deleteColumn.AspectToStringConverter = delegate {
    return String.Empty;
}; 

Aspect , " ". - , - .

+12

"" ObjectListView,

ShowImagesOnSubItems = true;

. ObjectListView.

0

All Articles