Reputation: 85
My datatable
contain column "Date" that have data like this in the following
'2013-01-01',
'2013-05-01',
'2014-01-01',
'2014-12-25',
'2014-12-26
But i want to get data something like 'select distinct year(Date)
', and bind selected data into dropdownlist
however, syntax error message popup: "Syntax error: Missing operand after 'year' operator.
"
can I get any support? THANKS!!!!
private DataTable BuildDT(string [] date)
{
DataTable dt = new DataTable("test");
dt.Columns.Add("RowID", typeof(Int16));
dt.Columns.Add("Date", typeof(DateTime));
for (int i = 0; i < date.Length; i++)
{
dt.Rows.Add(i+1,date[i]); // {1,2013-01-01}
}
return dt;
}
private void ddlGetData2(DataTable table)
{
table.DefaultView.RowFilter = "distinct year(Date)";
DropDownList1.DataSource = table;
DropDownList1.DataBind();
}
Upvotes: 0
Views: 79
Reputation: 11975
You can use the .ToTable()
on the associated DataView
instance.
var dv = dt.DefaultView;
var distinctDates = dv.ToTable(true, "Date");
Upvotes: 0
Reputation: 37520
You can do this with LINQ...
var years = table.AsEnumerable()
.Select(r => r.Field<DateTime>("Date").Year)
.Distinct()
.OrderBy(y => y)
.ToList();
Upvotes: 2