2

i have 2 dropdownlist in asp.net c#, one has only two items in it('OK' 'PK') and the other one is bound to a database field and it has 158 items in it.

I want remove two Items from the second list when the user selects 'OK' and keep original list when user selects 'PK'.

How can i do it?? As i have bound the second list to a database is there any property which can hide some values in the list??

3
  • For the record, based upon your last sentence, you do not want to delete those two items from the database as well but only from the list, right? Commented Nov 15, 2010 at 12:05
  • This may be off topic, but 158 items? From a usability perspective that sounds a bit much for a dropdownlist. Of course, if it's for something like countries, everyone is doing it that way, but if it's for something less straight forward than that, it might be a bit much. Commented Nov 15, 2010 at 15:10
  • @ Marcus L i need to do it that way Commented Nov 15, 2010 at 15:48

1 Answer 1

5

First, dropdownlists have a remove option. Something like

ddl.Items.Remove(ddl.Items.FindByValue("value"));

Another way would be to just not select those items from the list. This refers to you question to "hide" database values after retrieving. I take you use DropDownLists something like below:

DropDownList1.DataSource =ds.Tables[0]; // you list of items retrieved from DB

If you only like to display a subset of this table just retrieve only those records you need from your database using whatever DB connection options you have. For instance after the user selected OK use something like pseudo code below:

// get full list
allItems = DB.getAllItems();
selectedItems = allItems.where(p => p.itemID != itemID1 || itemID2);
DropDownList1.DataSource = selectedItems;

And for the rest specify the rest of the required values to init the dropdown.

Or in other example code, something like:

SqlCommand ddSqlCommand = new SqlCommand("SELECT * FROM TableName 
                                WHERE ID <> ID1 AND ID <> ID2", ddSqlConnection);
ddSqlConnection.Open();
ddDR = ddSqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
DropDownList1.DataSource = ddDR;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.