Link to home
Start Free TrialLog in
Avatar of fwstealer
fwstealerFlag for United States of America

asked on

bind blank item to combo box

how do i bind a blank item to a combo box?

here is my code with what i do for the web but this doesn't work for desktop

protected void getFiles()
        {
            string[] filePaths = Directory.GetFiles(filePath, "*.xml");
            cmbxFiles.DataSource = filePaths;

            //ListItem itm = new ListItem(); //namespace web.ui
            //itm.Text = "";
            //itm.Value = "-1";
            //itm.Selected = true;
            //cmbxFiles.Items.Insert(0, itm);
            //cmbxFiles.SelectedIndex = 0;
        }

Open in new window

Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

You cannot insert items when you have set the DataSource property in WIndows APplication.

http://msdn.microsoft.com/en-us/library/x8160f6f(v=vs.90).aspx

You need to do something alternative.
string[] blank = new string[]{""};
string[] filePaths = Directory.GetFiles(filePath, "*.xml");

string[] strDataSource = blank.Union(filePaths);

cmbxFiles.DataSource = strDataSource;

Open in new window

Avatar of fwstealer

ASKER

Error	1	Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string[]'. An explicit conversion exists (are you missing a cast?)

string[] strDataSource = blank.Union(filePaths);

Open in new window

I did something different:

 string[] filePaths = Directory.GetFiles(filePath, "*.xml");
            cmbxFiles.DataSource = filePaths;
            cmbxFiles.SelectedIndex = -1;

Open in new window


now nothing is selected - works fine
well may not when the user makes a selection like:

private void cmbxFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbxFiles.SelectedValue == "-1")
            {
                cmbxFiles.Focus();
                pnlPic.Visible = false;
            }
            else
            {
                pnlPic.Visible = true;
                getPic();
            }
        }

        protected void getPic()
        {
            MessageBox.Show(cmbxFiles.SelectedValue.ToString());
        }

Open in new window


crashes when i launch the program - not good
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i did the following:

protected void getFiles()
        {
            string[] blank = new string[] { "" };
            string[] filePaths = Directory.GetFiles(filePath, "*.xml");

            cmbxFiles.DataSource = blank.Union(filePaths).ToArray();
            cmbxFiles.SelectedIndex = 0;                                             
        }

        private void cmbxFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbxFiles.SelectedValue == "0" || cmbxFiles.SelectedValue == "-1" || cmbxFiles.SelectedValue == "")
            {
                //do nothing
                cmbxFiles.Focus();
                pnlPic.Visible = false;
            }
            else
            {
                //go get value of xml file
                pnlPic.Visible = true;
                getPic();
            }
        }

Open in new window


so i think this works
I don't think this is required. Is it?

cmbxFiles.SelectedValue == "0" || cmbxFiles.SelectedValue == "-1"