Hi Pete,
Is this difficult to do? I understand what you are saying, but I have
never had
to do anything like that before. So that leaves me as how do you do it?
I just need a head start.
Well, maybe it'd be better to start with the more basic suggestion I
offered. Especially since I'm doing a little hand-waving above. I know
that binding should be possible, but I don't actually use binding myself
that often and I'm not entirely clear on how binding with ListView would
work (with simpler controls, in which there's a simple display of a list
of text items, it's very straight-forward...for example, in ListBox, just
set the DataSource property, and it does the right thing, assuming your
class overrides ToString() in a useful way).
Even using binding, you'll have to react to changes in the selection,
which is demonstrated in this example illustrating the simpler
suggestion. The main difference between this example and the code you
posted is that the ListView always stays current with whatever is going on
in the TreeView.
Marc Gravell has helped me out with binding questions in the past, so
maybe he'll have some insight as to a specific technique that would hook
up a BindingList<Person> with a ListView. In the meantime, here's an
example (see below) of doing a similar thing, but with a little more code
(i.e. it's more explicit, because it's not using binding).
Pete
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestBindingListView
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
public class Form1 : Form
{
private struct ItemData
{
public readonly string Item;
public readonly string Details;
public ItemData(string item, string details)
{
Item = item;
Details = details;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TreeNodeCollection nodes = treeView1.SelectedNode != null ?
treeView1.SelectedNode.Nodes : treeView1.Nodes;
nodes.Add(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
TreeNode node = treeView1.SelectedNode;
if (node != null)
{
List<ItemData> rgid = (List<ItemData>)node.Tag;
if (rgid == null)
{
rgid = new List<ItemData>();
node.Tag = rgid;
}
rgid.Add(new ItemData(node.Text, textBox2.Text));
_UpdateListView(rgid);
}
}
private void _UpdateListView(List<ItemData> rgid)
{
listView1.Items.Clear();
if (rgid != null)
{
foreach (ItemData id in rgid)
{
ListViewItem lvi = new ListViewItem(id.Item);
lvi.SubItems.Add(id.Details);
listView1.Items.Add(lvi);
}
}
}
private void treeView1_AfterSelect(object sender,
TreeViewEventArgs e)
{
TreeNode node = treeView1.SelectedNode;
if (node != null)
{
_UpdateListView((List<ItemData>)node.Tag);
}
else
{
_UpdateListView(null);
}
}
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
treeView1.SelectedNode = null;
e.Handled = true;
}
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.treeView1 = new System.Windows.Forms.TreeView();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.button2 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 23);
this.button1.TabIndex = 0;
this.button1.Text = "New Tree Node";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(115, 15);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 1;
//
// treeView1
//
this.treeView1.Anchor =
((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.treeView1.Location = new System.Drawing.Point(13, 43);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(292, 257);
this.treeView1.TabIndex = 2;
this.treeView1.AfterSelect += new
System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.treeView1_KeyDown);
//
// listView1
//
this.listView1.Anchor =
((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles..Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Right)));
this.listView1.Columns.AddRange(new
System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.listView1.Location = new System.Drawing.Point(311, 43);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(174, 257);
this.listView1.TabIndex = 3;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Item";
//
// columnHeader2
//
this.columnHeader2.Text = "Description";
this.columnHeader2.Width = 108;
//
// button2
//
this.button2.Location = new System.Drawing.Point(311, 13);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 4;
this.button2.Text = "New Item";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new
System.EventHandler(this.button2_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(381, 15);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(104, 20);
this.textBox2.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(497, 312);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button2);
this.Controls.Add(this.listView1);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox2;
}
}