1. 程式人生 > >如何在DataGridView中加入自定義元件

如何在DataGridView中加入自定義元件

我想在DataGridView編輯時顯示如下圖控制元件:

(1)建立一個自定義元件UserControl,並繼承IDataGridViewEditingControl介面(用意:在DataGridView進入編輯狀態時可以呼叫該控制元件),新增如下程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Class.baseSys.controlSys
{

    public partial class UserControl1 : UserControl, IDataGridViewEditingControl
    {
        int rowIndex;
        DataGridView dataGridView;
        private bool valueChanged = false; 

        public UserControl1()
        {
           
            InitializeComponent();
        }

        /// <summary> 
        /// 更改控制元件的使用者介面 (UI),使之與指定單元格樣式一致。 
        /// </summary> 
        /// <param name="dataGridViewCellStyle">要用作使用者介面的模型的 System.Windows.Forms.DataGridViewCellStyle。</param> 
        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.textBox1.ForeColor = dataGridViewCellStyle.ForeColor;
            this.textBox1.BackColor = dataGridViewCellStyle.BackColor;
        }


        /// <summary> 
        /// 確定指定的鍵是應由編輯控制元件處理的常規輸入鍵,還是應由 System.Windows.Forms.DataGridView 處理的特殊鍵。 
        /// </summary> 
        /// <param name="key">一個 System.Windows.Forms.Keys,表示按下的鍵。</param> 
        /// <param name="dataGridViewWantsInputKey">當 System.Windows.Forms.DataGridView 要處理 keyData 中的 System.Windows.Forms.Keys時,則為 true;否則為 false。</param> 
        /// <returns>如果指定的鍵是應由編輯控制元件處理的常規輸入鍵,則為 true;否則為 false。</returns> 
        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch (keyData & 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;
            } 
        }

        /// <summary> 
        /// 檢索單元格的格式化值。 
        /// </summary> 
        /// <param name="context">System.Windows.Forms.DataGridViewDataErrorContexts 值的按位組合,它指定需要資料的上下文。</param> 
        /// <returns>一個 System.Object,表示單元格內容的格式化版本。</returns> 
        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        /// <summary> 
        /// 準備當前選中的單元格以進行編輯。 
        /// </summary> 
        /// <param name="selectAll">為 true,則選擇單元格的全部內容;否則為 false。</param> 
        public void PrepareEditingControlForEdit(bool selectAll)
        {
           
        }

        /// <summary> 
        /// 獲取或設定包含單元格的 System.Windows.Forms.DataGridView。 
        /// </summary> 
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
            } 
        }

        /// <summary> 
        /// 實現介面 IDataGridViewEditingControl.EditingControlFormattedValue  
        /// </summary> 
        public object EditingControlFormattedValue
        {
            get { return this.textBox1.Text;}
            set { this.textBox1.Text = value.ToString(); }
        }

        /// <summary>
        /// 增加屬性設定TextBox的值
        /// </summary>
        public String tetText
        {
            get { return this.textBox1.Text; }
            set { this.textBox1.Text = value.ToString(); }
        }

        /// <summary> 
        /// 獲取或設定該承載單元格的父行的索引。包含該單元格的行的索引,如果沒有父行,則為 -1。 
        /// </summary> 
        public int EditingControlRowIndex
        {
            get
            {
               
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            } 
        }


        /// <summary> 
        /// 獲取或設定一個值,該值指示編輯控制元件的值是否與承載單元格的值不同。 
        /// </summary> 
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        /// <summary> 
        /// 獲取當滑鼠指標位於 System.Windows.Forms.DataGridView.EditingPanel 上方但不位於編輯控制元件上方時所使用的游標。 
        /// </summary> 
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        /// <summary> 
        /// 獲取或設定一個值,該值指示每當值更改時,是否需要重新定位單元格的內容。 
        /// </summary> 
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }

        /// <summary> 
        /// 修改TextBox值時通知datagridviewCell.
        /// </summary> 
        /// <param name="eventargs"></param> 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

            //DataGridView中Cell的Changed事件  
            valueChanged = true; 
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true); 
       
        } 

       
    }
}

(2)增加控新地DataGridViewCell控制元件,使期在編輯時呼叫自定義元件

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Class.baseSys.controlSys
{
    /// <summary> 
    /// 選擇輸入控制元件
    /// </summary> 
    public class CalendarColumn : DataGridViewColumn 
    { 
        public CalendarColumn() : base(new CalendarCell()) 
        { 
        } 
        /// <summary> 
        /// 獲取或設定用於建立新單元格的模板。 
        /// </summary> 
        public override DataGridViewCell CellTemplate 
        { 
            get 
            { 
                return base.CellTemplate; 
            } 
            set 
            { 
                //確認單元格使用了自定義的日期單元格模版 
                if (value != null && !value.GetType().IsAssignableFrom(typeof(TetInputCell))) 
                { 
                    throw new InvalidCastException(/*Class.Properties.Resources.Message*/); 
                } 
                base.CellTemplate = value; 
            } 
        } 
    } 
    /// <summary> 
    /// 日期單元格 
    /// </summary> 
    public class TetInputCell : DataGridViewTextBoxCell
    {
        public TetInputCell()
            : base()
        {
            // 預設為短日期型 
            //this.Style.Format = "c";
        }
        /// <summary> 
        /// 初始化用於編輯單元格的控制元件。 
        /// </summary> 
        /// <param name="rowIndex">單元格所在位置的從零開始的行索引。</param> 
        /// <param name="initialFormattedValue">它表示在開始編輯時單元格顯示的值。</param> 
        /// <param name="dataGridViewCellStyle">表示單元格樣式的 System.Windows.Forms.DataGridViewCellStyle。</param> 
        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
           
            //初始化之後設定編輯狀態的值 
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            UserControl1 ctl = (UserControl1)DataGridView.EditingControl;
            //Value = "111";
            if (Value != null && Value != System.DBNull.Value)
                ctl.tetText = this.Value.ToString();
        }
        /// <summary> 
        /// 獲取單元格的寄宿編輯控制元件的型別。 
        /// </summary> 
        public override Type EditType
        {
            get
            {
                return typeof(UserControl1);
            }
        }
        /// <summary> 
        /// 獲取或設定單元格中值的資料型別。 
        /// </summary> 
        public override Type ValueType
        {
            get
            {
                return typeof(String);
            }
        }
        /// <summary> 
        /// 獲取新記錄所在行中單元格的預設值。 
        /// </summary> 
        public override object DefaultNewRowValue
        {
            get
            {
                return "";
            }
        }
    }

}

(3)程式呼叫

CustormerDgv dgView = null;