如何在组合框中显示未重复的日期列的年份? [英] How can I display in a combobox the year of the date column that is not repeated?

查看:61
本文介绍了如何在组合框中显示未重复的日期列的年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框可以获得Date列的年份,但组合框显示列表中的所有年份,我只想显示2个不同的年份



示例



日期栏



11/16/2016

12/16/2016

1/12/2017

1/13/2017



组合框这样显示





2016

2016

2017

2017





我想要显示2016年和2017年



我该怎么办?

提前感谢您的回复:)



我尝试了什么:



i已尝试此代码,但它返回上面的示例

For i = 0 To index - 1

drDate(i)= dset.Tables(dr)。行(i).Item(0)

drNo(i)= dset.Tables( dr)。行(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))然后

cboYear.Items.Add(drDate(i) .ToString(yyyy))

结束如果

下一步

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

推荐答案

检查项目是否已经预设在组合框中的项目,请验证语法

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"))


而不是在代码中处理它在绑定到组合框之前,更好的方法是使用单独的查询从数据库中获取不同的年份,然后绑定到组合框。



否则你可以尝试在您的数据表下面的行查询,以获得不同的年份。



注意:我没有验证这一点,请告诉我,以防它无效。

var years =(from d in数据表,其中d.HasValue选择Convert.ToDateTime(d.Value)。年).Distinct();
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();


这篇关于如何在组合框中显示未重复的日期列的年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆