怎麼給Datagridview裏面的控件添加事件

        //首先要建立EditingControl對象

        private DataGridViewComboBoxEditingControl AAADGVComboBox = null;
        private CalendarEditingControl AAADGVCalendar = null;

        //添加Datagridview事件EditingControlShowing
        private void BBBDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                if (BBBDGV.CurrentCell.OwningColumn.Name == "添加列的名字")
                {
                    //取得被表示的控件
                    this.AAADGVComboBox = (DataGridViewComboBoxEditingControl)e.Control;
                    // SelectedIndexChanged事件處理器追加
                    this.AAADGVComboBox.SelectedIndexChanged += new EventHandler(AAADGVComboBox_SelectedIndexChanged);

                }
            }
            if (e.Control is CalendarEditingControl)
            {
                if (BBBDGV.CurrentCell.OwningColumn.Name == "列的名字")
                {
                    this.AAADGVCalendar = (CalendarEditingControl)e.Control;
                    this.AAADGVCalendar.ValueChanged += new EventHandler(AAADGVCalendar_ValueChanged);
                }
            }
        }
        //CellEndEdit事件處理器
        private void BBBDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            //SelectedIndexChanged事件處理器刪除
            if (this.AAADGVComboBox != null)
            {
                this.AAADGVComboBox.SelectedIndexChanged -= new EventHandler(AAADGVComboBox_SelectedIndexChanged);
                this.AAADGVComboBox = null;
            }
            if (this.AAADGVCalendar != null)
            {
                this.AAADGVCalendar.ValueChanged -= new EventHandler(AAADGVCalendar_ValueChanged);
                this.AAADGVCalendar = null;
            }
        }
        //在DataGridView中表示的ComboBox的SelectedIndexChanged事件處理器
        private void AAADGVComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //表示被選擇的Item
            DataGridViewComboBoxEditingControl cb = (DataGridViewComboBoxEditingControl)sender;
            if (cb.SelectedItem.ToString() )
            {
                            }
            else if (cb.SelectedItem.ToString() )
            {
             }
        }
        private void AAADGVCalendar_ValueChanged(object sendar, EventArgs e)
        {
            try
            {
                if (BBBDGV.CurrentCell.OwningColumn.Name == "列名字")
                {
                    CalendarEditingControl ca = (CalendarEditingControl)sendar;
                    if (ca.Value.ToString() )
                    {
                     }
                }
            }
            catch { }
        }

其中AAA,BBB是datagridview的名字,其中要註冊CellEndEdit和EditingControlShowing事件,偵聽的具體操作在SelectedIndexChanged和ValueChanged裏面實現。

要是將Button添加到datagridview裏面的話:可以添加CellContentClick事件,代碼如下:

 private void DGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            try
            {
                if (CIndex == 9)
                {
                    if (this.DGV.CurrentRow.Cells[1].Value != null)
                    {
                    }
                    this.DGV.Rows.RemoveAt(this.DGV.CurrentRow.Index);

                }
                else if (CIndex == 10)
                {
                    this.DGV.CurrentRow.Cells[4].FormattedValue.ToString();
                  }
            }
            catch { }
        }

發佈了18 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章