DataGridViewTextBoxCell ติดปัญหาว่า ปุ่มใน DataGridViewTextBoxCell ไม่หายไปครับ

ปัญหาคือ


ถ้าเราคลิก แถวอื่นใน คอลัมน์ ปุ่มจะหายครับ
แต่ถ้าเราคลิกที่ columnheader หรือ คอลัมน์อื่นจะไม่หาย
อย่างในรูป


ถ้าเราคลิก แถวอื่นใน คอลัมน์ หรือในกรอบสี่เหลี่ยม ปุ่มจะหายครับ
แต่ถ้าเราคลิกที่ columnheader หรือ คอลัมน์อื่น หรือนอกกรอบสี่เหลี่ยม จะไม่หาย
ตามในรูปนี้ครับ


โค้ด


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

namespace WindowsFormsApplication1
{
    #region _DataGridViewFilePathColumn1 1

    /// <summary>
    /// Hosts a collection of DataGridViewTextBoxCell cells.
    /// </summary>
    public class DataGridViewFilePathColumn1 : DataGridViewColumn
    {

        // public enum PathType { directory, file };
        //public PathType pathtype = PathType.file;

        public DataGridViewFilePathColumn1()
            : base(new DataGridViewFilePathCell1())
        {

        }
        private string _Path;
        [System.ComponentModel.Browsable(true)]
        [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
        [System.ComponentModel.DefaultValue(@"C:")]
        [System.ComponentModel.Category("TOR Setting")]
        [System.ComponentModel.Description("Open Dialog is dispalyed and on success the contents of the Cell is replaced with the new  path.")]
        public string Directory
        {
            get
            {
                return (System.IO.Directory.Exists(_Path) || !string.IsNullOrEmpty(_Path)) ? _Path : Application.StartupPath;
            }
            set
            {
                _Path = value;
            }
        }

        public override object Clone()
        {
            var clm = base.Clone() as DataGridViewFilePathColumn1;
            DataGridViewFilePathColumn1 xxx = base.Clone() as DataGridViewFilePathColumn1;
            if (clm != null)
            {
                clm.Directory = _Path;
            }
            return clm;

        }


        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                if (null != value &&
                    !value.GetType().IsAssignableFrom(typeof(DataGridViewFilePathCell1)))
                {
                    throw new InvalidCastException("must be a DataGridViewFilePathCell1");
                }
                base.CellTemplate = value;
            }
        }
    }

    /// <summary>
    /// Displays editable text information in a DataGridView control. Uses
    /// PathEllipsis formatting if the column is smaller than the width of a
    /// displayed filesystem path.
    /// </summary>
    public class DataGridViewFilePathCell1 : DataGridViewTextBoxCell
    {
        Button browseButton;

        Dictionary<Color, SolidBrush> brushes = new Dictionary<Color, SolidBrush>();
        protected virtual SolidBrush GetCachedBrush(Color color)
        {
            if (this.brushes.ContainsKey(color))
                return this.brushes[color];
            SolidBrush brush = new SolidBrush(color);
            this.brushes.Add(color, brush);
            return brush;
        }


        protected virtual bool RightToLeftInternal
        {
            get
            {
                return this.DataGridView.RightToLeft == RightToLeft.Yes;
            }
        }

        protected override void OnContentClick(DataGridViewCellEventArgs e)
        {
            base.OnContentClick(e);
            if (this.RowIndex < 0) return;
            if (this.DataGridView.CurrentCell == this)
            {
                DataGridViewFilePathColumn1 filePathColumn = (DataGridViewFilePathColumn1)this.DataGridView.Columns[ColumnIndex];
                string file = Convert.ToString(filePathColumn.Directory + "\\" + base.Value);
                if (!System.IO.File.Exists(file))
                {
                    MessageBox.Show("ไม่พบไฟล์ " + file);
                }
                else
                {
                    System.Diagnostics.Process.Start(file);
                }
            }
        }

        protected override void OnLeave(int rowIndex, bool throughMouseClick)
        {
            base.OnLeave(rowIndex, throughMouseClick);
            browseButton.Hide();
        }
        protected override void OnEnter(int rowIndex, bool throughMouseClick)
        {
            browseButton.Hide();
            base.OnEnter(rowIndex, throughMouseClick);
            if (rowIndex < 0) return;
            
            Rectangle Loc;
            int Wid;
            Loc = this.DataGridView.GetCellDisplayRectangle(ColumnIndex, rowIndex, false);
            Wid = this.DataGridView.CurrentCell.Size.Width;
            browseButton.Location = new Point(Loc.X - 25 + Wid, Loc.Y - 4);
            browseButton.Height = this.DataGridView.CurrentCell.Size.Height + 4;
            browseButton.Width = browseButton.Height;

            if (!this.DataGridView.Controls.Contains(browseButton))
                this.DataGridView.Controls.Add(browseButton);
            browseButton.Show();
        }

        public bool ShowFocusCues
        {
            get { return true; }
        }

        protected bool ApplyVisualStylesToHeaders
        {
            get
            {
                if (Application.RenderWithVisualStyles)
                {
                    return this.DataGridView.EnableHeadersVisualStyles;
                }
                return false;
            }
        }

        void browseButton_Click(object sender, EventArgs e)
        {

            DataGridViewFilePathColumn1 filePathColumn = (DataGridViewFilePathColumn1)this.DataGridView.Columns[ColumnIndex];
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                SaveFileDialog dialog1 = new SaveFileDialog();
                dialog1.InitialDirectory = (!string.IsNullOrEmpty(filePathColumn.Directory)) ? filePathColumn.Directory : "C:\\";
                dialog1.FileName = System.IO.Path.GetFileName(dialog.FileName);
                DialogResult dl = dialog1.ShowDialog();
                if (dl == DialogResult.OK)
                {
                    string f = (string.IsNullOrEmpty(System.IO.Path.GetExtension(dialog1.FileName))) ? dialog1.FileName + System.IO.Path.GetExtension(dialog.FileName) : dialog1.FileName;
                    base.Value = f;
                    if (dialog.FileName != dialog1.FileName)
                        System.IO.File.Copy(dialog.FileName, f, true);
                }

            }


        }
        public DataGridViewFilePathCell1()
            : base()
        {

            browseButton = new Button();
            // browseButton.FlatStyle = FlatStyle.Flat;
            browseButton.FlatAppearance.BorderSize = 0;
            browseButton.Size = new Size(30, 25);
            browseButton.ImageAlign = ContentAlignment.MiddleCenter;
            browseButton.FlatAppearance.MouseDownBackColor = Color.Transparent;
            browseButton.FlatAppearance.MouseOverBackColor = Color.Transparent;
            browseButton.BackColor = Color.Transparent;
            browseButton.Image = Properties.Resources.finds;

            browseButton.Hide();

            browseButton.Click += new EventHandler(browseButton_Click);

        }

    }

    #endregion
}

แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่