Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1199
  • Last Modified:

Add a blank entry to a dropdownlist filled by a dataset

I'm not sure how to add a blank listitem to the top of a dropdownlist that is filled with a strongly typed dataset. Here is what I am using to fill the dropdownlist.

Tried adding the following to the top but was overwritten.

ddlCategories.Items.Add(new ListItem("", "Select Category"));
dsForumCategoryTableAdapters.ForumCategoriesTableAdapter categoryTA = new dsForumCategoryTableAdapters.ForumCategoriesTableAdapter();
dsForumCategory.ForumCategoriesDataTable categoryDT = categoryTA.GetData();
 
ddlCategories.DataSource = categoryDT;
ddlCategories.DataValueField = categoryDT.Columns["CategoryID"].ToString();
ddlCategories.DataTextField = categoryDT.Columns["Title"].ToString();
ddlCategories.DataBind();

Open in new window

0
tristamane
Asked:
tristamane
  • 3
  • 2
1 Solution
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
have you tried adding the item after you have bound the data?

Another option is to create the query in a stored procedure that adds the dummy record "Select a Category" to the resultset.
0
 
tristamaneAuthor Commented:
The snippet attached works but I am using the CategoryID as the value and it requires an int value otherwise throws error.

System.ArgumentException: Input string was not in a correct format.Couldn't store <> in CategoryID Column.  Expected type is Int32.

Any suggestions to get around it? I need a blank value for client side validation.


DataRow row = categoryDT.NewRow();
row["CategoryID"] = "0";
row["ParentID"] = "0";
row["Title"] = "";
categoryDT.Rows.InsertAt(row, 0);

Open in new window

0
 
Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
Can you give it a negative value , say -1?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
tristamaneAuthor Commented:
That didn't work. The RequiredFieldValidator is not firing. I'm guessing the only way around is to create a CustomValidator with client side validation.
0
 
Saqib KhanSenior DeveloperCommented:

No need to add a dummy value via database commands.

vendorsLst.Items.Insert(0, New ListItem("Choose Vendor"))
where vendorsLst is name of control, 0 is the first position and replace "choose vendor" with your value.
0
 
tristamaneAuthor Commented:
Eggsalad, the following did the trick.
ddlCategories.Items.Insert(0, new ListItem("Select Category", ""));

Open in new window

0

Featured Post

Independent Software Vendors: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now