Hi
I have dropdownlist populated from database.My requirement is to count the number of records for each dropdownlist item like:
Toyota(15)
Honda(10)
BMW(20)

I have used

ddlMake.Items.Add(datatable.Rows.Count)

but it shows records in below format

Toyota
Honda
BMW
(15)
(10)
(20)

Hello there

You can do this with the resultset from your dataset source.
You can write the Group query to select eg:

select ProductID , ProductName and Value as Contactinate Productname + RowCount from yourtable

And then bind it to your DDL.

Mark as fixed if it helps you

Acutually im populating DDL from one table(CarMake) and counting no. of records from another table(CarSale) means how many cars are there for sale of a certain CarMake.

Hi there

Use the Query like this . The result is tested in 'Adventureworks' database

Select 
product.ProductID, 
product.Name, 
ProductInventory.Quantity,
product.Name+'('+ cast(ProductInventory.Quantity as nvarchar(5))+ ')' as Value
from Production.product 
inner join Production.ProductInventory
on ProductInventory.ProductID = product.ProductID

Then bind this result to your DDL.

Mark as solved if it helps you

select MakeName + '(' + cast((select count(*) from CarSale p where p.MakePK = m.PK) as varchar) + ')' dispField, m.PK
 from CarMake m

For MS SQL 2000 above code, assumes PK as primary key for join of CarSale and CarMake. Also assuming that you will need the PK as value member.
- PadteS

Acutually im populating DDL from one table(CarMake) and counting no. of records from another table(CarSale) means how many cars are there for sale of a certain CarMake.

Wow Padtes thats exactly what i was looking for.Thank u very much
Thanks Yousaf to u also for your valuable suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.