Пример #1
0
        private void ExportXmlFile()
        {
            if (dataGrid.ColumnCount > 0)
            {
                using (var fileDialog = new SaveFileDialog())
                {
                    //openFileDialog.InitialDirectory = @"%HOMEPATH%";
                    fileDialog.Filter           = Resources.xmlfile_dialogextension;
                    fileDialog.FilterIndex      = 1;
                    fileDialog.RestoreDirectory = true;

                    if (fileDialog.ShowDialog() == DialogResult.OK)
                    {
                        var ds = (DataTable)dataGrid.DataSource;
                        // Save the contents of the dataGrid into the chosen file
                        ImpExpUtilities.ExportXML(ds, fileDialog.FileName);
                        currentOpenDocumentPath = fileDialog.FileName;                                   // Save to the exported file if future edits
                        newDocument             = false;                                                 // Set flag to false since the document was being exported to the disk
                        this.Text = Resources.title + '[' + Path.GetFileName(fileDialog.FileName) + ']'; // Change the Forms title to currentOpenDoc #10
                        MessageBox.Show(Resources.saveCurrent_success + currentOpenDocumentPath, Resources.success, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }
            }
            else
            {
                MessageBox.Show(Resources.saveEmptyTable_fail_msg, Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Пример #2
0
        private void ExportFile()
        {
            if (dataGrid.ColumnCount > 0)
            {
                using (var fileDialog = new SaveFileDialog())
                {
                    fileDialog.Filter           = Resources.supportedFiles_extension;
                    fileDialog.FilterIndex      = 1;
                    fileDialog.RestoreDirectory = true;

                    if (fileDialog.ShowDialog() == DialogResult.OK)
                    {
                        var fileSuccessfullyExported = false;
                        switch (Path.GetExtension(fileDialog.FileName)?.ToLower())
                        {
                        // Export the file depending on the chosen extension, !Export -> if export fails, return instead is to display err msg only
                        case ".xml":
                            fileSuccessfullyExported = ImpExpUtilities.ExportXML(dataGrid.DataSource as DataTable, fileDialog.FileName);
                            break;

                        case ".xlsx":
                            fileSuccessfullyExported = ImpExpUtilities.ExportXLS(dataGrid.DataSource as DataTable, fileDialog.FileName);
                            break;

                        case ".csv":
                            fileSuccessfullyExported = ImpExpUtilities.ExportCSV(dataGrid, fileDialog.FileName);
                            break;
                        }

                        if (fileSuccessfullyExported)
                        {
                            currentOpenDocumentPath = fileDialog.FileName;
                            this.Text = Resources.title + '[' + Path.GetFileName(currentOpenDocumentPath) + ']'; // Change the Forms title to currentOpenDoc #10
                            MessageBox.Show(Resources.saveCurrent_success + currentOpenDocumentPath, Resources.success, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        else
                        {
                            MessageBox.Show(string.Format(Resources.saveFile_fail_msg, fileDialog.FileName), Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show(Resources.saveEmptyTable_fail_msg, Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Пример #3
0
        private void SaveFile()
        {
            if (currentOpenDocumentPath != String.Empty)
            {
                if (!readOnly.Checked && !newDocument)
                {
                    var fileSuccessfullyExported = false;
                    switch (Path.GetExtension(currentOpenDocumentPath)?.ToLower())
                    {
                    // Save the file depending on the chosen extension, !Export -> if export fails, return instead is to display err msg only
                    case ".xml":
                        fileSuccessfullyExported = ImpExpUtilities.ExportXML(dataGrid.DataSource as DataTable, currentOpenDocumentPath);
                        break;

                    case ".xlsx":
                        fileSuccessfullyExported = ImpExpUtilities.ExportXLS(dataGrid.DataSource as DataTable, currentOpenDocumentPath);
                        break;

                    case ".csv":
                        fileSuccessfullyExported = ImpExpUtilities.ExportCSV(dataGrid, currentOpenDocumentPath);
                        break;
                    }
                    if (fileSuccessfullyExported)
                    {
                        MessageBox.Show(Resources.saveCurrent_success + currentOpenDocumentPath, Resources.success, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show(string.Format(Resources.saveFile_fail_msg, currentOpenDocumentPath), Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (readOnly.Checked && !newDocument)
                {
                    MessageBox.Show(Resources.saveXml_fail_msg, Resources.fail, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else if (newDocument || currentOpenDocumentPath?.Length == 0)
            {
                ExportFile();
            }
        }