Visual Studio: Display A File List In A DataGridView Control

This example also shows how to extract an icon image from a file in a windows directory and display the icon in a DataGridView Control.  One advantage of displaying a list of files in the DataGridView Control, is that you can take advantage of the built in sort function and the automatic ability to Select All and Copy built into the DataGridView Control.  The functionality can be accessed by clicking ‘Ctl + A’
and ‘Ctl + C’.  After you have copied the list, you can paste into Excel or Notepad.

First Create a new Visual Studio C# Windows Application Solution.
Drag and Drop a DataGridView Control onto the form.
Right Click on the DataGridView Control and Click Properties.
Select ‘Docking’ in the Properties and select the middle square.
Click on the arrow on the DataGridView Control and Uncheck:

Enable Adding
Enable Deleting
Enable Editing

Display A Directory List In A DataGridView Control

Copy and paste the code below into the load event of the form:

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.RowHeadersVisible = false;

            DataGridViewImageColumn colIcon = new DataGridViewImageColumn();
            colIcon.Name = "colIcon";
            colIcon.HeaderText = "";
            colIcon.ImageLayout = DataGridViewImageCellLayout.Zoom;
            colIcon.DefaultCellStyle.SelectionBackColor = Color.White;
            colIcon.Width = 18;
            //colIcon.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns.Add(colIcon);

            DataGridViewColumn colHold = new DataGridViewTextBoxColumn();
            colHold.Name = "colFileName";
            colHold.HeaderText = "Name";
            colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
            colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns.Add(colHold);

            colHold = new DataGridViewTextBoxColumn();

            colHold.Name = "colFileSize";
            colHold.HeaderText = "Size";
            colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
            colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns.Add(colHold);

            colHold = new DataGridViewTextBoxColumn();

            colHold.Name = "colFileType";
            colHold.HeaderText = "Extension";
            colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
            colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns.Add(colHold);

            colHold = new DataGridViewTextBoxColumn();

            colHold.Name = "colDateModified";
            colHold.HeaderText = "Date Modified";
            colHold.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
            colHold.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns.Add(colHold);

            dataGridView1.RowTemplate.Height = 18;

            //Replace the directory below with a directory on your computer.
            string strDirLocal = @"C:\documents";
            int nRow = 0;
            string sFileName = "";
            decimal nFileSize = 0;
            string sExt = "";
            string sDateModified = "";

            if (System.IO.Directory.Exists(strDirLocal))
            {                
                foreach (string sPath in System.IO.Directory.GetFiles(strDirLocal, "*.*"))
                {
                    //http://msdn.microsoft.com/en-us/library/ms404308.aspx
                    Icon FileIcon = SystemIcons.WinLogo;
                    FileIcon = Icon.ExtractAssociatedIcon(sPath);
                    //Add the file to the TextBox, and remove the Path from the sPath string,
                    //leaving the file name.
                    dataGridView1.Rows.Add();
                    nRow = dataGridView1.Rows.Count - 1;
                    sFileName = sPath.Replace(strDirLocal + @"\", "");
                    sDateModified = System.IO.File.GetLastWriteTime(sPath).ToString();
                    //sFileSize = System.IO.File.ReadAllBytes(sPath).ToString();
                    nFileSize = System.IO.File.ReadAllBytes(sPath).Length;
                    nFileSize = nFileSize / 1024;
                    nFileSize = System.Math.Round(nFileSize);
                    if (nFileSize < 1)
                    {
                        nFileSize = 1;
                    }
                    if (System.IO.File.ReadAllBytes(sPath).Length == 0)
                    {
                        nFileSize = 0;
                    }
                    string[] sExtHold = sPath.Split('.');
                    sExt = sExtHold[sExtHold.Count()-1];
                    //http://www.codeproject.com/KB/grid/ImagePreviewDataGridView.aspx

                    dataGridView1.Rows[nRow].Cells[0].Value = FileIcon;                                        
                    dataGridView1.Rows[nRow].Cells[1].Value = sFileName;
                    dataGridView1.Rows[nRow].Cells[2].Value = nFileSize;
                    dataGridView1.Rows[nRow].Cells[3].Value = sExt;
                    dataGridView1.Rows[nRow].Cells[4].Value = sDateModified;                    
                }
            }

Please note, if a file is open in the directory, you will receive an error.  You should add error handling to account for this error.

Related Links:

http://msdn.microsoft.com/en-us/library/ms404308.aspx

http://www.codeproject.com/KB/grid/ImagePreviewDataGridView.aspx

Export To A Text File From A DataGridView Control

Create a HTML File From a DataGridView Control

Display A Text File In A DataGridView

Display A Directory List In A TextBox Control

Advertisements
About

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , , , ,
Posted in Visual Studio Code Examples

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: