Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/forums/c-sharp/)
-   -   Import text into ListView? (http://www.go4expert.com/forums/import-text-listview-t4117/)

shah123 2May2007 19:27

Import text into ListView?
 
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 2May2007 20:40

Re: Import text into ListView?
 
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 2May2007 20:43

Re: Import text into ListView?
 
just the file name of the text file into a single cell

shabbir 2May2007 20:58

Re: Import text into ListView?
 
Ohh. Thats just inserting any simple string. Something like scheduleListView.Items.Add("FileName"); Where the string will be your filename.

shah123 2May2007 20:59

Re: Import text into ListView?
 
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 2May2007 21:03

Re: Import text into ListView?
 
Then add it as sub items.

shah123 2May2007 21:06

Re: Import text into ListView?
 
How would you do that? Can you please code something if possible? Thanks,

shah123 2May2007 21:19

Re: Import text into ListView?
 
I got the solution :
Code:

ListViewItem lvt = new ListViewItem(new String[] { subjTextbox.Text, textFile.Text, "", "" });
scheduleListView.Items.Add(lvt);

Thanks Shabbir

shabbir 2May2007 21:20

Re: Import text into ListView?
 
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 2May2007 21:53

Re: Import text into ListView?
 
I was just late by 1 min.


All times are GMT +5.5. The time now is 06:54.