Default Filename SaveFileDialog

Neversaysblack picture Neversaysblack · Jan 18, 2014 · Viewed 47k times · Source

I would like to create SaveFileDialog with default file name from value DataGridViewCells

So far I tried

private void buttonSave_Click(object sender, EventArgs e) 
{
    //first
    //mySaveFileDialog.FileName = myDataGridView.SelectedCells[2].Value.ToString();
    //second
    SaveFileDialog saveFile = new SaveFileDialog();
    saveFile.FileName = myDataGridView.SelectedCells[2].Value.ToString();
    saveFile.ShowDialog();
}

Can anyone help me solve this?

Answer

M.Babcock picture M.Babcock · Jan 18, 2014

The SaveFileDialog has a property intended for this purpose: DefaultFileName using Silverlight or FileName using .NET

Your (uncompilable) code from the question would become:

    private void buttonSave_Click(object sender, EventArgs e) 
    {
        SaveFileDialog mySaveFileDialog = new SaveFileDialog();
        //Silverlight
        mySaveFileDialog.DefaultFileName = myDataGridView.SelectedCells[2].Value.ToString();
        //.NET
        mySaveFileDialog.FileName = myDataGridView.SelectedCells[2].Value.ToString();
    }