Winforms从datagridview中的文件夹加载文件,编辑名称并保存它们

时间:2015-07-31 19:14:13

标签: c# datagridview

我有大约20.000个文件我必须重命名,遗憾的是我无法自动化,因为我必须检查每个文件的内容并添加相应的名称。

但是我打算建立一个能够帮助我的胜利形式解决方案。

计划是:

  • 按钮选择文件夹(完成)
  • 将文件加载到DataGridView(已完成)
  • 在gridview(TO DO)中内联编辑文件名
  • 退出/退出/离开另一个项目时使用新名称保存当前项目(TO DO)

问题:

  • 如何在数据网格视图中编辑内联行?
  • 离开时要使用什么事件处理程序来保存当前文件?
  • 我如何知道编辑了哪个文件,以便可以使用新名称重命名/保存?

还有一件事。我不会在一天内编辑20.000个项目,所以我想我是否可以为已编辑的文件添加属性并将其标记为true,或类似的东西,所以第二天我可以继续左边的那些。

2 个答案:

答案 0 :(得分:0)

If you binded your DataGridView with an Object of your design, you can use the selectedRow.DataBoundItem, to modidy its properties.

Now on the question to what event to use, there are some, I would suggest, RowEnter (to enter EditMode), and RowLeave (to validate/Save).

For your last question, again, if you have your own object, you can manage a property to do so (like a dirty bit set on any setter called).

Maybe based on FileInfo lastModified, or matching a name pattern...

Hope this helps! :)

答案 1 :(得分:0)

If you have 2 properties, and you use the CellEndEdit and Cell BeginEdit actions in the DataGrid. I'm assuming you have the full file path in the cell with the below example. There are options on the Datagrid on how to start editing, but it should be enabled by default.

  public string FileOriginal { get; set; }
  public string  FileNew { get; set; }

  private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        FileNew = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
        File.Move(FileOriginal, FileNew);
    }

    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        FileOriginal = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

    }

This will Move(rename) your file to the new name. taking the old path and moving it to the new edited path. If you just have the file name in the field, I would make another column with the path, then combine them to the name.