0

I have a datagridview where all the selected file names are displayed. Next to that column of file name(Dateinamen), there is another column for a new file name(Neue Dateinamen). User can type in new file name. How can I change the file names with the new ones which are typed in by the user?

Here is the code for listing the selected files

if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string[] dateiNamen = ofd.SafeFileNames;
                    for (int i = 0; i < ofd.FileNames.Count() - 1; i++)
                    {
                            dataGridView1.Rows.Add(dateiNamen[i]);
                    }

                }   

how the datagridview looks

1 Answer 1

1

At first i would recommend that you have to declare a database. The database could be a List where a FileObject has a structure like that:

public class FileObject
{
    public string FileName {get;set;}
    public string NewFileName {get; set;}
...
}

Next what you have to do, you have to check for the changes. Either you do it manually and you take e.g. the DataGridView.CellValueChanged event and you take the new value to update your List.

Or you use DataBinding and for that you use a DataSet or a DataTable. Tip: Don't forget the value checking of a value changing

Sign up to request clarification or add additional context in comments.

4 Comments

I declared a database as you advised, but I got the following error message: Rows cannot be programmatically added to the DataGridView rows collection when the control is data-bound.
If you use DataBinding, you add a complete new row to your databound object instead of the datagridview. The datagridview will add it "automatically"
Then, how can I list files on DGV using ofd, just like the code I posted? It doesn't work by defining a new class database.
Well, you have to "connect" the frontend (datagridview => visualization) with the backend (DataSource => your data). E.g. you can do that with a bindinglist. If you want to add a new row, you have to instanciate a new row and add the data you want to add

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.