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. Getting data from listview

Getting data from listview

Scheduled Pinned Locked Moved C#
help
8 Posts 3 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.
  • T Offline
    T Offline
    teejayem
    wrote on last edited by
    #1

    Hi i am having an issue about getting data from list view. I am wanting to get data from a listview and place it in 3 textboxes. Here is the code i have to place it from the 3 textboxes to the listview but i am wanting to reverse this.

                string\[\] myItems = new string\[\]
                    {
                        textBox1.Text,
                        textBox2.Text,
                        textBox3.Text
                    };
                ListViewItem lvi = new ListViewItem(myItems);
    
                listView1.Items.Add(lvi);
    

    thanks for you help in advance Don't be overcome by evil, but overcome evil with good

    S K 2 Replies Last reply
    0
    • T teejayem

      Hi i am having an issue about getting data from list view. I am wanting to get data from a listview and place it in 3 textboxes. Here is the code i have to place it from the 3 textboxes to the listview but i am wanting to reverse this.

                  string\[\] myItems = new string\[\]
                      {
                          textBox1.Text,
                          textBox2.Text,
                          textBox3.Text
                      };
                  ListViewItem lvi = new ListViewItem(myItems);
      
                  listView1.Items.Add(lvi);
      

      thanks for you help in advance Don't be overcome by evil, but overcome evil with good

      S Offline
      S Offline
      Sean89
      wrote on last edited by
      #2

      To get the data from a list view you use the Items property. For example if you wanted to get the data from the selected item in the list:

      int selected = listView1.SelectedItems[0].Index;  // get the first selected item's index
      
      textBox1.Text = listView1.Items[selected].Text // to get the data in the the first column.
      
      textBox2.Text = listView1.Items[selected].SubItems[1].Text; // to get the data in the second column
      

      And so on... Hope that helps :laugh:

      T 1 Reply Last reply
      0
      • S Sean89

        To get the data from a list view you use the Items property. For example if you wanted to get the data from the selected item in the list:

        int selected = listView1.SelectedItems[0].Index;  // get the first selected item's index
        
        textBox1.Text = listView1.Items[selected].Text // to get the data in the the first column.
        
        textBox2.Text = listView1.Items[selected].SubItems[1].Text; // to get the data in the second column
        

        And so on... Hope that helps :laugh:

        T Offline
        T Offline
        teejayem
        wrote on last edited by
        #3

        Thanks for your help sean. I tried that but i couldn't get it to work. Let me explain what i have going on in this program. First i have a button that opens an "openFileDialog". After i select the file it puts the fileName in textbox1.text, in textbox2.text it has the filetype, in textbox3.text it has "pending". Then after that i have it set up to move to the listview by the code i posted above. What i'm wanting to do is to be able to push a button and it will move the filename back from the listview to textbox1.text. Then from there i will be able to do File.Copy(path, path2) ((path would be textbox1.text)) I don't know if you even understand what i'm saying (i think i just confused myself as i'm posting this). As you can tell i am still learning :) Don't be overcome by evil, but overcome evil with good

        S 1 Reply Last reply
        0
        • T teejayem

          Thanks for your help sean. I tried that but i couldn't get it to work. Let me explain what i have going on in this program. First i have a button that opens an "openFileDialog". After i select the file it puts the fileName in textbox1.text, in textbox2.text it has the filetype, in textbox3.text it has "pending". Then after that i have it set up to move to the listview by the code i posted above. What i'm wanting to do is to be able to push a button and it will move the filename back from the listview to textbox1.text. Then from there i will be able to do File.Copy(path, path2) ((path would be textbox1.text)) I don't know if you even understand what i'm saying (i think i just confused myself as i'm posting this). As you can tell i am still learning :) Don't be overcome by evil, but overcome evil with good

          S Offline
          S Offline
          Sean89
          wrote on last edited by
          #4

          I understand what your trying to do ;) The only way I know how to get the data from the list view is by the way I posted earlier. Thats odd that this is not working for you though :confused: Could you post the code that you have written to get the data out of the list view?

          T 2 Replies Last reply
          0
          • S Sean89

            I understand what your trying to do ;) The only way I know how to get the data from the list view is by the way I posted earlier. Thats odd that this is not working for you though :confused: Could you post the code that you have written to get the data out of the list view?

            T Offline
            T Offline
            teejayem
            wrote on last edited by
            #5

            Alright here is what i got going on: This button does the openfile and puts it in the textboxes:

                private void button1\_Click(object sender, EventArgs e)
                {
                    OpenFileDialog music = new OpenFileDialog();
                    music.Title = "Add a music file";
                    music.InitialDirectory = "C:\\\\";
                    music.Filter = "Supported File Types|\*.mp3,\*.wma|All files (\*.\*)|\*.\*";
                    music.FilterIndex = 2;
                    music.RestoreDirectory = false;
            
                    if (music.ShowDialog() == DialogResult.OK)
                    {
                        music.Multiselect = true;
                        foreach (string fileName in music.FileNames)
                        {
                            textBox1.Text = fileName;
                            textBox2.Text = "Audio";
                            textBox3.Text = "Pending";
                        }
                        string\[\] myItems = new string\[\]
                            {
                                textBox1.Text,
                                textBox2.Text,
                                textBox3.Text
                            };
                        ListViewItem lvi = new ListViewItem(myItems);
            
                        listView1.Items.Add(lvi);
                    }
                    
                }
            

            what i'm wanting to do before i do file.copy is move the filename in the listview back to textbox1.text (textbox2 and 3 also but it doesn't really matter as of right now)

                private void button6\_Click(object sender, EventArgs e)
                {
                    string path = textBox1.Text;
                    string path2 = textBox4.Text;
            
                    try
                    {
                        // Copy the file.
                        File.Copy(path, path2);
                        this.syncstat.Text = "Status: Files Copied";
                    }
            
                    catch
                    {
                        this.syncstat.Text = "Status:  Cannot copy into the same folder";
                    }
            

            Don't be overcome by evil, but overcome evil with good

            1 Reply Last reply
            0
            • S Sean89

              I understand what your trying to do ;) The only way I know how to get the data from the list view is by the way I posted earlier. Thats odd that this is not working for you though :confused: Could you post the code that you have written to get the data out of the list view?

              T Offline
              T Offline
              teejayem
              wrote on last edited by
              #6

              I'm sorry sean i shoulda read more clearly. this is how i tested it

                  private void button6\_Click(object sender, EventArgs e)
                  {
                      int selected = listView1.SelectedItems\[0\].Index;  // get the first selected item's index
                      textBox1.Text = listView1.Items\[selected\].Text; // to get the data in the the first column.
                      textBox2.Text = listView1.Items\[selected\].SubItems\[1\].Text; // to get the data in the second column
                      textBox3.Text = listView1.Items\[selected\].SubItems\[2\].Text;
              
              
                  }
              

              but i couldn't get it to show in the 3 boxes Don't be overcome by evil, but overcome evil with good

              S 1 Reply Last reply
              0
              • T teejayem

                Hi i am having an issue about getting data from list view. I am wanting to get data from a listview and place it in 3 textboxes. Here is the code i have to place it from the 3 textboxes to the listview but i am wanting to reverse this.

                            string\[\] myItems = new string\[\]
                                {
                                    textBox1.Text,
                                    textBox2.Text,
                                    textBox3.Text
                                };
                            ListViewItem lvi = new ListViewItem(myItems);
                
                            listView1.Items.Add(lvi);
                

                thanks for you help in advance Don't be overcome by evil, but overcome evil with good

                K Offline
                K Offline
                Kuira
                wrote on last edited by
                #7

                I dont know if this works but I doubt it cause its wierd the way your putting it to a question: ListViewItem myItems = (ListViewItem) listView1.SelectedItems[0]; textBox1.Text = myItems.Text; textBox2.Text = myItems.SubItems[0].Text; textBox3.Text = myItems.SubItems[1].Text; But thats just my guess :shrug:

                1 Reply Last reply
                0
                • T teejayem

                  I'm sorry sean i shoulda read more clearly. this is how i tested it

                      private void button6\_Click(object sender, EventArgs e)
                      {
                          int selected = listView1.SelectedItems\[0\].Index;  // get the first selected item's index
                          textBox1.Text = listView1.Items\[selected\].Text; // to get the data in the the first column.
                          textBox2.Text = listView1.Items\[selected\].SubItems\[1\].Text; // to get the data in the second column
                          textBox3.Text = listView1.Items\[selected\].SubItems\[2\].Text;
                  
                  
                      }
                  

                  but i couldn't get it to show in the 3 boxes Don't be overcome by evil, but overcome evil with good

                  S Offline
                  S Offline
                  Sean89
                  wrote on last edited by
                  #8

                  I set up a program with the code you posted and it works fine. So I don't know what else to tell you :(

                  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