控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

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

作者:佚名 出处:互联网 2010年08月27日 阅读:

给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;
            }
        }
    }
}

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统