---
title: "how to add item in listview control"  
description: "how to add item in listview control"  
author: "Anupam Mishra"  
published: 2016-01-17  
updated: 2016-01-18  
canonical: https://www.mindstick.com/forum/33883/how-to-add-item-in-listview-control  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 2 minutes  

---

# how to add item in listview control

Hi all,i want to use list [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view) to display [item](https://www.mindstick.com/forum/156564/remove-item-in-an-array-in-javascript) in [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) form. Please give me a suitable example to [display content](https://www.mindstick.com/forum/118/problem-in-display-content-of-one-page-to-another-page) [as dynamic](https://www.mindstick.com/forum/12679/change-event-not-work-as-dynamic-as-expected).thank you .

## Replies

### Reply by Anupam Mishra

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:


---

Original Source: https://www.mindstick.com/forum/33883/how-to-add-item-in-listview-control

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
