0

How to add Subitem to listview programmatically. want to add dynamically items to listview. Below is the secnario.

   listView1.Items.Add(new Datalist()
            {
                clinicid = "Clinic ID",
                if (chkdate.IsChecked == true)
                listView1.Items.Add(new Datalist() { Date = "Date" });

            });

enter image description here

Khalid
  • 603
  • 9
  • 22
  • Is the above code not working? what is the error? – user1672994 Nov 21 '15 at 19:02
  • Problem is i try alot to add a subitem to listView on the base of checkboxcontrol checked and new Column is added to listview and i want to add a subitems element to listview which contain data which is retrive from database. – Khalid Nov 21 '15 at 19:05
  • I want to code like this to working in WPF this Code work in Window Form but in WPF its not working.
    ListView1.Items.Add(new { ClinicID = "123",if(chkname.IsChecked == true){ Name = "Khalid"}, if(chkgender.IsChecked == true) { Gender = "Male" }});
    – Khalid Nov 21 '15 at 19:23
  • possible duplicate of [WPF ListView Subitems without XAML](http://stackoverflow.com/questions/4687184/add-subitems-to-listview-without-using-xaml). – user1672994 Nov 21 '15 at 19:30
  • WPF listview dont have 'subitems' like the old winforms does – user1672994 Nov 21 '15 at 19:30
  • use List li = new List(); – Khalid Nov 21 '15 at 19:47

1 Answers1

1

Something like this:

ListView1.Items.Add(new 
{
   ClinicID = "123",
   Name = chkname.IsChecked ? "Khalid" : string.Empty,
   Gender = chkgender.IsChecked ? "Male" : string.Empty
});
toddmo
  • 20,682
  • 14
  • 97
  • 107
  • ListView1.Items.Add(new { ClinicID = "123",if(chkname.IsChecked == true){ Name = "Khalid"}, if(chkgender.IsChecked == true) { Gender = "Male" }}); – Khalid Nov 21 '15 at 19:22
  • 1
    @Khalid, yeah you can't have sub-brackets in an object constructor like that. I changed it to what should compile given your desires. – toddmo Nov 21 '15 at 20:53