Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a combobox that gets the year of the column "Date", but the combo box displays all the year that is in the list,i just want to display the 2 different year

example

Date Column

11/16/2016
12/16/2016
1/12/2017
1/13/2017

the combobox displays like this


2016
2016
2017
2017


all i want to display the 2016 and 2017

how can i do that??
Thank you in advance for the response:)

What I have tried:

i have tried this code but it returns the above example
For i = 0 To index - 1
drDate(i) = dset.Tables("dr").Rows(i).Item(0)
drNo(i) = dset.Tables("dr").Rows(i).Item(1).ToString.Trim
credit(i) = CDbl(dset.Tables("dr").Rows(i).Item(2))
strDRdate(i) = drDate(i).ToString("MM/dd/yyyy")
If (strDRdate(i) = drDate(count).ToString("yyyy")) Then
cboYear.Items.Add(drDate(i).ToString("yyyy"))
End If
Next
Posted
Updated 19-Jul-17 17:20pm

check if the item is already preset in the combo box Items, Please verify the syntax
if(<>cboYear.Items.Contains(drDate(i).ToString("yyyy")) then
               cboYear.Items.Add(drDate(i).ToString("yyyy"))
 
Share this answer
 
Instead of handling it in code behind before binding to combo box, the better approach is to use a separate query to get distinct years from the database and then bind to combo box.

Otherwise you can try below line query on your datatable to get distinct years.

Note: I have not verified this, just let me know in case it does not work.
var years = (from d in datatable where d.HasValue select Convert.ToDateTime(d.Value).Year).Distinct();
 
Share this answer
 
Comments
Atlapure Ambrish 19-Jul-17 23:32pm    
d.value in above query is your date column in datatable you are using.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900