August 19, 2006

Using A ListView

To get a nice excel type view of the list set the properties 'View=Details' and 'GridLines=True' or put the following code in the designer

//this.listView.Anchor =  //Dont forget to set the anchors appropriately
this.listView.AllowColumnReorder = true;
this.listView.View = System.Windows.Forms.View.Details; 
this.listView.GridLines = true;

// Select highlights a full row
this.listView.FullRowSelect = true;

// Select a single row only
this.listView.MultiSelect = false;

// Keep selected rows highlited even when the control does not have the focus 
this.listView.HideDelection = false; 
Adding items to the ListView
private const int NAME_COLUMN = 0;
private const int ID_COLUMN = 1;

void PopulateListView()
{
    // DONT call "listView.Clear()" or you'll lose all your
    // column data and then wonder why you table is empty!
    listView.Items.Clear(); 
    // Add the item to the ListView
    string[] fields = new string[] 
    { 
        "column1string", 
        "column2string", 
        ... 
    };
OR
    fields[NAME_COLUMN] = xxx.Name;
    fields[ID_COLUMN] = xxx.Id.ToString();
    ...

    ListViewItem lvItem = new ListViewItem(fields);
    // Insert at the top of the listview control
    listView.Items.Insert(0, lvItem);
    // Append to the end of the ListView control
    listView.Items.Add(lvItem);

    // with custom listviewitems
    MyListViewItem li = new MyListViewItem(...)
    listView.Items.Add(li);
}

To update a column:
private void UpdateIdColumn(int id)
{
    listView.Items[m_SelectedIndex].SubItems[ID_COLUMN].Text = id.ToString();
}
Optionally define a custom list view item which holds a reference to one of your types XXX
internal class MyListViewItem : ListViewItem
{
    internal MyListViewItem(XXX xxx)
        : base(new string[] { 
                    xxx.Name, 
                    xxx.Description, 
                })
    {
        XXX = xxx;
    }

    internal XXX XXX { get; set; }
}
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listView.SelectedItems.Count == 0)
    {
 // Are there any controls need enabling/
 // disabling when the nothing is selected
        button.Enabled = false;
        return;
    }
 
    // Are there any controls need enabling/
    // disabling when the nothing is selected
    otherCtrl.Enabled = true;
 
    foreach (ListViewItem lvi in lvParts.SelectedItems)
    {
        ((XXX)lvi.Tag).DoSomething();
 // or
        MyCustomListItem li = lvi as MyCustomListItem;
        if (li != null)
        {
            li.XXX.DoSomething(); 
        }
        break;
    }
}

Dont forget to add a method to resize the columns:
private void FitColumnsToListView()
{
    int extra = ListViewSpareColumnSpace(lvParts);
    if (extra > 0)
    {
        lvParts.Columns[0].Width += extra;
    }
}

private int ListViewSpareColumnSpace(ListView lv)
{
    if (lv.Columns.Count == 0)
        return 0;

    int width = lv.Size.Width;
    int usedWidth = 0;
    // 2 pixels for the divider lines
    foreach (ColumnHeader ch in lv.Columns)
    {
        usedWidth += ch.Width + 2;
    }
    int extra = width - usedWidth;
    return extra;
}
To disable selection of listview items, handle the ItemSelectionChanged event on the listView:
private void listView_ItemSelectionChanged(object sender, 
  ListViewItemSelectionChangedEventArgs e)
{
  if (e.IsSelected)
    e.Item.Selected = false;
}

No comments: