Пример #1
0
        private void ShowFileDialog(List <DataGridViewCell> pathCells)
        {
            string filePath = pathCells[0].Value as string;

            if (string.IsNullOrWhiteSpace(filePath))
            {
                SetPathFileDialog.InitialDirectory = null;
                SetPathFileDialog.FileName         = "FileName";
            }
            else
            {
                SetPathFileDialog.InitialDirectory = Path.GetDirectoryName(filePath);
                SetPathFileDialog.FileName         = Path.GetFileName(filePath);
            }

            SetPathFileDialog.FileName = pathCells.Count <= 1 ? SetPathFileDialog.FileName : "FileName not used for multiple file path changes";

            if (SetPathFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string dir = Path.GetDirectoryName(SetPathFileDialog.FileName);

                Action <DataGridViewCell, string> funcUpdatePath = (DataGridViewCell selectedCell, string forcedFileName) =>
                {
                    string oldFileName  = Path.GetFileNameWithoutExtension(selectedCell.Value as string);
                    string oldExtension = Path.GetExtension(selectedCell.Value as string);

                    if (forcedFileName != null && string.IsNullOrWhiteSpace(oldFileName))
                    {
                        return;
                    }

                    string newPath = Path.Combine(dir, forcedFileName ?? oldFileName);
                    newPath = Path.ChangeExtension(newPath, oldExtension);

                    // change cell value, which triggers value changed event to update the export item
                    selectedCell.Value = newPath;
                };

                if (pathCells.Count > 1)
                {
                    foreach (DataGridViewCell selectedCell in pathCells)
                    {
                        funcUpdatePath(selectedCell, null);
                    }
                }
                else
                {
                    funcUpdatePath(pathCells[0], SetPathFileDialog.FileName);
                }
            }
        }
Пример #2
0
        private void btn_change_path_Click(object sender, EventArgs e)
        {
            if (ExportItemGridView.SelectedCells.Count <= 0)
            {
                return;
            }

            List <DataGridViewCell> pathCells = new List <DataGridViewCell>(ExportItemGridView.SelectedCells.Count);

            foreach (DataGridViewCell selectedCell in ExportItemGridView.SelectedCells)
            {
                DataGridViewCell matchingPathCell = selectedCell.OwningRow.Cells[ColumnFilePath.Index];
                if (pathCells.Contains(matchingPathCell))
                {
                    continue;
                }
                pathCells.Add(matchingPathCell);
            }

            if (pathCells.Count == 0)
            {
                return;
            }

            string firstSelectedCellPath = pathCells[0].Value as string;

            if (string.IsNullOrWhiteSpace(firstSelectedCellPath))
            {
                SetPathFileDialog.InitialDirectory = null;
                SetPathFileDialog.FileName         = "FileName";
            }
            else
            {
                SetPathFileDialog.InitialDirectory = Path.GetDirectoryName(firstSelectedCellPath);
                SetPathFileDialog.FileName         = Path.GetFileName(firstSelectedCellPath);
            }

            SetPathFileDialog.FileName = pathCells.Count <= 1 ? SetPathFileDialog.FileName : "FileName not used for multiple file path changes";

            if (SetPathFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string dir      = Path.GetDirectoryName(SetPathFileDialog.FileName);
                string filename = Path.GetFileNameWithoutExtension(SetPathFileDialog.FileName);

                Action <DataGridViewCell, string> funcUpdatePath = (DataGridViewCell selectedCell, string forcedFileName) =>
                {
                    string oldFileName  = Path.GetFileNameWithoutExtension(selectedCell.Value as string);
                    string oldExtension = Path.GetExtension(selectedCell.Value as string);

                    if (forcedFileName != null && string.IsNullOrWhiteSpace(oldFileName))
                    {
                        return;
                    }

                    string newPath = Path.Combine(dir, forcedFileName ?? oldFileName);
                    newPath = Path.ChangeExtension(newPath, oldExtension);

                    // change cell value, which triggers value changed event to update the export item
                    selectedCell.Value = newPath;
                };

                if (pathCells.Count > 1)
                {
                    foreach (DataGridViewCell selectedCell in pathCells)
                    {
                        funcUpdatePath(selectedCell, null);
                    }
                }
                else
                {
                    funcUpdatePath(pathCells[0], SetPathFileDialog.FileName);
                }
            }
        }