Ishan
Ishan

Reputation: 4028

working with dropdownlist in asp.net c#

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??

Upvotes: 2

Views: 1094

Answers (1)

bastijn
bastijn

Reputation: 5953

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;

Upvotes: 5

Related Questions