Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0
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 27-Feb-13 4:02am
Edited 27-Feb-13 4:54am
v2
Comments
BC @ CV at 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?
Darthillian at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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
 
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
  Permalink  
v2
Comments
Darthillian at 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 at 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 fileNames = new List();
foreach (string FileName in Filelistarray)
{
string filenameWithoutPath = Path.GetFileName(FileName);
fileNames.Add(filenameWithoutPath);
}
 
toolStripComboBox2.Items.AddRange(fileNames.ToArray());
}
 
Regards
Jegan
Darthillian at 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)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 PIEBALDconsult 110
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100