首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

一个关于自定义DataGridViewColumn的有关问题,高手来帮帮忙啊

2012-01-05 
请教大家一个关于自定义DataGridViewColumn的问题,急,高手来帮帮忙啊!!!我在写一个自定义的DataGridViewCo

请教大家一个关于自定义DataGridViewColumn的问题,急,高手来帮帮忙啊!!!
我在写一个自定义的DataGridViewColumn对象,功能是该列的单元格用来输入密码,因此必需象文本框的UseSystemPasswordChar功能。大部分都写好了,可以最后发现该列中无法输入 q 字符,不知道为什么,好晕啊,高手帮我看看有什么问题!!谢谢了!!我的思路是把单元格的值用*来替换,把真实的值保存在TAG中,编辑的时候用从TextBox继承的编辑控件来编辑。下面是代码
using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;

namespace   gif2seal
{
        public   class   TextBoxColumn   :   DataGridViewColumn
        {
                public   TextBoxColumn()
                        :   base(new   TextBoxCell())
                {
                }

                public   override   DataGridViewCell   CellTemplate
                {
                        get
                        {
                                return   base.CellTemplate;
                        }
                        set
                        {
                                //   Ensure   that   the   cell   used   for   the   template   is   a   TextBoxCell.
                                if   (value   !=   null   &&   !value.GetType().IsAssignableFrom(typeof(TextBoxCell)))
                                {
                                        throw   new   InvalidCastException( "Must   be   a   TextBoxCell ");
                                }
                                base.CellTemplate   =   value;
                        }
                }
        }

        public   class   TextBoxCell   :   DataGridViewTextBoxCell
        {

                public   TextBoxCell()
                        :   base()
                {
                }
                protected   override   bool   SetValue(int   rowIndex,   object   value)


                {
                        if   (this.DataGridView   !=   null   &&   this.DataGridView.Tag   !=   null)
                        {
                                this.Tag   =   DataGridView.Tag;
                                DataGridView.Tag   =   null;
                                if   (value   !=   null)
                                        value   =   new   string( '* ',   this.Tag.ToString().Length);
                        }
                        return   base.SetValue(rowIndex,   value);
                }
                public   override   void   InitializeEditingControl(int   rowIndex,   object   initialFormattedValue,   DataGridViewCellStyle   dataGridViewCellStyle)
                {
                        //   Set   the   value   of   the   editing   control   to   the   current   cell   value.
                        base.InitializeEditingControl(rowIndex,   initialFormattedValue,   dataGridViewCellStyle);

                        TextBoxEditingControl   ctl   =   DataGridView.EditingControl   as   TextBoxEditingControl;

                        ctl.Text   =   this.Tag.ToString();
                }

                public   override   Type   EditType
                {
                        get
                        {
                                //   Return   the   type   of   the   editing   contol   that   TextBoxCell   uses.
                                return   typeof(TextBoxEditingControl);
                        }
                }

                public   override   Type   ValueType
                {
                        get


                        {
                                //   Return   the   type   of   the   value   that   TextBoxCell   contains.
                                return   typeof(string);
                        }
                }
        }

        class   TextBoxEditingControl   :   TextBox,   IDataGridViewEditingControl
        {
                DataGridView   dataGridView;
                private   bool   valueChanged   =   false;
                int   rowIndex;

                public   TextBoxEditingControl()
                {
                        this.UseSystemPasswordChar   =   true;
                }

                //   Implements   the   IDataGridViewEditingControl.EditingControlFormattedValue   property.
                public   object   EditingControlFormattedValue
                {
                        get
                        {
                                return   this.Text;
                        }
                        set
                        {
                                this.Text   =   value.ToString();
                        }
                }

                //   Implements   the   IDataGridViewEditingControl.GetEditingControlFormattedValue   method.
                public   object   GetEditingControlFormattedValue(DataGridViewDataErrorContexts   context)
                {
                        return   EditingControlFormattedValue;
                }

                //   Implements   the   IDataGridViewEditingControl.ApplyCellStyleToEditingControl   method.
                public   void   ApplyCellStyleToEditingControl(DataGridViewCellStyle   dataGridViewCellStyle)


                {

                }

                //   Implements   the   IDataGridViewEditingControl.EditingControlRowIndex   property.
                public   int   EditingControlRowIndex
                {
                        get
                        {
                                return   rowIndex;
                        }
                        set
                        {
                                rowIndex   =   value;
                        }
                }

                //   Implements   the   IDataGridViewEditingControl.EditingControlWantsInputKey   method.
                public   bool   EditingControlWantsInputKey(Keys   key,   bool   dataGridViewWantsInputKey)
                {
                        switch   (key   &   Keys.KeyCode)
                        {
                                case   Keys.Left:
                                case   Keys.Up:
                                case   Keys.Down:
                                case   Keys.Right:
                                case   Keys.Home:
                                case   Keys.End:
                                case   Keys.PageDown:
                                case   Keys.PageUp:
                                        return   true;
                                default:
                                        /*if   (key.GetHashCode()   ==   81)
                                        {


                                                KeysConverter   kc   =   new   KeysConverter();
                                                this.EditingControlFormattedValue   =   kc.ConvertToString(key);
                                                this.OnTextChanged(new   EventArgs());
                                        }*/
                                        return   false;
                        }
                }

                //   Implements   the   IDataGridViewEditingControl.PrepareEditingControlForEdit   method.
                public   void   PrepareEditingControlForEdit(bool   selectAll)
                {
                        //   No   preparation   needs   to   be   done.
                }

                //   Implements   the   IDataGridViewEditingControl   RepositionEditingControlOnValueChange   property.
                public   bool   RepositionEditingControlOnValueChange
                {
                        get
                        {
                                return   false;
                        }
                }

                //   Implements   the   IDataGridViewEditingControl   EditingControlDataGridView   property.
                public   DataGridView   EditingControlDataGridView
                {
                        get
                        {
                                return   dataGridView;
                        }
                        set
                        {
                                dataGridView   =   value;


                        }
                }

                //   Implements   the   IDataGridViewEditingControl   EditingControlValueChanged   property.
                public   bool   EditingControlValueChanged
                {
                        get
                        {
                                return   valueChanged;
                        }
                        set
                        {
                                valueChanged   =   value;
                        }
                }

                //   Implements   the   IDataGridViewEditingControl   EditingPanelCursor   property.
                public   Cursor   EditingPanelCursor
                {
                        get
                        {
                                return   base.Cursor;
                        }
                }

                protected   override   void   OnTextChanged(EventArgs   eventargs)
                {
                        valueChanged   =   true;
                        this.EditingControlDataGridView.Tag   =   null;
                        this.EditingControlDataGridView.Tag   =   this.EditingControlFormattedValue;
                        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                        base.OnTextChanged(eventargs);
                }
        }
}


[解决办法]
看了一下应该没什么问题的。不过因为你是一个相当于密码显示的TextBox,没有必要相应其它的系统键,将下列函数修改:
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
return false;
}

热点排行