Default Filename SaveFileDialog

前端 未结 5 2153
时光取名叫无心
时光取名叫无心 2020-12-18 19:39

I would like to create SaveFileDialog with default file name from value DataGridViewCells

So far I tried

priva         


        
相关标签:
5条回答
  • 2020-12-18 20:16

    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?

    0 讨论(0)
  • 2020-12-18 20: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();
        }
    
    0 讨论(0)
  • 2020-12-18 20:30

    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!

    0 讨论(0)
  • 2020-12-18 20:36

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

    0 讨论(0)
  • 2020-12-18 20:38

    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.

    0 讨论(0)
提交回复
热议问题