Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > บทความจากสมาชิก > DateTimePicker ใน DataGridView C# จะพูดถึง การใช้ DateTimePicker ใน DataGridView ผมใช้เป็นประจำ



 
Clound SSD Virtual Server

DateTimePicker ใน DataGridView C# จะพูดถึง การใช้ DateTimePicker ใน DataGridView ผมใช้เป็นประจำ

DateTimePicker ใน DataGridView C# พอดีอยากลองเขียนบทความแบบบ้านๆจากความรู้บ้านๆ ดูครับ บทความแรกนี้(จะมีต่อไปอีกเรอะ 555)จะพูดถึง การใช้ dateTimePicker ใน DataGridView ที่ผมใช้เป็นประจำครับ เผื่อจะเป็นความรู้ให้กับบางท่านได้



DateTimePicker ใน DataGridView C#


สิ่งแรกที่จะต้องมีคือโค้ดนี้ครับ

Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DateTimeColumn
{
    #region _GridDateControl
    public class GridDateControl : DataGridViewColumn
    {

        public GridDateControl()
            : base(new CalendarCell())
        {
        }

        public override DataGridViewCell CellTemplate
        {
            get { return base.CellTemplate; }

            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if ((value != null) && !value.GetType().IsAssignableFrom(typeof(CalendarCell)))
                {
                    throw new InvalidCastException("Must be a CalendarCell");
                }
                base.CellTemplate = value;

            }
        }

    }

    public class CalendarCell : DataGridViewTextBoxCell
    {

        public CalendarCell()
        {
            
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            CalendarEditingControl ctl = (CalendarEditingControl)DataGridView.EditingControl;
            if ((!object.ReferenceEquals(this.Value, DBNull.Value)))
            {
                if ((this.Value != null))
                {
                    ctl.Value = (DateTime)this.Value;
                }
            }
        }

        public override Type EditType
        {
            // Return the type of the editing contol that CalendarCell uses.
            get { return typeof(CalendarEditingControl); }
        }

        public override Type ValueType
        {
            // Return the type of the value that CalendarCell contains.
            get { return typeof(DateTime); }
        }
    }

    class CalendarEditingControl : DateTimePicker, IDataGridViewEditingControl
    {

        private DataGridView dataGridViewControl;
        private bool valueIsChanged = false;
        private int rowIndexNum;
        public CalendarEditingControl()
        {
            this.Format = DateTimePickerFormat.Short;
        }

        public object EditingControlFormattedValue
        {
            get { return this.Value.ToShortDateString(); }
            set
            {
                if (value is String)
                {
                    this.Value = DateTime.Parse(Convert.ToString(value));
                }
            }
        }


        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {

            return this.Value.ToShortDateString();

        }


        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;

        }

        public int EditingControlRowIndex
        {

            get { return rowIndexNum; }
            set { rowIndexNum = value; }
        }


        public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
        {

            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:


                    return true;
                default:
                    return false;
            }

        }


        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.

        }

        public bool RepositionEditingControlOnValueChange
        {

            get { return false; }
        }


        public DataGridView EditingControlDataGridView
        {

            get { return dataGridViewControl; }
            set { dataGridViewControl = value; }
        }


        public bool EditingControlValueChanged
        {

            get { return valueIsChanged; }
            set { valueIsChanged = value; }
        }
        public Cursor EditingControlCursor
        {

            get { return base.Cursor; }
        }
        Cursor IDataGridViewEditingControl.EditingPanelCursor
        {
            get { return EditingControlCursor; }
        }

        protected override void OnValueChanged(EventArgs eventargs)
        {
            // Notify the DataGridView that the contents of the cell have changed.
            valueIsChanged = true;

            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnValueChanged(eventargs);
        }

    }
    #endregion
}

Rebuild Solution ซักรอบครับ จากนั้นก็เพิ่มคอลัมใน DataGridView จะพบ GridDateControl ในรายการ

222







   
Share
Bookmark.   

  By : TOR_CHEMISTRY
  Article : บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ
  Score Rating :
  Create Date : 2015-09-26
  Download : No files
Sponsored Links
ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่