HOW TO ADD ITEM IN LISTVIEW CONTROL

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
C#  .NET 
C#
 543  View(s)
Ratings:
Rate this:
Hi all,
i want to use list view  to display item in windows form. Please give me a suitable example to display content as dynamic.
thank you .
  1. Anupam Mishra

    Post:135

    Points:949
    Re: how to add item in listview control

    I have a listview in C# with four columns and add an item with a specified directory (i.e. c or other drive). I have done it like this and it seems to work:

    public Form1()
            {
                InitializeComponent();
                listView1.View = View.Details;
                listView1.GridLines = true;
                listView1.FullRowSelect = true;
                      // for adding column in listview 
                listView1.Columns.Add("Index", 60);
                listView1.Columns.Add("File Name", 300);
                listView1.Columns.Add("Size", 100);
                listView1.Columns.Add("Readonly", 70);
                int i = 1;
                  // Get all text files of specified directory to add a listview
                string[] filePaths = Directory.GetFiles(@"D:\\Anupam Mishra" "*.txt",         SearchOption.AllDirectories);
                foreach (var item in filePaths)
                {
                    long len = new FileInfo(item.ToString()).Length;
                    String name = new FileInfo(item.ToString()).Name;
                  // for adding item in listview 
                    ListViewItem _item = new ListViewItem();
                    _item.Text = i.ToString();
                    _item.SubItems.Add(name);
                    _item.SubItems.Add(BytesToString(len));
                    _item.SubItems.Add(filePaths.IsReadOnly.ToString());
                     // adding item in listview
                    listView1.Items.Add(_item);
                    i++;
                }
            }

     for checking the file size we create a new function BytesToString(double) as below:

    It returns total size and unit of file size. 

      static String BytesToString(long byteCount)
            {
                string[] _unit = { "Byte", "KB", "MB", "GB", "TB", "PB", "EB" };
                if (byteCount == 0)
                    return "0" + _unit [0];
                long bytes = Math.Abs(byteCount);
                int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
                double num = Math.Round(bytes / Math.Pow(1024, place), 1);
                return (Math.Sign(byteCount) * num).ToString() + " "+_unit [place];
            }

    It runs where user click on the button and display current selected row data. Code as follow:

    private void button1_Click(object sender, EventArgs e)
            {
                string _filePath = null;
                string Size = null;
                string ro = null;
                try
                {
                  // When we selected any row
                    _filePath =listView1.SelectedItems[0].SubItems[1].Text;
                    Size =listView1.SelectedItems[0].SubItems[2].Text;
                    ro =listView1.SelectedItems[0].SubItems[3].Text;
                    MessageBox.Show(_filePath + "," + Size + "," + ro);
                }
                catch (Exception)
                {
                    MessageBox.Show("Please select the atleast one file");
                }
            }

    Output:

      Modified On Apr-11-2018 12:27:39 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!