Search code examples
c#datagridviewwindows-forms-designeropenfiledialog

How to display selected file names with openfiledialog on datagridview in WindowsForms C#


I set a list as the datasource of my DGV. I want to select files and then add names and formats of selected files under a certain column on DGV, adding rows for each file. I have googled so far, but all answers are for data which is given 'by the user', for example a person's name and age are given by the user. However, what I need is that the program reads information(name and format) without any input by the user.

Any help is welcomed and appreciated!

private void button2_Click(object sender, EventArgs e) //Dokument auswählen
{
 
   OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
            ofd.Multiselect = true;
            List<DateienList> dateienlist = new List<DateienList>();
            
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                   //Don't know what to do here.
                }
   }           

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {    
            BindingList<DateienList> myList = new BindingList<DateienList>();
            dataGridView1.DataSource = myList;  //Datasource set    
        }

class DateienList //datasource, the list
    {
        [DisplayName("Dateiennamen")]
        public string Dateiennamen { get; set; }
        [DisplayName("Neue Dateiennamen")]
        public string Neue_Dateiennamen { get; set; }
        [DisplayName("Anzahl Kopien")]
        public int Anzahl_Kopien { get; set; }
        [DisplayName("Umlaut Entfernen")]
        public bool Umlaut_Entfernen { get; set; }
        [DisplayName("PDF Erzeugen")]
        public bool PDF_Erzeugen { get; set; }
        [DisplayName("Wasserzeichen Hinzufügen")]
        public bool Waasserzeichen_Hinzufügen { get; set; }
    }
}

Solution

  • private void button2_Click(object sender, EventArgs e) //Dokument auswählen
            {
                //This should be done in Form load event
                BindingList<DateienList> myList = new BindingList<DateienList>();
                dataGridView1.DataSource = myList;  //Datasource set    
    
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
                ofd.Multiselect = true;
                List<DateienList> dateienlist = new List<DateienList>();
    
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //Loop the files selected from dialog, and find information here.
                    foreach (var item in ofd.FileNames)
                    {
                        DateienList tmp= new DateienList();
                        tmp.Dateiennamen = item;
                        FileInfo fileInfo = new FileInfo(item);
                        if (fileInfo.Extension.ToLower().Equals(".pdf"))
                        {
                            tmp.PDF_Erzeugen = true;
                            tmp.Umlaut_Entfernen = false;
                            tmp.Waasserzeichen_Hinzufügen = false;
                        } 
                        // check for other file types
                        myList.Add(tmp);
                    }
                }
                
            }