Import text into ListView?

shah123's Avatar
Contributor
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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
shah123's Avatar
Contributor
just the file name of the text file into a single cell
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Ohh. Thats just inserting any simple string. Something like scheduleListView.Items.Add("FileName"); Where the string will be your filename.
shah123's Avatar
Contributor
I agree to your solution but i have more then one column in ListView. I want to add that file name into specific column.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Then add it as sub items.
shah123's Avatar
Contributor
How would you do that? Can you please code something if possible? Thanks,
shah123's Avatar
Contributor
I got the solution :
Code:
ListViewItem lvt = new ListViewItem(new String[] { subjTextbox.Text, textFile.Text, "", "" });
scheduleListView.Items.Add(lvt);
Thanks Shabbir
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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});
      }
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I was just late by 1 min.