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?
The SaveFileDialog control prompts the user to select a location for saving a file and allows the user to specify the name of the file to save data. The SaveFileDialog control class inherits from the abstract class FileDialog.
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();
}
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With