Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using openfiledialog in C# for opening a .csv file. Now I want to filter .csv files which starts with letter 'L'.
eg. If I open dialog box using opendialog with .csv filter, it shows me all .csv files in current folder. But I want to filter the .csv files which has name starts with letter 'L'.
Is it possible?

What I have tried:

C#
private void btmImport_Click(object sender, EventArgs e)
       {
           try
           {
               string filename = "";
               OpenFileDialog dialog = new OpenFileDialog();
               dialog.Title = "Open CSV File";
               dialog.Filter = "CSV Files (*.csv)|*.csv";
               if (dialog.ShowDialog() == DialogResult.OK)
               {
                   filename = dialog.FileName;
               }
               else
               {
                   return;
               }
               //code to load file in datatable.
               DataTable data = new DataTable();
               data = NewDataTable(filename, ",", true);
               grdDataImp.DataSource = data;
           }
           catch (Exception)
           {
               throw;
           }

       }
Posted
Updated 3-Apr-18 5:18am
Comments
Richard MacCutchan 3-Apr-18 11:08am    
You need to add the letter to the beginning of the "*.csv" filter.

Change your filter to
C#
dialog.Filter = "CSV Files (L*.csv)|L*.csv";
 
Share this answer
 
Try:
private void btmImport_Click(object sender, EventArgs e)
    {
    string filename = "";
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Title = "Open CSV File";
    dialog.Filter = "CSV Files (L*.csv)|L*.csv";
    if (dialog.ShowDialog() == DialogResult.OK)
        {
        filename = dialog.FileName;
        //code to load file in datatable.
        DataTable data = new DataTable();
        data = NewDataTable(filename, ",", true);
        grdDataImp.DataSource = data;
        }
    }
Catching all exceptions just to rethrow them isn't really needed...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900