23

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?

Surya Matadewa
  • 1,017
  • 5
  • 19
  • 38

5 Answers5

29

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();
    }
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • 1
    @GrantWinney - The link included in the answer shows otherwise. – M.Babcock Jan 18 '14 at 03:19
  • @GrantWinney - I always forget that Silverlight exists... I'll adjust my answer to consider real platforms – M.Babcock Jan 18 '14 at 03:29
  • 1
    @M.Babcock i can't fint "defaultFileName" property too..and yeah i use WinForms platform – Surya Matadewa Jan 18 '14 at 03:30
  • 1
    @katik - Can you describe what __failed__ means here? The property you show is the right one for Winforms so understanding what is happening is important. – M.Babcock Jan 18 '14 at 03:31
  • @M.Babcock that 2 code i used above give no file name...just like that code not have any effect...i try to set it from property windows "mySaveFileDialog"...it's run well if i just use static string like 'test' default file name....but i kinda find hard when it's from variable – Surya Matadewa Jan 18 '14 at 03:36
  • @katik - Where is `mySaveDialog` instantiated? It doesn't appear to be the same instance where you're setting the `Filename` property. – M.Babcock Jan 18 '14 at 03:38
  • @katik the issue might be in the `indexing`, please see my answer. – Marek Jan 18 '14 at 08:50
  • I cannot see that property too: https://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog_properties(v=vs.110).aspx – Darius Miliauskas Nov 11 '15 at 17:30
4

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?

Marek
  • 3,555
  • 17
  • 74
  • 123
3

Your code should look the following way:

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

Use FileName but set the filename before showing the dialog.

Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
2

Please, try this in a simple WinForm application :

    static void Main()
    {
        var saveFile = new SaveFileDialog();
        saveFile.FileName = "myfile.txt";
        saveFile.ShowDialog();
        string fileName = saveFile.FileName ;
        MessageBox.Show(fileName);
    }

It works!

Renaud Bancel
  • 853
  • 6
  • 5
-3

to print all the controls in panel

public Bitmap MemoryImage;
    public void GetPrintArea( Panel pn1)
    {      
        MemoryImage = new Bitmap(panel13.Width, pn1.Height);
        pn1.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pn1.Width, pn1.Height));
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (MemoryImage != null)
        {
            e.Graphics.DrawImage(MemoryImage, 0, 0);
            base.OnPaint(e);
        }
    }
    void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
    {




        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.panel13.Width / 2), this.panel13.Location.Y);


    }


        Bitmap bmp = new Bitmap(MemoryImage.Width, MemoryImage.Height);
        panel13.DrawToBitmap(bmp, panel13.Bounds);

        saveFileDialog1.ShowDialog();
        saveFileDialog1.Title = "Save";
        saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";

        bmp.Save(saveFileDialog1.FileName);

sniffi
  • 125
  • 2
  • 15