尝试为DataGridView控件自定义RadioButton单元格和列

时间:2014-08-22 18:17:50

标签: c# .net wpf winforms datagridview

我依靠http://msdn.microsoft.com/en-us/library/aa730882(vs.80).aspx在DataGridView控件的单元格中实现单选按钮的使用。但是,我在圈子里跑,因为我不确定我应该做什么。

到目前为止,我已按照链接中的说明设置了类,但如何实现这些方法?

    /// Represents the cell's current formatted value
    public virtual object EditingCellFormattedValue

    /// Keeps track of whether the cell's value has changed or not.
    public virtual bool EditingCellValueChanged

    /// Returns the current formatted value of the cell
    public virtual object GetEditingCellFormattedValue(DataGridViewDataErrorContexts context)

    /// Called by the grid when the cell enters editing mode. 
    public virtual void PrepareEditingCellForEdit(bool selectAll)

如果有更好的方法将单选按钮合并到DataGridViews或ListViews中,请告诉我们!否则,我真的很感激如何让这个自定义类工作的一些建议。

提前致谢!

修改

根据文档实现了我需要的东西,但它们显示为组合框。如何正确显示单选按钮?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Input;

namespace B3.CatapultProjectUserInterfaceKit.DialogsContribution
{
    public class DataGridViewRadioButtonColumn : DataGridViewColumn
    {
        private DataGridViewRadioButtonCell RadioButtonCellTemplate;

        public DataGridViewRadioButtonColumn()
            : base(new DataGridViewRadioButtonCell())
        {
        }

        public int MaxDisplayedItems
        {
            get
            {
                if (this.RadioButtonCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.RadioButtonCellTemplate.MaxDisplayedItems;
            }
            set
            {
                if (this.MaxDisplayedItems != value)
                {
                    this.RadioButtonCellTemplate.MaxDisplayedItems = value;
                    if (this.DataGridView != null)
                    {
                        DataGridViewRowCollection dataGridViewRows = this.DataGridView.Rows;
                        int rowCount = dataGridViewRows.Count;
                        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
                        {
                            DataGridViewRow dataGridViewRow = dataGridViewRows.SharedRow(rowIndex);
                            DataGridViewRadioButtonCell dataGridViewCell = dataGridViewRow.Cells[this.Index] as DataGridViewRadioButtonCell;
                            if (dataGridViewCell != null)
                            {
                                dataGridViewCell.MaxDisplayedItemsInternal = value;
                            }
                        }
                        this.DataGridView.InvalidateColumn(this.Index);
                        // TODO: Add code to autosize the column and rows, the column headers,
                        // the row headers, depending on the autosize settings of the grid.
                        // The DataGridView control does not expose a public method that takes care of this.
                    }
                }
            }
        }

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                DataGridViewRadioButtonCell dataGridViewRadioButtonCell = value as DataGridViewRadioButtonCell;
                if (value != null && dataGridViewRadioButtonCell == null)
                {
                    throw new InvalidCastException("Value provided for CellTemplate must be of type DataGridViewRadioButtonCell or derive from it.");
                }
                base.CellTemplate = value;
            }
        }
    }

    public class DataGridViewRadioButtonCell : DataGridViewComboBoxCell, IDataGridViewEditingCell
    {
        private int maxDisplayedItems;       // Caches the value of the MaxDisplayedItems property
        private object formattedValue;
        private bool valueChanged = false;

        public DataGridViewRadioButtonCell()
        {
            this.maxDisplayedItems = 1;
        }

        public int MaxDisplayedItems
        {
            get
            {
                return this.maxDisplayedItems;
            }
            set
            {
                if (value < 1 || value > 100)
                {
                    throw new ArgumentOutOfRangeException("MaxDisplayedItems");
                }
                this.maxDisplayedItems = value;

                if (this.DataGridView != null && !this.DataGridView.IsDisposed && !this.DataGridView.Disposing)
                {
                    if (this.RowIndex == -1)
                    {
                        // Invalidate and autosize column
                        this.DataGridView.InvalidateColumn(this.ColumnIndex);

                        // TODO: Add code to autosize the cell's column, the rows, the column headers 
                        // and the row headers depending on their autosize settings.
                        // The DataGridView control does not expose a public method that takes care of this.
                    }
                    else
                    {
                        // The DataGridView control exposes a public method called UpdateCellValue
                        // that invalidates the cell so that it gets repainted and also triggers all
                        // the necessary autosizing: the cell's column and/or row, the column headers
                        // and the row headers are autosized depending on their autosize settings.
                        this.DataGridView.UpdateCellValue(this.ColumnIndex, this.RowIndex);
                    }
                }
            }
        }

        internal int MaxDisplayedItemsInternal
        {
            set
            {
                Debug.Assert(value >= 1 && value <= 100);
                this.maxDisplayedItems = value;
            }
        }

        public override Type EditType
        {
            get
            {
                // Return null since no editing control is used for the editing experience.
                return null;
            }
        }

        public override object Clone()
        {
            DataGridViewRadioButtonCell dataGridViewCell = base.Clone() as DataGridViewRadioButtonCell;
            if (dataGridViewCell != null)
            {
                dataGridViewCell.MaxDisplayedItems = this.MaxDisplayedItems;
            }
            return dataGridViewCell;
        }

        protected override bool ContentClickUnsharesRow(DataGridViewCellEventArgs e)
        {
            Point ptCurrentCell = this.DataGridView.CurrentCellAddress;
            return ptCurrentCell.X == this.ColumnIndex &&
                   ptCurrentCell.Y == e.RowIndex &&
                   this.DataGridView.IsCurrentCellInEditMode;
        }

        /// Represents the cell's current formatted value
        public virtual object EditingCellFormattedValue
        {
            get { return formattedValue; }
            set { formattedValue = value; }
        }

        /// Keeps track of whether the cell's value has changed or not.
        public virtual bool EditingCellValueChanged
        {
            get { return valueChanged; }
            set { valueChanged = value; }
        }

        /// Returns the current formatted value of the cell
        public virtual object GetEditingCellFormattedValue(DataGridViewDataErrorContexts context)
        {
            return formattedValue;
        }

        /// Called by the grid when the cell enters editing mode. 
        public virtual void PrepareEditingCellForEdit(bool selectAll)
        {
        }
    }
}

0 个答案:

没有答案