none
Agenda of Contacts RRS feed

  • Question

  • Hello,

    I have to make an agenda of contacts. I had saved my data in a DataGridView. It contains the name, surname,phone number and the date of birth. How can I find all the contacts that I have saved in DataGridView who have birthday in the current day using threads and a button?

    Thank you in advance.

    Friday, May 10, 2019 8:54 PM

All replies

  • Hello,

    The correct way to start is to load data into a List<T> or a DataTable, in this case I have mocked up data in a List<T>. Many developers think to load data via DataGridView.Rows.Add but that is not proper, setup a DataSource as I have done below.

    public class Item
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime BirthDate { get; set; }
    }

    Mocked data is set to the DataGridView in form load. Button1 click event shows how to get birth dates for today.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                var itemList = new List<Item>() 
                {
    
                    new Item { FirstName = "Anne", LastName = "Smith", BirthDate = DateTime.Parse("5/1/2019") },
                    new Item { FirstName = "Bob", LastName = "Jones", BirthDate = DateTime.Parse("5/2/2019") },
                    new Item { FirstName = "Cathy", LastName = "Adams", BirthDate = DateTime.Parse("5/3/2019") },
                    new Item { FirstName = "Dan", LastName = "Gallagher", BirthDate = DateTime.Parse("5/4/2019") },
                    new Item { FirstName = "Ed", LastName = "White", BirthDate = DateTime.Parse("5/5/2019") },
                    new Item { FirstName = "Frank", LastName = "Lebow", BirthDate = DateTime.Parse("5/10/2019") },
                    new Item { FirstName = "George", LastName = "Henderson", BirthDate = DateTime.Parse("5/7/2019") },
                    new Item { FirstName = "Hank", LastName = "Bellow", BirthDate = DateTime.Parse("5/8/2019") },
                    new Item { FirstName = "Mary", LastName = "Lily", BirthDate = DateTime.Parse("5/9/2019") },
                    new Item { FirstName = "Jane", LastName = "Houser", BirthDate = DateTime.Parse("5/10/2019") },
                    new Item { FirstName = "Ken", LastName = "Hicks", BirthDate = DateTime.Parse("5/11/2019") },
                    new Item { FirstName = "Larry", LastName = "Payne", BirthDate = DateTime.Parse("5/12/2019") }
                };
    
                DataGridView1.DataSource = itemList;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var itemList = (List<Item>)DataGridView1.DataSource;
    
                var results = itemList.Where((item) => item.BirthDate.Date == DateTime.Now.Date).ToList();
    
                if (results.Count > 0)
                {
                    foreach (Item item in results)
                    {
                        Console.WriteLine($"{item.FirstName} {item.LastName}");
                    }
                }
                else
                {
                    Console.WriteLine("No birthdays today");
                }
            }
        }
    }
    
    In the case above there are two both days for today 5/10, tomorrow 1.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by BonnieBMVP Sunday, May 12, 2019 4:12 PM
    Friday, May 10, 2019 10:51 PM
    Moderator
  • Hi AndriannaTs,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 15, 2019 6:52 AM
    Moderator