默认文件名SaveFileDialog

时间:2014-01-18 03:12:42

标签: c# .net savefiledialog

我想从值SaveFileDialog创建default file name DataGridViewCells

到目前为止,我试过

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

任何人都可以帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:17)

SaveFileDialog有一个用于此目的的属性:DefaultFileName使用Silverlight或FileName使用.NET

问题中的(不可编译的)代码将变为:

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

答案 1 :(得分:3)

问题是您需要使用:

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

而不是

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

直到您不使用鼠标或任何方式选择3个或更多细胞。您可以像[2]

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

这对你有用吗?

答案 2 :(得分: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.

答案 3 :(得分:1)

请在简单的WinForm应用程序中尝试:

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

有效!

答案 4 :(得分:-1)

打印面板中的所有控件

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