Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListView in C#

ListView in C#

Scheduled Pinned Locked Moved C#
csharpdata-structuresxmlhelpquestion
25 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mrithula8

    Hi I am using this code to list the files in ListView control.When i try to add the files to the listview control using

    listView1.Items.Add(fil2.ToString());

    it gives me only one file i.e the last file in the folder.What change should i do to list all the files in that folder?Please help me with this

    amespace WindowsApplication30
    {
    public partial class Form1 : Form
    {
    XmlTextReader reader;
    XmlDocument doc = new XmlDocument();
    ColumnHeader colHead;
    String sss = "";
    //private System.Windows.Forms.ListView lwFilesAndFolders;
    public Form1()
    {
    InitializeComponent();
    }

        private void ListDrives\_Click(object sender, EventArgs e)
        {
            string\[\] drives = Environment.GetLogicalDrives();
            // Loop into the string array
            foreach (string strDrive in drives)
            {
                // Add items (drives) to the list
                listBox1.Items.Add(strDrive.ToString());
            }
        }
    
        private void ShowPath\_Click(object sender, EventArgs e)
        {
            CreateHeadersAndFillListView();
            PaintList(sss);
        }
    
        private void CreateHeadersAndFillListView()
        {
            colHead = new ColumnHeader();
            colHead.Text = "Servers";
            colHead.Width = 200;
            listView1.Columns.Add(colHead);
    
            colHead = new ColumnHeader();
            colHead.Text = "Status";
            colHead.Width = 100;
            listView1.Columns.Add(colHead);
    
            colHead = new ColumnHeader();
            colHead.Text = "Last accessed";
            colHead.Width = 100;
            listView1.Columns.Add(colHead);            
        }
    
        private void PaintList(String sss)
        {
            try
            {
                //ListViewItem lvi;
                //ListViewItem.ListViewSubItem lvsi;
                reader = new XmlTextReader("path.xml");
               
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Text: //Display the text in each element.
                            //   Console.WriteLine(reader.Value);
                            String s1;
                            s1 = reader.Value;
                            String ss;
                            ss = listBox1.SelectedItem.ToString();
    
    X Offline
    X Offline
    Xmen Real
    wrote on last edited by
    #3

    I use this code to get all files from a folder or drive

        public static string\[\] GetFilesPaths(string directory)
        {
            List filesPaths = new List();
            GetDirectories(directory, filesPaths);
            return filesPaths.ToArray();
        }
        static void GetDirectories(string dir, List filesPaths)
        {
            filesPaths.AddRange(Directory.GetFiles(dir));
            string\[\] subdirectoryEntries = Directory.GetDirectories(dir);
            foreach (string subdirectory in subdirectoryEntries)
                GetDirectories(subdirectory, filesPaths);
        }
    

    you can get string[] and then you can add 'em in ListView...ListView is slow BTW, use DataGridView instead.

    TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

    ----------------------------------------------- 128 bit encrypted signature, crack if you can

    modified on Wednesday, March 25, 2009 3:13 AM

    0 1 Reply Last reply
    0
    • C Christian Graus

      What happens when you step through the code ?

      Christian Graus Driven to the arms of OSX by Vista.

      M Offline
      M Offline
      mrithula8
      wrote on last edited by
      #4

      Hi fil2 contains the list of all the files.If i use listBox.Items.Add(fil2.Tostring()); it dis[plays the list of files in the listbox.But i want to display in the listview control.I have added column headers to the listview control.I want to display the names of the files under the header named "Servers".

      foreach (String filename in files)
      {
      FileInfo fil2 = new FileInfo(filename);
      foreach (String sfil in fil2)
      {
      listView1.Items.Add(sfil);

                                              }
      

      This gives me name of only the last file in that folder.

      K 1 Reply Last reply
      0
      • X Xmen Real

        I use this code to get all files from a folder or drive

            public static string\[\] GetFilesPaths(string directory)
            {
                List filesPaths = new List();
                GetDirectories(directory, filesPaths);
                return filesPaths.ToArray();
            }
            static void GetDirectories(string dir, List filesPaths)
            {
                filesPaths.AddRange(Directory.GetFiles(dir));
                string\[\] subdirectoryEntries = Directory.GetDirectories(dir);
                foreach (string subdirectory in subdirectoryEntries)
                    GetDirectories(subdirectory, filesPaths);
            }
        

        you can get string[] and then you can add 'em in ListView...ListView is slow BTW, use DataGridView instead.

        TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

        ----------------------------------------------- 128 bit encrypted signature, crack if you can

        modified on Wednesday, March 25, 2009 3:13 AM

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #5

        Just a point of curiosity. Why do you pass the List by reference? I thought that a collection can be added to without passing by reference

        X 1 Reply Last reply
        0
        • 0 0x3c0

          Just a point of curiosity. Why do you pass the List by reference? I thought that a collection can be added to without passing by reference

          X Offline
          X Offline
          Xmen Real
          wrote on last edited by
          #6

          Computafreak wrote:

          I thought that a collection can be added to without passing by reference

          yes, you're right...thanks

          TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

          ----------------------------------------------- 128 bit encrypted signature, crack if you can

          1 Reply Last reply
          0
          • M mrithula8

            Hi fil2 contains the list of all the files.If i use listBox.Items.Add(fil2.Tostring()); it dis[plays the list of files in the listbox.But i want to display in the listview control.I have added column headers to the listview control.I want to display the names of the files under the header named "Servers".

            foreach (String filename in files)
            {
            FileInfo fil2 = new FileInfo(filename);
            foreach (String sfil in fil2)
            {
            listView1.Items.Add(sfil);

                                                    }
            

            This gives me name of only the last file in that folder.

            K Offline
            K Offline
            King Julien
            wrote on last edited by
            #7

            To add an item in ListView control you should supply the listitem. This may work.... Modify your code like this and try....

            foreach (String filename in files)
            {
            FileInfo fil2 = new FileInfo(filename);

            foreach (String sfil in fil2)
            {
            ListViewItem listitem = new ListViewItem();
            listitem.SubItems.Add(sfil);
            listView1.Items.Add(listitem);

            }
            }

            Have a Happy Coding.....

            M 1 Reply Last reply
            0
            • K King Julien

              To add an item in ListView control you should supply the listitem. This may work.... Modify your code like this and try....

              foreach (String filename in files)
              {
              FileInfo fil2 = new FileInfo(filename);

              foreach (String sfil in fil2)
              {
              ListViewItem listitem = new ListViewItem();
              listitem.SubItems.Add(sfil);
              listView1.Items.Add(listitem);

              }
              }

              Have a Happy Coding.....

              M Offline
              M Offline
              mrithula8
              wrote on last edited by
              #8

              Hi Thanks for the reply.i tried with this, but it shows me an error for the line

              foreach (String sfil in fil2)

              saying that the foreach cannot be applied to FileInfo.So i tried without the foreach

              foreach (String filename in files)
              {
              FileInfo fil2 = new FileInfo(filename);
              lvi = new ListViewItem();
              lvi.Text = filename.ToString();
              listView1.Items.Add(lvi);}

              In any way im getting only the last file in the folder.The following gets the files from the folder.I do not know how to display it in the listview control.

              DirectoryInfo di = new DirectoryInfo(foldername);
              if (di.Exists)
              {
              String[] files;
              files = Directory.GetFiles(foldername);

              K 1 Reply Last reply
              0
              • M mrithula8

                Hi Thanks for the reply.i tried with this, but it shows me an error for the line

                foreach (String sfil in fil2)

                saying that the foreach cannot be applied to FileInfo.So i tried without the foreach

                foreach (String filename in files)
                {
                FileInfo fil2 = new FileInfo(filename);
                lvi = new ListViewItem();
                lvi.Text = filename.ToString();
                listView1.Items.Add(lvi);}

                In any way im getting only the last file in the folder.The following gets the files from the folder.I do not know how to display it in the listview control.

                DirectoryInfo di = new DirectoryInfo(foldername);
                if (di.Exists)
                {
                String[] files;
                files = Directory.GetFiles(foldername);

                K Offline
                K Offline
                King Julien
                wrote on last edited by
                #9

                Hi mrithula.... I have personally checked the below code and its working properly in my machine....

                DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
                if (di.Exists)
                {
                String[] files;
                files = Directory.GetFiles(fbd.SelectedPath);
                foreach (string file in files)
                {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = file;
                lvi.ImageIndex = 0;
                listView1.Items.Add(lvi);
                }
                }

                If it shows any error let me know....

                Have a Happy Coding.....

                M 2 Replies Last reply
                0
                • K King Julien

                  Hi mrithula.... I have personally checked the below code and its working properly in my machine....

                  DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
                  if (di.Exists)
                  {
                  String[] files;
                  files = Directory.GetFiles(fbd.SelectedPath);
                  foreach (string file in files)
                  {
                  ListViewItem lvi = new ListViewItem();
                  lvi.Text = file;
                  lvi.ImageIndex = 0;
                  listView1.Items.Add(lvi);
                  }
                  }

                  If it shows any error let me know....

                  Have a Happy Coding.....

                  M Offline
                  M Offline
                  mrithula8
                  wrote on last edited by
                  #10

                  Hi Thank you.Im getting it right now

                  K 1 Reply Last reply
                  0
                  • M mrithula8

                    Hi Thank you.Im getting it right now

                    K Offline
                    K Offline
                    King Julien
                    wrote on last edited by
                    #11

                    "Ella Pugazhum iraivan oruvanukkae"

                    Have a Happy Coding.....

                    1 Reply Last reply
                    0
                    • K King Julien

                      Hi mrithula.... I have personally checked the below code and its working properly in my machine....

                      DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
                      if (di.Exists)
                      {
                      String[] files;
                      files = Directory.GetFiles(fbd.SelectedPath);
                      foreach (string file in files)
                      {
                      ListViewItem lvi = new ListViewItem();
                      lvi.Text = file;
                      lvi.ImageIndex = 0;
                      listView1.Items.Add(lvi);
                      }
                      }

                      If it shows any error let me know....

                      Have a Happy Coding.....

                      M Offline
                      M Offline
                      mrithula8
                      wrote on last edited by
                      #12

                      Hi I am able to list all the exe files in listView control.If the 1st exe has started i want to set the status of the ist sub item to "Started"

                      foreach(Process pr in proclist)
                      {
                      String proname = pr.ProcessName;
                      //listBox4.Items.Add(proname);
                      lvsi=new ListViewItem.ListViewSubItem();
                      lvsi.Text="Started...";
                      lvi.SubItems.Add(lvsi);
                      }

                              int b = proclist.Count;
                              listBox4.Items.Add(b.ToString());
                      

                      proclist contains the list of all exe that have started.Currently there is only 1 exe(i.e the 1st one in the listview control).I want to display started to the 1st subitem.The above code that im using displays "started" as the last sub item.How can i change this?

                      K 1 Reply Last reply
                      0
                      • M mrithula8

                        Hi I am able to list all the exe files in listView control.If the 1st exe has started i want to set the status of the ist sub item to "Started"

                        foreach(Process pr in proclist)
                        {
                        String proname = pr.ProcessName;
                        //listBox4.Items.Add(proname);
                        lvsi=new ListViewItem.ListViewSubItem();
                        lvsi.Text="Started...";
                        lvi.SubItems.Add(lvsi);
                        }

                                int b = proclist.Count;
                                listBox4.Items.Add(b.ToString());
                        

                        proclist contains the list of all exe that have started.Currently there is only 1 exe(i.e the 1st one in the listview control).I want to display started to the 1st subitem.The above code that im using displays "started" as the last sub item.How can i change this?

                        K Offline
                        K Offline
                        King Julien
                        wrote on last edited by
                        #13

                        when you add the subitem for an item in the listview the sequence starts always from left to right. Not sure what mistake you are doing. But you can try this... To add the subitem, for the STATUS column header, use the same item object which you used to add to the listview control. i.e. if item 1 in the Server header is added by using lvi then,

                        ListviewItem lvi = new ListViewItem();
                        lvi.SubItem.Add("Started");
                        ListView.Items.Add(lvi);

                        This should work i believe.

                        Have a Happy Coding.....

                        M 1 Reply Last reply
                        0
                        • K King Julien

                          when you add the subitem for an item in the listview the sequence starts always from left to right. Not sure what mistake you are doing. But you can try this... To add the subitem, for the STATUS column header, use the same item object which you used to add to the listview control. i.e. if item 1 in the Server header is added by using lvi then,

                          ListviewItem lvi = new ListViewItem();
                          lvi.SubItem.Add("Started");
                          ListView.Items.Add(lvi);

                          This should work i believe.

                          Have a Happy Coding.....

                          M Offline
                          M Offline
                          mrithula8
                          wrote on last edited by
                          #14

                          Hi I tried with this but it displays "Started.." as a subitem for the last item in listview control.I used this code in the button click event of "Start" button which starts the exe file.

                          K 1 Reply Last reply
                          0
                          • M mrithula8

                            Hi I tried with this but it displays "Started.." as a subitem for the last item in listview control.I used this code in the button click event of "Start" button which starts the exe file.

                            K Offline
                            K Offline
                            King Julien
                            wrote on last edited by
                            #15

                            okay..... then you may try the SubItem index property, to directly specify which subitem to modify. for example, if you want to change the status of the first server Item, then ServerItem1.SubItems[1].text = "Started";

                            Have a Happy Coding.....

                            M 2 Replies Last reply
                            0
                            • K King Julien

                              okay..... then you may try the SubItem index property, to directly specify which subitem to modify. for example, if you want to change the status of the first server Item, then ServerItem1.SubItems[1].text = "Started";

                              Have a Happy Coding.....

                              M Offline
                              M Offline
                              mrithula8
                              wrote on last edited by
                              #16

                              Hi I tried with this.Is this right?Nothing gets displayed when the exe starts.

                              int g = lvi.Index;

                                          lvi = new ListViewItem();
                                         
                                          if (g == 1)
                                              lvi.SubItems\[1\].Text = "Started..";
                              
                                          listView1.Items.Add(lvi);
                              
                              K 1 Reply Last reply
                              0
                              • M mrithula8

                                Hi I tried with this.Is this right?Nothing gets displayed when the exe starts.

                                int g = lvi.Index;

                                            lvi = new ListViewItem();
                                           
                                            if (g == 1)
                                                lvi.SubItems\[1\].Text = "Started..";
                                
                                            listView1.Items.Add(lvi);
                                
                                K Offline
                                K Offline
                                King Julien
                                wrote on last edited by
                                #17

                                no this will not work. because you are creating a new lvi.

                                Have a Happy Coding.....

                                1 Reply Last reply
                                0
                                • K King Julien

                                  okay..... then you may try the SubItem index property, to directly specify which subitem to modify. for example, if you want to change the status of the first server Item, then ServerItem1.SubItems[1].text = "Started";

                                  Have a Happy Coding.....

                                  M Offline
                                  M Offline
                                  mrithula8
                                  wrote on last edited by
                                  #18

                                  Hi Can you please tell why it adds "started.." as a sub item for last item?Why does it not display it as the sub item for the 1st item?

                                  K 1 Reply Last reply
                                  0
                                  • M mrithula8

                                    Hi Can you please tell why it adds "started.." as a sub item for last item?Why does it not display it as the sub item for the 1st item?

                                    K Offline
                                    K Offline
                                    King Julien
                                    wrote on last edited by
                                    #19

                                    Not sure what's the problem.... but instead of this

                                    foreach(Process pr in proclist)
                                    {
                                    String proname = pr.ProcessName;
                                    //listBox4.Items.Add(proname);
                                    lvsi=new ListViewItem.ListViewSubItem();
                                    lvsi.Text="Started...";
                                    lvi.SubItems.Add(lvsi);
                                    }

                                    try this foreach(Process pr in proclist) { String proname = pr.ProcessName; //listBox4.Items.Add(proname); lvi.SubItems[1].Text = "started"; } Make it sure that you are executing this code after adding the item to the list view... otherwise you may get an exception in the blocked line.

                                    Have a Happy Coding.....

                                    M 1 Reply Last reply
                                    0
                                    • K King Julien

                                      Not sure what's the problem.... but instead of this

                                      foreach(Process pr in proclist)
                                      {
                                      String proname = pr.ProcessName;
                                      //listBox4.Items.Add(proname);
                                      lvsi=new ListViewItem.ListViewSubItem();
                                      lvsi.Text="Started...";
                                      lvi.SubItems.Add(lvsi);
                                      }

                                      try this foreach(Process pr in proclist) { String proname = pr.ProcessName; //listBox4.Items.Add(proname); lvi.SubItems[1].Text = "started"; } Make it sure that you are executing this code after adding the item to the list view... otherwise you may get an exception in the blocked line.

                                      Have a Happy Coding.....

                                      M Offline
                                      M Offline
                                      mrithula8
                                      wrote on last edited by
                                      #20

                                      Hi It tells that 1 is not valid argument index.

                                      K 1 Reply Last reply
                                      0
                                      • M mrithula8

                                        Hi It tells that 1 is not valid argument index.

                                        K Offline
                                        K Offline
                                        King Julien
                                        wrote on last edited by
                                        #21

                                        how many column headers you have??? because the subitems index always starts from 0. can you post the entire code block of the listcontrol??? if possible...

                                        Have a Happy Coding.....

                                        M 2 Replies Last reply
                                        0
                                        • K King Julien

                                          how many column headers you have??? because the subitems index always starts from 0. can you post the entire code block of the listcontrol??? if possible...

                                          Have a Happy Coding.....

                                          M Offline
                                          M Offline
                                          mrithula8
                                          wrote on last edited by
                                          #22

                                          Hi I have created 3 column headers

                                          private void CreateHeadersAndFillListView()
                                          {
                                          colHead = new ColumnHeader();
                                          colHead.Text = "Servers";
                                          colHead.Width = 250;
                                          listView1.Columns.Add(colHead);

                                                  colHead = new ColumnHeader();
                                                  colHead.Text = "Status";
                                                  colHead.Width = 100;
                                                  listView1.Columns.Add(colHead);
                                          
                                                  colHead = new ColumnHeader();
                                                  colHead.Text = "Last accessed";
                                                  colHead.Width = 100;
                                                  listView1.Columns.Add(colHead);
                                          
                                          
                                              }
                                          
                                              private void PaintList(String sss)
                                              {
                                                  try
                                                  {
                                                      
                                                      //ListViewItem.ListViewSubItem lvsi;
                                                      reader = new XmlTextReader("path.xml");
                                          
                                                      while (reader.Read())
                                                      {
                                                          switch (reader.NodeType)
                                                          {
                                                              case XmlNodeType.Text: //Display the text in each element.
                                                                  //   Console.WriteLine(reader.Value);
                                                                  String s1;
                                                                  s1 = reader.Value;
                                                                  String ss;
                                                                  ss = listBox4.SelectedItem.ToString();
                                                                  sss = ss + s1;
                                                                  object o3 = (object)sss;
                                                                  //String s = (string)o3;
                                                                  //listBox2.Items.Add(o3);
                                          
                                                                  DirectoryInfo dire = new DirectoryInfo(sss);
                                                                  if (dire.Exists)
                                                                  {
                                                                      String\[\] folder;
                                                                      folder = Directory.GetDirectories(sss);
                                                                      foreach (string foldername in folder)
                                                                      {
                                                                          DirectoryInfo di = new DirectoryInfo(foldername);
                                                                          if (di.Exists)
                                                                          {
                                                                              String\[\] files;
                                                                              files = Directory.GetFiles(foldername,"\*.exe\*");
                                          
                                                                              foreach (String filename in files)
                                                                              {
                                                                                  lvi = new ListViewItem();
                                                                                  lvi.Text = filename;
                                                                                  listView1.Items.Add(lvi);
                                          
                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups