自定义c#gridview和grid列 [英] customizing c# gridview and grid column

查看:107
本文介绍了自定义c#gridview和grid列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我一直在尝试自定义网格视图.
我有一个用户控件,现在我想将该用户控件用作网格列,并且必须为该自定义列添加新属性.
问题是,如果我在网格中使用该列的2个不同实例,则新添加的列的属性将无法正常工作.这是我的代码:

Hello
I''ve been trying to customize a grid view.
i had a user control and now i want to use that user control as a grid column and i have to add a new property for that customized column.
problem is that, if i use 2 different instance of that column in my grid the newly added column''s property doesn''t work properly. here is my code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GymProject.UI;
using System.ComponentModel;

namespace GymProject.Components
{
    public class DataGridEXLinkFieldColumn : System.Windows.Forms.DataGridViewColumn
    {
        public static BaseInfoKind _InfoKind;

        [Browsable(true)]
        public BaseInfoKind InfoKind
        {
            get
            {

                if (this.DataGridEXLinkFieldCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                return this.DataGridEXLinkFieldCellTemplate.InfoKind;
            }
            set
            {

                if (this.DataGridEXLinkFieldCellTemplate == null)
                {
                    throw new InvalidOperationException("Operation cannot be completed because this DataGridViewColumn does not have a CellTemplate.");
                }
                this.DataGridEXLinkFieldCellTemplate.InfoKind = 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);
                        DataGridEXLinkFieldCell dataGridViewCell = dataGridViewRow.Cells[this.Index] as DataGridEXLinkFieldCell;
                        if (dataGridViewCell != null)
                        {
                            dataGridViewCell.SetInfoKind(rowIndex, value);
                        }
                    }
                    this.DataGridView.InvalidateColumn(this.Index);
                }

            }
        }

        public DataGridEXLinkFieldColumn()
            : base(new DataGridEXLinkFieldCell())
        {
            
        }

        private DataGridEXLinkFieldCell DataGridEXLinkFieldCellTemplate
        {
            get
            {
                return (DataGridEXLinkFieldCell)this.CellTemplate;
            }
        }

    }

    public class DataGridEXLinkFieldCell : System.Windows.Forms.DataGridViewTextBoxCell
    {
        
        //public BaseInfoKind InfoKindCell { get; set; }

        static  BaseInfoKind _InfoKind;

        public BaseInfoKind InfoKind
        {
            get
            {
                return _InfoKind;
            }
            set
            {
                _InfoKind = value;
            }
        }



        public override object Clone()
        {
            DataGridEXLinkFieldCell dataGridViewCell = base.Clone() as DataGridEXLinkFieldCell;
            // Setting properties
            return dataGridViewCell;
        }

        protected override object GetFormattedValue(object value, int rowIndex, ref DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter valueTypeConverter, System.ComponentModel.TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
        {
            return value == null ? string.Empty : value.ToString(); // string.Format("{0:n0}", value);
        }

        public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);

            DataGridEXLinkFieldEditingControl editingControl = this.DataGridView.EditingControl as DataGridEXLinkFieldEditingControl;


            editingControl.Anchor = AnchorStyles.Left;
            editingControl.InfoKind = this.InfoKind;

            string initialFormattedValueStr = initialFormattedValue as string;
            if (initialFormattedValueStr == null)
            {
                editingControl.Text = string.Empty;
            }
            else
            {
                editingControl.Text = initialFormattedValueStr;
            }


        }

        private Type DefaultEditType = typeof(DataGridEXLinkFieldEditingControl);
        private Type DefaultValueType = typeof(DataGridEXLinkFieldEditingControl);

        public override Type EditType
        {
            get
            {
                return this.DefaultEditType;
            }
        }

        public override Type ValueType
        {
            get
            {
                return this.DefaultValueType;
            }
        }

        internal void SetInfoKind(int rowIndex, BaseInfoKind value)
        {
            this.InfoKind = value;
        }
    }

    public class DataGridEXLinkFieldEditingControl : LinkField, IDataGridViewEditingControl
    {
        

        public DataGridEXLinkFieldEditingControl()
        {
            this.TabStop = false;
            this.InfoKind = InfoKind;
        }


        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.NotifyDataGridView();
        }

        private void NotifyDataGridView()
        {
            if (!this.EditingControlValueChanged)
            {
                this.EditingControlValueChanged = true;
                this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            }
        }

        public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.BorderStyle = BorderStyle.None;
            this.BackColor = dataGridViewCellStyle.BackColor;
        }

        public System.Windows.Forms.DataGridView EditingControlDataGridView
        {
            get;
            set;
        }

        public object EditingControlFormattedValue
        {
            get
            {
                return this.SelectedValue; // string.Format("{0:n0}", this.Value);
            }
            set
            {
                this.Text= value.ToString();
            }
        }

        public int EditingControlRowIndex
        {
            get;
            set;
        }

        public bool EditingControlValueChanged
        {
            get;
            set;
        }

        public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            if (!dataGridViewWantsInputKey)
                return true;

            if (keyData == Keys.Up || keyData == Keys.Down)
                return true;

            return false;
        }

        public Cursor EditingPanelCursor
        {
            get { return Cursors.Default; }
        }

        public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return this.SelectedValue.ToString();
        }

        public void PrepareEditingControlForEdit(bool selectAll)
        {

        }

        public bool RepositionEditingControlOnValueChange
        {
            get { return false; }
        }
    }
}


我会很感激您的帮助,因为我确实需要这个.
谢谢:)


i''ll appreciate your help because i do need this.
thanks :)

推荐答案

CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^] might be of some assistance to you.


不是确定您的代码;但 http://www.dotnetperls.com/datagridview [
Not sure about your code; but nice GridView tips are available at http://www.dotnetperls.com/datagridview[^]


这篇关于自定义c#gridview和grid列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆