Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

Upload and Download Files From DataGridView Cells

Rate me:
Please Sign up or sign in to vote.
4.30/5 (7 votes)
25 May 2011CPOL 50.1K   2.7K   12   11
This post helps to upload any file into a datagridview cell and also download it from the datagridview cell.

DataGridViewCells/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.

C#
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.

C#
/// <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.

C#
/// <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)


Written By
Software Developer UVJ Technologies
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAsk Pin
Member 1222198018-Jan-16 2:10
Member 1222198018-Jan-16 2:10 
QuestionHow could i populate this grid with database Pin
Mohammad Imran18-Feb-15 19:24
Mohammad Imran18-Feb-15 19:24 
QuestionDownload Attachment from WCF service Pin
kiquenet.com18-Nov-14 2:25
professionalkiquenet.com18-Nov-14 2:25 
QuestionHow to add attach file for my second and third column Pin
Member 1094597318-Jul-14 14:45
Member 1094597318-Jul-14 14:45 
AnswerRe: How to add attach file for my second and third column Pin
Balu Sathish21-Jul-14 3:39
Balu Sathish21-Jul-14 3:39 
GeneralRe: How to add attach file for my second and third column Pin
Member 1094597321-Jul-14 9:30
Member 1094597321-Jul-14 9:30 
GeneralRe: How to add attach file for my second and third column Pin
Balu Sathish21-Jul-14 19:26
Balu Sathish21-Jul-14 19:26 
GeneralRe: How to add attach file for my second and third column Pin
Member 1094597322-Jul-14 4:31
Member 1094597322-Jul-14 4:31 
Questiondatagried Pin
Member 101908545-Oct-13 15:26
Member 101908545-Oct-13 15:26 
QuestionCan we open the file from DataGridView? Pin
TAN TH19-Jul-12 22:44
TAN TH19-Jul-12 22:44 
GeneralMy vote of 3 Pin
shareque27-Jun-12 19:55
shareque27-Jun-12 19:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.