Default Filename SaveFileDialog

2019-01-26 07:41发布

问题:

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?

回答1:

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();
    }


回答2:

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?



回答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.



回答4:

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!



回答5:

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);