给datagridview添加一个进度条类型的可以拖拽列

1.新建一个类:(完整代码)

using System;

using System.ComponentModel;

using System.Collections.Generic;

using System.Diagnostics;

using System.Text;

using System.Windows.Forms;

using System.Drawing;



namespace winclient

{

    public partial class progressEdit:TrackBar,IDataGridViewEditingControl

    {

        private DataGridView datagrid;

        private bool isChange = false;

        private int rowindex;



        public progressEdit():base()

        {

            InitializeComponent();

            this.Minimum = 0;

            this.Maximum = 100;

        }



        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)

        {

            if (Value == 100)

                BackColor = Color.Green;

            else

                BackColor = dataGridViewCellStyle.BackColor;

        }



        public DataGridView EditingControlDataGridView

        {

            get

            {

                return datagrid;

            }

            set

            {

                datagrid = value;

            }

        }



        public object EditingControlFormattedValue

        {

            get

            {

                return Value;

            }

            set

            {

                this.Value=(int)value;

            }

        }



        public int EditingControlRowIndex

        {

            get

            {

                return rowindex;

            }

            set

            {

                rowindex = value;

            }

        }



        public bool EditingControlValueChanged

        {

            get

            {

                return isChange;

            }

            set

            {

                isChange = value;

            }

        }



        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:

                    return true;

                default:

                    return false;

            }

        }



        public Cursor EditingPanelCursor

        {

            get { return base.Cursor; }

        }



        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)

        {

            return EditingControlFormattedValue;

        }



        public void PrepareEditingControlForEdit(bool selectAll)

        {

            

        }



        public bool RepositionEditingControlOnValueChange

        {

            get { return false; }

        }



        protected override void OnValueChanged(EventArgs e)

        {

            isChange = true;

            EditingControlDataGridView.NotifyCurrentCellDirty(true);

            base.OnValueChanged(e);

        }



        private void initComponent()

        {

            ((ISupportInitialize)(this)).BeginInit();

            this.SuspendLayout();

            this.AutoSize = false;

            this.Dock = DockStyle.Fill;

            this.TickStyle = TickStyle.TopLeft;

            ((ISupportInitialize)(this)).EndInit();

            this.ResumeLayout(false);

        }

    }

}



 

2.定义单元格:(完整代码)

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;



namespace winclient

{

    class progresscell:DataGridViewTextBoxCell

    {

        private progressEdit pre;



        public progresscell(): base()        

        { }



        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)

        {

            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            pre = DataGridView.EditingControl as progressEdit;

            pre.Value = Convert.ToInt32(this.Value);

        }



        public override Type EditType

        {

            get

            {

                return typeof(progressEdit);

            }

        }



        public override Type ValueType

        {

            get

            {

                return typeof(int);

            }

        }



        public override Type FormattedValueType

        {

            get

            {

                return typeof(int);

            }

        }



        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)

        {

            return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context).ToString();

        }



        protected override object GetValue(int rowIndex)

        {

            int value = (int)base.GetValue(rowIndex);

            return value;

        }



        public override object DefaultNewRowValue

        {

            get

            {

                return 0;

            }

        }

    }

}



 

3.定义列:(完整代码)

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;



namespace winclient

{

    class progresscolumn:DataGridViewColumn

    {

        public progresscolumn(): base(new progresscell())

        { }



        public override DataGridViewCell CellTemplate

        {

            get

            {

                return base.CellTemplate;

            }

            set

            {

                if (value != null && !value.GetType().IsAssignableFrom(typeof(progresscell)))

                    throw new InvalidCastException("列中只能使用滑动条单元格");

                base.CellTemplate = value;

            }

        }

    }

}

你可能感兴趣的:(datagridview)