Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

I'm trying to display some file names in a toolStripComboBox but sadly enough it isn't working.
It loads the file names in to the local database but there seems to be something wrong with the connection between the database and the DisplayMember.

string[] Filelistarray = Directory.GetFiles(@"C:\Users\a961835\Programming", "*.txt");
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName");
            foreach (string FileName in Filelistarray)
            {
                DataRow dr = dt.NewRow();
                string filenameWithoutPath = Path.GetFileName(FileName);
                dr["FileName"] = filenameWithoutPath;
                dt.Rows.Add(dr);
            }

            //toolStripComboBox.ComboBox.ValueMember = "FileName";
            toolStripComboBox1.ComboBox.DataSource = dt;
            toolStripComboBox1.ComboBox.DisplayMember = "FileName";



The dropdown menu combobox i'm using is from ButtonDropDown using Custom Control (ButtonDropDownMenu Control)[^]

The thing is that I added a menutStripItem and under that ToolStripItem I added a toolStripComboBox only when I try to display the File Names into the toolStripComboBox it s doesn't show anything.

When I try this with the normal (standard) combobox it works just fine.


I'm not sure what i'm doing wrong. So any idea is welcome.

Thanks in advance.

Darthillian
Posted
Updated 27-Feb-13 3:54am
v2
Comments
BC @ CV 27-Feb-13 9:17am    
I just pasted your code in my win form and it loads the file names to the combobox perfectly. I don't see any ADO code so can't help you with your database issues. Can you show more code and explain better what you're trying to do?
Nomardus 27-Feb-13 9:22am    
The thing is it works fine in a normal ComboBox, but I'm trying to use this in to a dropdown menu ComboBox.
Or is there a way that I can use a button control to get the combobox opening underneath the button so the user won't notice it is a combobox?

1 solution

Hi,
you are adding the row, then setting the row by name [FileName], which hasn't been added until the next line.

so if you change your code as follow:

C#
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
string filenameWithoutPath = Path.GetFileName(FileName);
dr["FileName"] = filenameWithoutPath;


======================= Correction =========================================

To add the items in the combobox inside the drop down menu

C#
public void SetFileNames()
{
    string[] Filelistarray = Directory.GetFiles(@"D:\Projects\VS_Test_Projects\WpfApplication1\InfragisticsTest\bin\Debug", "*.txt");
    List<string> fileNames = new List<string>();
    foreach (string FileName in Filelistarray)
    {
        string filenameWithoutPath = Path.GetFileName(FileName);
        fileNames.Add(filenameWithoutPath);
    }

    toolStripComboBox2.Items.AddRange(fileNames.ToArray());
}



Regards
Jegan
 
Share this answer
 
v2
Comments
Nomardus 27-Feb-13 9:42am    
This does exactly the same as the original code, I still don't get the FileName in the dropdown combobox.
Jegan Thiyagesan 27-Feb-13 10:09am    
My mistake , I didn't read it properly, that you said you have added the combobox inside the drop down menu,
try this one:

public void SetFileNames()
{
string[] Filelistarray = Directory.GetFiles(@"D:\Projects\VS_Test_Projects\WpfApplication1\InfragisticsTest\bin\Debug", "*.txt");
List<string> fileNames = new List<string>();
foreach (string FileName in Filelistarray)
{
string filenameWithoutPath = Path.GetFileName(FileName);
fileNames.Add(filenameWithoutPath);
}

toolStripComboBox2.Items.AddRange(fileNames.ToArray());
}

Regards
Jegan
Nomardus 27-Feb-13 10:17am    
Dude, so much thanks :D you solved it :D

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