Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Windows Forms, .NET 3.5
When I populate listView from Array, why first is empty?

I am trying to populate listView(win forms)control with folder content(file names) stored in an Array. Everything populates fine, but first
row remain empty. To delete first empty row I use:
listView1.Items.RemoveAt(0);
and that takes care of it.
Still, I really like to know why first row is empty?
Please someone enlighten me.
Here is the code:
string[] arrfilePathlist;
        // Select Folder/Directory
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            // Clear text box
            txtPath.Clear();
            txtPath.Text = String.Empty;
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.ShowNewFolderButton = false;
            folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            folderDialog.Description = "Select the directory that you want to use";
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = folderDialog.SelectedPath;
                // Use Dirctory.GetFiles method to get all files in directory
                // and, store in an array.
                arrfilePathlist = Directory.GetFiles(folderDialog.SelectedPath, "*.*");
                lblFileCount.Text = arrfilePathlist.Length.ToString();
                listView1.Items.RemoveAt(0);
                // Display items in the ListView control
                for(int i = 0; i < arrfilePathlist.Length; i++)
                {
                    listView1.Items.Add(Path.GetFileName(arrfilePathlist[i]));
                    //listView1.Items[0].SubItems.Add(Path.GetFileName(arrfilePathlist[i]));
                }
            }


Thanks,
Vijay
Posted

I have tried your code but I am getting error on this line

listView1.Items.RemoveAt(0);


if I remove its working fine.
so what is the problem I am not getting

what value you set for View Property ???
 
Share this answer
 
Thanks for answer,
View property is set to details.
Here is complete code:
C#
using System;
using System.Windows.Forms;
using System.IO;

namespace FolderContent
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            lblFileCount.Text = String.Empty;
        }
        string[] arrfilePathlist;
        // Select Folder/Directory
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            // Clear text box
            txtPath.Clear();
            txtPath.Text = String.Empty;
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.ShowNewFolderButton = false;
            folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
            folderDialog.Description = "Select the directory that you want to use";

            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = folderDialog.SelectedPath;

                // Use Dirctory.GetFiles method to get all files in directory
                // and, store in an array.
                arrfilePathlist = Directory.GetFiles(folderDialog.SelectedPath, "*.*");
                lblFileCount.Text = arrfilePathlist.Length.ToString();
                //uncomment next line to remove first empty row.
                //listView1.Items.RemoveAt(0);

                // Display items in the ListView control
                for(int i = 0; i < arrfilePathlist.Length; i++)
                {
                    listView1.Items.Add(Path.GetFileName(arrfilePathlist[i]));
                    //listView1.Items[0].SubItems.Add(Path.GetFileName(arrfilePathlist[i]));
                }
            }
            else
            { return; }
            
                // populate listview control with array.
        }
    }
}
 
Share this answer
 
Place this line after you have populated all items in the list view control.

C#
listView1.Items.RemoveAt(0);


You wont get the first row as empty...


Aravind R
 
Share this answer
 
v2
ListViews must start with a blank first row. Unless you are trying to build up a list of all browse requests, you should call ListView.Items.Clear before adding your new items, instead of your ad hoc hack to remove the first row (which might work the first time but it won't help you on subsequent browses).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900