sandy
sandy

Reputation: 31

c# Windows application add file into datagrid control

I want to search all files in particular folder and add those files into data grid view in C#. I have two text box and two buttons. The first button browse the folder and textbox1 select folder path and textbox2 want search all the doc files click on second button and add the files into datagridview. plz help me.

Upvotes: 2

Views: 1893

Answers (2)

Raj
Raj

Reputation: 29

 OpenFileDialog openFileDialog = new OpenFileDialog();
                    openFileDialog.Multiselect = true;
                    openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
                    openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                    if(openFileDialog.ShowDialog() == true)
                    {
                            foreach(string filename in openFileDialog.FileNames)
                                    datagrid.Items.Add(Path.GetFileName(filename));
                    }

Upvotes: 0

RQDQ
RQDQ

Reputation: 15569

Here is my guess at what you're looking for:

//Assume that textbox1 has folder path
DataGridView1.DataSource = Directory.GetFiles(textbox1.Text);

Upvotes: 2

Related Questions