简体   繁体   English

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

[英]Winforms load files from folder in datagridview, edit name and save them

I have about 20.000 files I have to rename, unfortunately is nothing I can automate because I have to check the content of each of them and add the appropriate name. 我必须重命名大约20.000个文件,但是我无法自动执行任何操作,因为我必须检查每个文件的内容并添加适当的名称。

However I am planning to build a win forms solution that will help me with that. 但是,我计划构建一个胜利表格解决方案,以帮助我解决这一问题。

Plan is: 计划是:

  • button to select the folder (done) 按钮选择文件夹(完成)
  • load files to DataGridView (done) 将文件加载到DataGridView(完成)
  • edit the file name inline within the gridview (TO DO) 在gridview中内联编辑文件名(要做)
  • when exiting/quiting/moving away to another item save the current one with the new name (TO DO) 退出/退出/移至另一项时,以新名称保存当前项(待办事项)

Questions: 问题:

  • how can I edit the rows inline in data grid view? 如何在数据网格视图中内联编辑行?
  • what event handler should I use when moving away to save the current file? 离开保存当前文件时应使用哪个事件处理程序?
  • how do I actually know what file was edited so it can be renamed/saved with new name? 我实际上如何知道编辑了什么文件,以便可以使用新名称重命名/保存文件?

And one more thing. 还有一件事。 I won't edit 20.000 items in one day, so I'm thinking if I can add an attribute to the file that was edited already and mark it as true, or something like that, so the next day I can continue with the left ones. 我不会在一天内编辑20.000个项目,所以我在考虑是否可以向已编辑的文件中添加一个属性并将其标记为true或类似的东西,因此第二天我可以继续向左那些。

If you binded your DataGridView with an Object of your design, you can use the selectedRow.DataBoundItem, to modidy its properties. 如果将DataGridView与设计的对象绑定,则可以使用selectedRow.DataBoundItem修改其属性。

Now on the question to what event to use, there are some, I would suggest, RowEnter (to enter EditMode), and RowLeave (to validate/Save). 现在,关于使用什么事件的问题,我建议使用RowEnter(进入EditMode)和RowLeave(验证/保存)。

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). 同样,对于最后一个问题,如果您有自己的对象,则可以管理一个属性来执行此操作(就像在任何调用的setter上设置脏位一样)。

Maybe based on FileInfo lastModified, or matching a name pattern... 也许基于FileInfo lastModified,或匹配名称模式...

Hope this helps! 希望这可以帮助! :) :)

If you have 2 properties, and you use the CellEndEdit and Cell BeginEdit actions in the DataGrid. 如果您有2个属性,并且在DataGrid中使用CellEndEdit和Cell BeginEdit操作。 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. Datagrid上有关于如何开始编辑的选项,但是默认情况下应启用它。

  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. 如果您仅在该字段中输入文件名,那么我将在路径中添加另一列,然后将其组合为名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM