fwstealer
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
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;
}
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);
ASKER
I did something different:
now nothing is selected - works fine
string[] filePaths = Directory.GetFiles(filePath, "*.xml");
cmbxFiles.DataSource = filePaths;
cmbxFiles.SelectedIndex = -1;
now nothing is selected - works fine
ASKER
well may not when the user makes a selection like:
crashes when i launch the program - not good
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());
}
crashes when i launch the program - not good
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i did the following:
so i think this works
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();
}
}
so i think this works
I don't think this is required. Is it?
cmbxFiles.SelectedValue == "0" || cmbxFiles.SelectedValue == "-1"
cmbxFiles.SelectedValue == "0" || cmbxFiles.SelectedValue == "-1"
http://msdn.microsoft.com/en-us/library/x8160f6f(v=vs.90).aspx
You need to do something alternative.
Open in new window