1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Import text into ListView?

Discussion in 'C#' started by shah123, May 2, 2007.

  1. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have a listview control where i added some column headers. Now the query is when i select any text on the textbox i want to import on listview control under the specific column header. ????

    Currently its adding any selected text from textbox to listview control but under the first column header. e.g.

    Code:
    // Column Header Code
    private void Form1_Load(object sender, EventArgs e)
            {
                ListView scheduleListView=new ListView();
                scheduleListView.View = View.Details;
                colheader1 = new ColumnHeader();
                colheader1.Text = "Schedule Name";
                colheader1.Width = 100;
                scheduleListView.Columns.Add(colheader1);
                colheader2 = new ColumnHeader();
                colheader2.Text = "File Name";
                colheader2.Width = 100;
                scheduleListView.Columns.Add(colheader2);
                colheader3 = new ColumnHeader();
                colheader3.Text = "Start Time";
                colheader3.Width = 100;
                scheduleListView.Columns.Add(colheader3);
                colheader4 = new ColumnHeader();
                colheader4.Text = "End Time";
                colheader4.Width = 100;
                scheduleListView.Columns.Add(colheader4);
                scheduleListView.Sorting = SortOrder.Ascending;
                scheduleListView.GridLines = true;
    
            }
    
    // When button is clicked it should import the filename under file name column header?
    private void addBtn_Click(object sender, EventArgs e)
    {
    ListViewItem lvt = new ListViewItem();
    lvt = scheduleListView.Items.Add(textFile.Text);
    }
    [/code]
    Thanks, Please Advise
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    I could not get you. Do you mean to say the content of the txt file into the list view or just the content of the complete text file into a single cell.
     
  3. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    just the file name of the text file into a single cell
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Ohh. Thats just inserting any simple string. Something like scheduleListView.Items.Add("FileName"); Where the string will be your filename.
     
  5. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    I agree to your solution but i have more then one column in ListView. I want to add that file name into specific column.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Then add it as sub items.
     
  7. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    How would you do that? Can you please code something if possible? Thanks,
     
  8. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    I got the solution :
    Code:
    ListViewItem lvt = new ListViewItem(new String[] { subjTextbox.Text, textFile.Text, "", "" });
    scheduleListView.Items.Add(lvt);
    
    Thanks Shabbir
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Yes here comes the code.

    Code:
    private void PopulateListView()
    {
             // Create a new ListView control.
             ListView listView1 = new ListView();
    
             // Create three items and three sets of subitems for each item.
             ListViewItem item1 = new ListViewItem("item1",0);
    
             item1.SubItems.Add("1");
             item1.SubItems.Add("2");
             item1.SubItems.Add("3");
    
             ListViewItem item2 = new ListViewItem("item2",1);
    
             item2.SubItems.Add("4");
             item2.SubItems.Add("5");
             item2.SubItems.Add("6");
    
             ListViewItem item3 = new ListViewItem("item3",0);
    
             item3.SubItems.Add("7");
             item3.SubItems.Add("8");
             item3.SubItems.Add("9");
    
             //Add the items to the ListView.
             listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});
          }
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    I was just late by 1 min.
     
  11. shah123

    shah123 New Member

    Joined:
    Mar 27, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    But you are the star.. Thanks
     

Share This Page