简体   繁体   中英

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.

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)
  • edit the file name inline within the gridview (TO DO)
  • 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.

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! :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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