none
Listview subitems

    Question

  • string txtFile = (string.Concat(AppDomain.CurrentDomain.BaseDirectory, "results.txt"));
          
          if (File.Exists(txtFile))
          {
            using (StreamReader sr = new StreamReader(txtFile))
            {
              while (!sr.EndOfStream)
              {
               
               listView1.Items.Add(sr.ReadLine());
               // ListViewItem listViewItem1;
               //listViewItem1 = new ListViewItem();
               //ListViewItem.ListViewSubItem listViewSubItem1 = listViewItem1.SubItems.Add("?");
                         
              }
              sr.Close();
           }
          }
          else
          {
            System.Windows.Forms.DialogResult dialogResult2 = MessageBox.Show(((IWin32Window)this), "Text file not found!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Hand);
    
    }

    i have 4 columns in my listview.

     

    listView1.Items.Add(sr.ReadLine());

    above line insert data in First column ... i wanna insert ("?") remaining in columns.

    i try......

     //ListViewItem.ListViewSubItem listViewSubItem1 = listViewItem1.SubItems.Add("?");


     

     

     

    Friday, June 04, 2010 7:55 AM

Answers

  • The first line will add an LVItem to . Other lines are adding subItems to the main item. I mean in the same row? You can use this method in a loop to add 'n' number of records to the list view.

     

    There is no need to create multiple listview items to add records in same row.

     

     ListViewItem lvItem = new ListViewItem(doc.ReminderDate.ToShortDateString());//this will create main item 
    
    
        lvItem.SubItems.Add(doc.ReminderDescription);//these are subitems in same row.
        lvItem.SubItems.Add(doc.ReminderStatus);
        lvItem.SubItems.Add(doc.ReminderDialog);
        lvItem.SubItems.Add(doc.ReminderDays.Count.ToString());
        this.listView1.Items.Add(lvItem);//finally add it to list view
    
    Friday, June 04, 2010 12:34 PM
  • Hi,

    Try this,

    listView1.View = View.Details;
          listView1.Columns.Add("File Value", 100);
          //Added two additional columns for question marks
          listView1.Columns.Add("QuestionOne", 100);
          listView1.Columns.Add("QuestionTwo", 100);
          string txtFile = (string.Concat(AppDomain.CurrentDomain.BaseDirectory, "results.txt"));
          if (File.Exists(txtFile))
          {
            using (StreamReader sr = new StreamReader(txtFile))
            {
              while (!sr.EndOfStream)
              {
                ListViewItem oListViewItem = listView1.Items.Add(sr.ReadLine());
                //Adding two question mark for second and third column
                oListViewItem.SubItems.Add("?");
                oListViewItem.SubItems.Add("?");
              }
            }
          }
          else
          {
            System.Windows.Forms.DialogResult dialogResult2 = MessageBox.Show(((IWin32Window)this), "Text file not found!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Hand);
          }

     

    Regards,

    Vinil;

    Friday, June 04, 2010 12:49 PM

All replies

  •       this.listView1.Items.Clear();//clear all before adding
          foreach(ReminderDocument doc in reminderdatabject.ReminderList)
          {
            ListViewItem lvItem = new ListViewItem(doc.ReminderDate.ToShortDateString());
    
            if (doc.ReminderStatus == "Enabled")
              lvItem.ImageIndex = 1;;//setting image index 
            else
              lvItem.ImageIndex = 0;//setting image index 
    
            lvItem.SubItems.Add(doc.ReminderDate.ToShortTimeString());//Main item
            lvItem.SubItems.Add(doc.ReminderDescription);
            lvItem.SubItems.Add(doc.ReminderStatus);
            lvItem.SubItems.Add(doc.ReminderDialog);
            lvItem.SubItems.Add(doc.ReminderDays.Count.ToString());
            lvItem.Tag = doc;
            this.listView1.Items.Add(lvItem);//finally add it to list view
           }
    
          listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);//this is needed if you need to resize colums based on data added.

    Yeh you should add it to sub items. Above code listing should be the pattern.

    Friday, June 04, 2010 8:09 AM
  •    this
    .listView1.Items.Clear();//clear all before adding
    
    foreach (ReminderDocument doc in reminderdatabject.ReminderList)
    {
    ListViewItem lvItem = new ListViewItem(doc.ReminderDate.ToShortDateString());

    if (doc.ReminderStatus == "Enabled" )
    lvItem.ImageIndex = 1;;//setting image index
    else
    lvItem.ImageIndex = 0;//setting image index

    lvItem.SubItems.Add(doc.ReminderDate.ToShortTimeString());//Main item
    lvItem.SubItems.Add(doc.ReminderDescription);
    lvItem.SubItems.Add(doc.ReminderStatus);
    lvItem.SubItems.Add(doc.ReminderDialog);
    lvItem.SubItems.Add(doc.ReminderDays.Count.ToString());
    lvItem.Tag = doc;
    this .listView1.Items.Add(lvItem);//finally add it to list view
    }

    listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);//this is needed if you need to resize colums based on data added.

    Yeh you should add it to sub items. Above code listing should be the pattern.

    listView1.Items.Add(sr.ReadLine());
                ListViewItem listViewItem1;
                listViewItem1 = new ListViewItem();
    
    
                // listView1.Clear();
                ListViewItem.ListViewSubItem listViewSubItem1 = listViewItem1.SubItems.Add("?");
                ListViewItem.ListViewSubItem listViewSubItem2 = listViewItem1.SubItems.Add("?");
                ListViewItem.ListViewSubItem listViewSubItem3 = listViewItem1.SubItems.Add("?");
                listView1.Items.Add(listViewItem1);

    Can i combine them?

     listView1.Items.Add(sr.ReadLine());

     listView1.Items.Add(listViewItem1);

    i need data in below order

    sr.ReadLine()       listViewItem1 listViewItem2 listViewItem3

    text file data          ?                        ?                   ?

     

    Friday, June 04, 2010 11:40 AM
  • The first line will add an LVItem to . Other lines are adding subItems to the main item. I mean in the same row? You can use this method in a loop to add 'n' number of records to the list view.

     

    There is no need to create multiple listview items to add records in same row.

     

     ListViewItem lvItem = new ListViewItem(doc.ReminderDate.ToShortDateString());//this will create main item 
    
    
        lvItem.SubItems.Add(doc.ReminderDescription);//these are subitems in same row.
        lvItem.SubItems.Add(doc.ReminderStatus);
        lvItem.SubItems.Add(doc.ReminderDialog);
        lvItem.SubItems.Add(doc.ReminderDays.Count.ToString());
        this.listView1.Items.Add(lvItem);//finally add it to list view
    
    Friday, June 04, 2010 12:34 PM
  • Hi,

    Try this,

    listView1.View = View.Details;
          listView1.Columns.Add("File Value", 100);
          //Added two additional columns for question marks
          listView1.Columns.Add("QuestionOne", 100);
          listView1.Columns.Add("QuestionTwo", 100);
          string txtFile = (string.Concat(AppDomain.CurrentDomain.BaseDirectory, "results.txt"));
          if (File.Exists(txtFile))
          {
            using (StreamReader sr = new StreamReader(txtFile))
            {
              while (!sr.EndOfStream)
              {
                ListViewItem oListViewItem = listView1.Items.Add(sr.ReadLine());
                //Adding two question mark for second and third column
                oListViewItem.SubItems.Add("?");
                oListViewItem.SubItems.Add("?");
              }
            }
          }
          else
          {
            System.Windows.Forms.DialogResult dialogResult2 = MessageBox.Show(((IWin32Window)this), "Text file not found!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Hand);
          }

     

    Regards,

    Vinil;

    Friday, June 04, 2010 12:49 PM