Jeff Hardy
Jeff Hardy

Reputation: 135

How to add subitems in wpf listvew c#

I want to insert list view subitems in wpf.

I am using this code to insert subitems in windows form listview,

con.Open();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.Tables.Add(dt);

OleDbDataAdapter da = new OleDbDataAdapter("select * from test where (SecSym='" + secsym + "')", con);
da = new OleDbDataAdapter("select * from test where (SecSym='" + secsym + "')order by Date desc", con);
da.Fill(dt);
int iRecords = 0;
foreach (DataRow myrow in dt.Rows)
{
    ListViewItem lItem = new ListViewItem();
    lItem.UseItemStyleForSubItems = false;
    DateTime Date = DateTime.ParseExact(myrow[3].ToString(), "yyyyMMdd", CultureInfo.CurrentCulture);
    string date = Date.ToString("ddd, dd-MMM-yyyy");
    lItem = listviewTargets.Items.Insert(iRecords, date);
    lItem.UseItemStyleForSubItems = false;
    // listviewTargets.Items.Add(myrow[2].ToString());
    lItem.SubItems.Add(myrow[1].ToString());
    lItem.SubItems.Add(myrow[15].ToString());
    lItem.SubItems.Add(myrow[5].ToString(), Color.White, Color.Green, lItem.Font);
    lItem.SubItems.Add(myrow[7].ToString());
    lItem.SubItems.Add(myrow[8].ToString());
    lItem.SubItems.Add(myrow[9].ToString());
    lItem.SubItems.Add(myrow[10].ToString());
    iRecords++;
    lItem = listviewTargets.Items.Insert(iRecords, "");
    lItem.UseItemStyleForSubItems = false;
    lItem.SubItems.Add("");
    lItem.SubItems.Add("");
    lItem.SubItems.Add(myrow[6].ToString(), Color.White, Color.Red, lItem.Font);
    lItem.SubItems.Add(myrow[11].ToString());
    lItem.SubItems.Add(myrow[12].ToString());
    lItem.SubItems.Add(myrow[13].ToString());
    lItem.SubItems.Add(myrow[14].ToString());
    iRecords++;
}
con.Close();

But I don't know how to insert subitems in wpf listview.

There would be a great appreciation if someone could me.

Thanks In Advance.

Upvotes: 2

Views: 5097

Answers (1)

doerig
doerig

Reputation: 1857

The WPF Listview is a bit different and has no "SubItems" You could achieve your goal as follows:

Define the Listview in xaml (with databindings to the collection):

<ListView ItemsSource="{Binding People}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Date of Birth" DisplayMemberBinding="{Binding DOB}"/>
        </GridView>
    </ListView.View>
</ListView>

CodeBehind of this example:

public partial class MainWindow : Window
{
    public class Person
    {
        public string Name {get;set;}
        public DateTime DOB {get;set;}
    }

    public IList<Person> People { get; set; }

    public MainWindow()
    {
        People = new List<Person>() 
        {
            new Person() {Name = "Martin", DOB = DateTime.Now.AddYears(-20)},
            new Person() {Name = "Lilo", DOB = DateTime.Now.AddYears(-25)}
        };

        InitializeComponent();
        this.DataContext = this;
    }
}

ListView Expample

Upvotes: 2

Related Questions