.

Displaying Items in a Listview with details view

Displaying Items in a Listview with details view

 

listView1.View = View.Details;
this.listView1.Columns.Add(new ColumnHeader() { Text = "move" });
this.listView1.Columns.Add(new ColumnHeader() { Text = "followed by" });
this.listView1.Columns.Add(new ColumnHeader() { Text = "score" });
ListViewItem lvi = new ListViewItem("e2-e4");
lvi.SubItems.Add("e7-e5 d2-d24 e5xd4");
lvi.SubItems.Add("+0.86");
listView1.Items.Add(lvi);

2 Responses to this post.

  1. Posted by Pietman on 09.06.12 at 1:43 pm

    and sorting:

    // An instance of the SortWrapper class is created for
    // each item and added to the ArrayList for sorting.
    public class SortWrapper
    {
    internal ListViewItem sortItem;
    internal int sortColumn;

    // A SortWrapper requires the item and the index of the clicked column.
    public SortWrapper (ListViewItem Item, int iColumn)
    {
    sortItem = Item;
    sortColumn = iColumn;
    }

    // Text property for getting the text of an item.
    public string Text
    {
    get
    {
    return sortItem.SubItems[sortColumn].Text;
    }
    }

    // Implementation of the IComparer
    // interface for sorting ArrayList items.
    public class SortComparer : IComparer
    {
    bool ascending;

    // Constructor requires the sort order;
    // true if ascending, otherwise descending.
    public SortComparer(bool asc)
    {
    this.ascending = asc;
    }

    // Implemnentation of the IComparer:Compare
    // method for comparing two objects.
    public int Compare(object x, object y)
    {
    SortWrapper xItem = (SortWrapper) x;
    SortWrapper yItem = (SortWrapper) y;

    string xText = xItem.sortItem.SubItems[xItem.sortColumn].Text;
    string yText = yItem.sortItem.SubItems[yItem.sortColumn].Text;
    return xText.CompareTo(yText) * (this.ascending ? 1 : -1);
    }
    }
    }

  2. Posted by Pietman on 09.06.12 at 1:43 pm

    or by comparer:

    this.listView1.ListViewItemSorter = new ListviewScoreComparer(board.turn);
    listView1.Sort();

    class ListviewScoreComparer : IComparer
    {
    ChessLibrary.Color C;
    public ListviewScoreComparer(ChessLibrary.Color col)
    {
    C = col;
    }

    public int Compare(object x, object y)
    {
    int returnVal = -1;
    int v1 = Convert.ToInt16(((ListViewItem)x).SubItems[2].Text);
    int v2 = Convert.ToInt16(((ListViewItem)y).SubItems[2].Text);
    if (C == ChessLibrary.Color.Black)
    returnVal = v1==v2? 0 : (v1<v2 ? -1:1);
    else
    returnVal = v1 == v2 ? 0 : (v1 > v2 ? -1 : 1);
    return returnVal;
    }
    }

What's your thoughts on this?

*

Protected by WP Anti Spam