Click here to Skip to main content
Licence CPOL
First Posted 25 May 2011
Views 4,817
Downloads 447
Bookmarked 7 times

Upload and Download Files From DataGridView Cells

By | 25 May 2011 | Article
This post helps to upload any file into a datagridview cell and also download it from the datagridview cell.
Image.jpg

Introduction

This post helps to upload any file into a datagridview cell and also download it from the datagridview cell. In the attached project file, I have declared a model variable Dictionary<int, byte[]> _myAttachments which stores the attachments as Byte array, and key stores the rowIndex.

Dictionary<int, byte[]> _myAttachments = new Dictionary<int, byte[]>();

Select the attachment cell and click on the ‘Upload File’ button. It will pop up a FileDialog for the user to select the required file which is to be uploaded.

The following routine uploads the user selected file and shows the file name in the provided dataGridViewCell.

/// <summary>
/// Upload Attachment at the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void UploadAttachment(DataGridViewCell dgvCell)
{
    using (OpenFileDialog fileDialog = new OpenFileDialog())
    {
        //Set File dialog properties
        fileDialog.CheckFileExists = true;
        fileDialog.CheckPathExists = true;
        fileDialog.Filter = "All Files|*.*";
        fileDialog.Title = "Select a file";
        fileDialog.Multiselect = false;

        if (fileDialog.ShowDialog() == DialogResult.OK)
        {
            FileInfo fileInfo = new FileInfo(fileDialog.FileName);
            byte[] binaryData = File.ReadAllBytes(fileDialog.FileName);
            dataGridView1.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;

            if (_myAttachments.ContainsKey(dgvCell.RowIndex))
                _myAttachments[dgvCell.RowIndex] = binaryData;
            else
                _myAttachments.Add(dgvCell.RowIndex, binaryData);
        }
    }
}

The DownloadAttachment routine can be called on cell double click or the ‘Download’ button click event.

/// <summary>
/// Download Attachment from the provided DataGridViewCell
/// </summary>
/// <param name="dgvCell"></param>
private void DownloadAttachment(DataGridViewCell dgvCell)
{
    string fileName = Convert.ToString(dgvCell.Value);

    //Return if the cell is empty
    if (fileName == string.Empty)
        return;

    FileInfo fileInfo = new FileInfo(fileName);
    string fileExtension = fileInfo.Extension;

    byte[] byteData = null;

    //show save as dialog
    using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
    {
        //Set Save dialog properties
        saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
        saveFileDialog1.Title = "Save File as";
        saveFileDialog1.CheckPathExists = true;
        saveFileDialog1.FileName = fileName;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            byteData = _myAttachments[dgvCell.RowIndex];
            File.WriteAllBytes(saveFileDialog1.FileName, byteData);
        }
    }
}

License

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

About the Author

Balu Sathish

Software Developer
Calpine Technologies
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 25 May 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid