winforms 如何使用保存对话框来保存DataGridView单元格的单元格单击事件中的XML文件?

62lalag4  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(100)

我有一个datagrid视图,在其中一个单元格中,我必须提供下载文件选项。我正在动态创建XML文件,当用户单击下载按钮时,他/她应该能够这样做。
下面是我的代码:

private void DownloadFile(ScriptInfo scriptInfo)
        {
            XDocument doc =
                         new XDocument(
                           new XElement("scriptfilenames",
                               new XElement("SqlEye",
                                   new XElement("scriptfilename", new XAttribute("fileName", scriptInfo.FileName),
                                       new XElement("warnings",
                                        scriptInfo.ErrorMessages[RuleAction.Error].Select(x => new XElement("warning", new XAttribute("value", x)))),
                                        new XElement("remarks",
                                        scriptInfo.ErrorMessages[RuleAction.warning].Select(x => new XElement("remark", new XAttribute("value", x))))
                                        ))));

            string fileName = scriptInfo.FileName;
            FileInfo fileInfo = new FileInfo(fileName);
            string fileExtension = ".xml";

            byte[] byteData = null;

            //show save as dialog
            using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
            {
                //Set Save dialog properties
                saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
                saveFileDialog1.Title = "Save File as";
                saveFileDialog1.CheckPathExists = true;
                saveFileDialog1.FileName = fileName;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //what will be here????
                }
            }
           
        }
1dkrff03

1dkrff031#

如果我理解你的问题是正确的,这应该有所帮助:http://msdn.microsoft.com/en-us/library/sfezx97z.aspx检查文件名是否不为空,创建文件名并按照msdn上的示例写入数据。

4ioopgfo

4ioopgfo2#

我得到了它

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
                    doc.Save(saveFileDialog1.FileName);                   
}
ruoxqz4g

ruoxqz4g3#

试试这个,它会完美地工作。如果它是一个基于服务器的数据库,那么试试这个。

string s = cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value.ToString();
   File.WriteAllBytes(saveFileDialog1.FileName, byteData);
   byteData = System.Text.Encoding.ASCII.GetBytes(s);

如果你使用的是本地数据库,那么试试下面的代码。

SqlCeConnection cnn = new SqlCeConnection(Properties.Settings.Default.CncConnectionString);
                FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                byte[] SqlTypeBlob = File.ReadAllBytes(fileDialog.FileName);
                cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;
                SqlCeCommand cmd = new SqlCeCommand("INSERT INTO CncInfo (Drawings) VALUES (@DATA)", cnn);
                cmd.Parameters.Add("@DATA", SqlDbType.VarBinary);
                cmd.Parameters["@DATA"].Value = SqlTypeBlob;
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();

如果还有别的话,

byteData = _myAttachments[dgvCell.ColumnIndex];
  File.WriteAllBytes(saveFileDialog1.FileName, byteData);

如果这工作,请关闭这个,并给我一个给予批准点击我的答案。再见!!!!!!!

相关问题