Hi PSowmiya,
For Windows Froms
1. Set property Format of DateTimePicker control to Custom.
2. Set property CustomFormat to yyyy.
3. Set property ShowUpDown to true.
Check this example. Now please take its reference and correct your code.
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy";
dateTimePicker1.ShowUpDown = true;
dataGridView1.DataSource = GetData();
}
private void Filter(object sender, EventArgs e)
{
DataRow[] dr = GetData().Select("Year=" + dateTimePicker1.Value.Year);
if (dr.Length > 0)
{
dataGridView1.DataSource = dr.CopyToDataTable();
}
else
{
dataGridView1.DataSource = GetData().Clone();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Year",typeof(int)) });
dt.Rows.Add(1, "John Hammond", 2018);
dt.Rows.Add(2, "Mudassar Khan", 2017);
dt.Rows.Add(3, "Suzanne Mathews", 2017);
dt.Rows.Add(4, "Robert Schidner", 2018);
return dt;
}
}
VB.Net
Public Class Form1
Public Sub New()
InitializeComponent()
dateTimePicker1.Format = DateTimePickerFormat.Custom
dateTimePicker1.CustomFormat = "yyyy"
dateTimePicker1.ShowUpDown = True
dataGridView1.DataSource = GetData()
End Sub
Private Sub Filter(ByVal sender As Object, ByVal e As EventArgs) Handles btnFilter.Click
Dim dr As DataRow() = GetData().Select("Year=" & dateTimePicker1.Value.Year)
If dr.Length > 0 Then
dataGridView1.DataSource = dr.CopyToDataTable()
Else
dataGridView1.DataSource = GetData().Clone()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Year", GetType(Integer))})
dt.Rows.Add(1, "John Hammond", 2018)
dt.Rows.Add(2, "Mudassar Khan", 2017)
dt.Rows.Add(3, "Suzanne Mathews", 2017)
dt.Rows.Add(4, "Robert Schidner", 2018)
Return dt
End Function
End Class
Screenshot