Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Filename SaveFileDialog

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?

like image 999
Neversaysblack Avatar asked Jan 18 '14 03:01

Neversaysblack


People also ask

How do I save SaveFileDialog in VB net?

The SaveFileDialog control prompts the user to select a location for saving a file and allows the user to specify the name of the file to save data. The SaveFileDialog control class inherits from the abstract class FileDialog.


2 Answers

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();
    }
like image 198
M.Babcock Avatar answered Sep 22 '22 21:09

M.Babcock


The problem is that you need to use:

myDataGridView.SelectedCells[0].Value.ToString();

instead of

myDataGridView.SelectedCells[2].Value.ToString();

Until you don't select 3 or more cells with mouse or whatsoever. You can index like [2]

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

Does this work for you?

like image 39
Marek Avatar answered Sep 19 '22 21:09

Marek