ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Display only Year in Windows Form DateTimePicker and filter DataGridView using C# and VB.Net<p>Hi&nbsp;<a class="username" rel="PSowmiya">PSowmiya,</a></p> <p>For Windows Froms</p> <p>1. Set property <span style="text-decoration: underline;">Format</span> of DateTimePicker control to <span style="text-decoration: underline;">Custom</span>.</p> <p>2. Set property <span style="text-decoration: underline;">CustomFormat</span> to <span style="text-decoration: underline;">yyyy</span>.</p> <p>3. Set property <span style="text-decoration: underline;">ShowUpDown</span> to <span style="text-decoration: underline;">true</span>.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">C#</span></strong></p> <pre class="brush: csharp">public partial class Form1 : Form { public Form1() { InitializeComponent(); dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = &#34;yyyy&#34;; dateTimePicker1.ShowUpDown = true; dataGridView1.DataSource = GetData(); } private void Filter(object sender, EventArgs e) { DataRow[] dr = GetData().Select(&#34;Year=&#34; + dateTimePicker1.Value.Year); if (dr.Length &gt; 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(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Year&#34;,typeof(int)) }); dt.Rows.Add(1, &#34;John Hammond&#34;, 2018); dt.Rows.Add(2, &#34;Mudassar Khan&#34;, 2017); dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, 2017); dt.Rows.Add(4, &#34;Robert Schidner&#34;, 2018); return dt; } }</pre> <p><strong><span style="text-decoration: underline;">VB.Net</span></strong></p> <pre class="brush: vb">Public Class Form1 Public Sub New() InitializeComponent() dateTimePicker1.Format = DateTimePickerFormat.Custom dateTimePicker1.CustomFormat = &#34;yyyy&#34; 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(&#34;Year=&#34; &amp; dateTimePicker1.Value.Year) If dr.Length &gt; 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(&#34;Id&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Year&#34;, GetType(Integer))}) dt.Rows.Add(1, &#34;John Hammond&#34;, 2018) dt.Rows.Add(2, &#34;Mudassar Khan&#34;, 2017) dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, 2017) dt.Rows.Add(4, &#34;Robert Schidner&#34;, 2018) Return dt End Function End Class</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/z5VLvLQ.gif" alt="" width="370" height="186" /></span></strong></p>https://www.aspforums.net:443/Threads/344018/Display-only-Year-in-Windows-Form-DateTimePicker-and-filter-DataGridView-using-C-and-VBNet/https://www.aspforums.net:443/Threads/344018/Display-only-Year-in-Windows-Form-DateTimePicker-and-filter-DataGridView-using-C-and-VBNet/Sun, 09 Dec 2018 23:40:03 GMT