Default Filename SaveFileDialog
Asked Answered
P

5

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?

Pent answered 18/1, 2014 at 3:12 Comment(4)
You have two SaveFileDialog mySaveFileDialog (probably a component dropped on your form) and saveFile (built by program).Ralaigh
@RenaudBancelDAVEO i just use one when i run program...in this post i upoad 2 code for show i already try two of themPent
@GrantWinney winForms platformPent
nope...i already test it with "messagebox.show" it's have value....and when i try that with just simple string text like "test"...it's still not appear (i mean the "test", not dialog save box)Pent
E
30

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();
    }
Elo answered 18/1, 2014 at 3:16 Comment(8)
@GrantWinney - The link included in the answer shows otherwise.Elo
@GrantWinney - I always forget that Silverlight exists... I'll adjust my answer to consider real platformsElo
@Elo i can't fint "defaultFileName" property too..and yeah i use WinForms platformPent
@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.Elo
@Elo 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 variablePent
@katik - Where is mySaveDialog instantiated? It doesn't appear to be the same instance where you're setting the Filename property.Elo
@katik the issue might be in the indexing, please see my answer.Cruelty
I cannot see that property too: msdn.microsoft.com/en-us/library/…Scifi
C
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?

Cruelty answered 18/1, 2014 at 8:47 Comment(0)
S
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.

Scifi answered 12/11, 2015 at 11:47 Comment(0)
R
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!

Ralaigh answered 18/1, 2014 at 8:26 Comment(1)
.ShowDialog() should go after .FileName.Scifi
P
-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);

Permission answered 18/2, 2015 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.