NEWS

[C#] Tìm kiếm dữ liệu từ ngày đến ngày giống phần mềm kế toán

[C#] Tìm kiếm dữ liệu từ ngày đến ngày giống phần mềm kế toán
Đăng bởi: Thảo Meo - Lượt xem: 10809 15:31:32, 12/04/2019DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách tính từ ngày đến ngày để tìm kiếm dữ liệu giống phần mềm kế toán trong lập trình C#.

Nếu bạn nào sử dụng phần mềm kế toán, chắc các bạn cũng thấy trong tất cả các báo cáo kho, nhập, xuất...

Thì thường thay vì chọn từ ngày đến ngày, phần mềm sẽ cho phép chúng ta chọn nhanh thêm các thông tin.

Ví dụ: như trong quý, năm, tháng...

Dưới đây là giao diện demo ứng dụng C#:

tim_kiem_ketoan

Đầu tiên, các bạn tạo cho mình một class: DateTimeExtensions.cs

Source code C#:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalcDateOfWeek
{
    public static class DateTimeExtensions
    {       
        public static DataTable GetDateOfWeek(this DateTime date)
        {
            var table = new DataTable();
            table.Columns.Add("id", typeof(int));
            table.Columns.Add("name", typeof(string));
            table.Columns.Add("from", typeof(DateTime));
            table.Columns.Add("to", typeof(DateTime));

            table.Rows.Add(1, "Hôm nay", DateTime.Now.Date, DateTime.Now.Date);
            table.Rows.Add(2, "Tuần này", date.FirstDayOfWeek(), date.LastDayOfWeek());
            table.Rows.Add(3, "Đầu tuần đến hiện tại", date.FirstDayOfWeek(), DateTime.Now.Date);
            table.Rows.Add(4, "Tháng này", new DateTime(date.Year, date.Month, 1), new DateTime(date.Year, date.Month, 1).LastDayOfMonth());
            table.Rows.Add(5, "Đầu tháng đến hiện tại", new DateTime(date.Year, date.Month, 1), DateTime.Now.Date);
            DateTime _quyfrom = DateTime.Now, _quyto = DateTime.Now;
            Get_Quarter(date, ref _quyfrom, ref _quyto);
            table.Rows.Add(6, "Quý này", _quyfrom, _quyto);
            table.Rows.Add(7, "Đầu quý đến hiện tại", _quyfrom, date.Date);
            table.Rows.Add(8, "Năm này", new DateTime(date.Year, 1, 1), new DateTime(date.Year, 12, 31));
            table.Rows.Add(9, "Đầu năm đến hiện tại", new DateTime(date.Year, 1, 1), DateTime.Now.Date);
            table.Rows.Add(10, "6 tháng đầu năm", new DateTime(date.Year, 1, 1), new DateTime(date.Year, 6, 30));
            table.Rows.Add(11, "6 tháng cuối năm", new DateTime(date.Year, 7, 1), new DateTime(date.Year, 12, 31));
            table.Rows.Add(12, "Tháng 1", new DateTime(date.Year,1, 1), new DateTime(date.Year, 1, 1).LastDayOfMonth());            
            table.Rows.Add(13, "Tháng 2", new DateTime(date.Year, 2, 1), new DateTime(date.Year, 2, 1).LastDayOfMonth());
            table.Rows.Add(14, "Tháng 3", new DateTime(date.Year, 3, 1), new DateTime(date.Year, 3, 1).LastDayOfMonth());
            table.Rows.Add(15, "Tháng 4", new DateTime(date.Year, 4, 1), new DateTime(date.Year, 4, 1).LastDayOfMonth());
            table.Rows.Add(16, "Tháng 5", new DateTime(date.Year, 5, 1), new DateTime(date.Year, 5, 1).LastDayOfMonth());
            table.Rows.Add(17, "Tháng 6", new DateTime(date.Year, 6, 1), new DateTime(date.Year, 6, 1).LastDayOfMonth());
            table.Rows.Add(18, "Tháng 7", new DateTime(date.Year, 7, 1), new DateTime(date.Year, 7, 1).LastDayOfMonth());
            table.Rows.Add(19, "Tháng 8", new DateTime(date.Year, 8, 1), new DateTime(date.Year, 8, 1).LastDayOfMonth());
            table.Rows.Add(20, "Tháng 9", new DateTime(date.Year, 9, 1), new DateTime(date.Year, 9, 1).LastDayOfMonth());
            table.Rows.Add(21, "Tháng 10", new DateTime(date.Year, 10, 1), new DateTime(date.Year, 10, 1).LastDayOfMonth());
            table.Rows.Add(22, "Tháng 11", new DateTime(date.Year, 11, 1), new DateTime(date.Year, 11, 1).LastDayOfMonth());
            table.Rows.Add(23, "Tháng 12", new DateTime(date.Year, 12, 1), new DateTime(date.Year, 12, 1).LastDayOfMonth());
            table.Rows.Add(24, "Quý 1", new DateTime(date.Year, 1, 1), new DateTime(date.Year, 4, 1).AddDays(-1));
            table.Rows.Add(25, "Quý 2", new DateTime(date.Year, 4, 1), new DateTime(date.Year, 7, 1).AddDays(-1));
            table.Rows.Add(26, "Quý 3", new DateTime(date.Year, 7, 1), new DateTime(date.Year, 10, 1).AddDays(-1));
            table.Rows.Add(27, "Quý 4", new DateTime(date.Year, 10, 1), new DateTime(date.Year, 12, 1).AddMonths(1).AddDays(-1));
            table.Rows.Add(28, "Hôm qua", date.AddDays(-1).Date, date.AddDays(-1).Date);
            table.Rows.Add(29, "Tuần trước", date.AddDays(-7).FirstDayOfWeek(), date.AddDays(-7).LastDayOfWeek());
            table.Rows.Add(30, "Tháng trước", date.AddMonths(-1).FirstDayOfMonth(), date.AddDays(-7).AddMonths(-1).LastDayOfMonth());
            table.Rows.Add(31, "Năm trước", new DateTime(date.AddYears(-1).Year, 1, 1), new DateTime(date.AddYears(-1).Year, 12, 31));
            table.Rows.Add(32, "Tuần sau", date.AddDays(7).FirstDayOfWeek(), date.AddDays(7).LastDayOfWeek());
            table.Rows.Add(33, "Tháng sau", date.AddMonths(1).FirstDayOfMonth(), date.AddMonths(1).LastDayOfMonth());
            table.Rows.Add(34, "Năm sau", new DateTime(date.AddYears(1).Year, 1, 1), new DateTime(date.AddYears(1).Year, 12, 31));
            return table;
        }

        public static void Get_Quarter(DateTime date, ref DateTime from, ref DateTime to)
        {
            int quarterNumber = (date.Month - 1) / 3 + 1;
            var firstDayOfQuarter = new DateTime(date.Year, (quarterNumber - 1) * 3 + 1, 1);
            from = new DateTime(date.Year, (quarterNumber - 1) * 3 + 1, 1);
            to = firstDayOfQuarter.AddMonths(3).AddDays(-1);
        }
        public static DateTime FirstDayOfWeek(this DateTime dt)
        {
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            var diff = dt.DayOfWeek - culture.DateTimeFormat.FirstDayOfWeek;
            if (diff < 0)
                diff += 7;
            return dt.AddDays(-diff + 1).Date;
        }

        public static DateTime LastDayOfWeek(this DateTime dt)
        {
            return dt.FirstDayOfWeek().AddDays(6);
        }

        public static DateTime FirstDayOfMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }

        public static DateTime LastDayOfMonth(this DateTime dt)
        {
            return dt.FirstDayOfMonth().AddMonths(1).AddDays(-1);
        }

        public static DateTime FirstDayOfNextMonth(this DateTime dt)
        {
            return dt.FirstDayOfMonth().AddMonths(1);
        }
    }
}

 

Cách sử dụng vào Gridview và combobox edit Winform

Code C# Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CalcDateOfWeek
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       

        private void Form1_Load(object sender, EventArgs e)
        {
            var table = DateTime.Now.GetDateOfWeek();
            dataGridView2.DataSource = table;
            comboBox1.DataSource = table;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";
            comboBox1.SelectedIndex = 15;
        }

        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView selectedValue = (DataRowView)comboBox1.SelectedItem;
            var from = selectedValue.Row["from"].ToString();
            var to = selectedValue.Row["to"].ToString();
            dateTimePicker1.Value = DateTime.Parse(from);
            dateTimePicker2.Value = DateTime.Parse(to);
        }
    }
}

 

THANKS FOR WATCHING!

DOWNLOAD SOURCE

Tags: datetime c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Tìm kiếm dữ liệu từ ngày đến ngày giống phần mềm kế toán
Đăng bởi: Thảo Meo - Lượt xem: 10809 15:31:32, 12/04/2019DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.