?
Solved

bind blank item to combo box

Posted on 2012-08-24
7
Medium Priority
?
533 Views
Last Modified: 2012-08-27
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

0
Comment
Question by:fwstealer
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38329920
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

0
 

Author Comment

by:fwstealer
ID: 38330115
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

0
 

Author Comment

by:fwstealer
ID: 38330648
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:fwstealer
ID: 38330689
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
0
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 38331880
Changed the code a bit for adding blank item to combobox.

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

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

Open in new window


In your code I guess this is wrong

cmbxFiles.SelectedValue == -1

it should be

cmbxFiles.SelectedIndex == -1
0
 

Author Comment

by:fwstealer
ID: 38332402
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
0
 
LVL 20

Expert Comment

by:informaniac
ID: 38332492
I don't think this is required. Is it?

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question