【问题标题】:Why doesn't this Button in my DataGridView click?为什么我的 DataGridView 中的这个按钮没有点击?
【发布时间】:2017-07-15 13:40:08
【问题描述】:

我在 DataGridView 中添加了一个按钮,当鼠标悬停在一行上时,用户可以删除该行。我以前将此作为一个额外的列,但现在我只想在悬停时显示该按钮。该按钮将自身移动到悬停在最后一列末尾的行。

为什么按钮不允许我点击它(事件永远不会触发)?

public class CustomDataGridView : DataGridView
{
    private Button deleteButton = new Button();

    public CustomDataGridView()
    {
        this.Columns.Add("Column1", "Column1");
        this.Columns.Add("Column2", "Column2");
        this.Columns.Add("Column3", "Column3");

        this.CellMouseEnter += this_CellMouseEnter;
        this.CellMouseLeave += this_CellMouseLeave;

        deleteButton.Height = this.RowTemplate.Height - 1;
        deleteButton.Width = this.RowTemplate.Height - 1;
        deleteButton.Text = "";
        deleteButton.Visible = false;

        deleteButton.MouseClick += (s, e) =>
            MessageBox.Show("Delete Button Clicked!", "", MessageBoxButtons.OK);

        this.Controls.Add(deleteButton);
    }

    private void this_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            deleteButton.Visible = true;
            Rectangle rect = this.GetCellDisplayRectangle(2, e.RowIndex, true);
            deleteButton.Location = new Point(rect.Right - deleteButton.Width - 1, rect.Top);
        }
    }

    private void this_CellMouseLeave(object sender, DataGridViewCellEventArgs e) {
        if (e.RowIndex != -1)
            deleteButton.Visible = false;
    }
}

我尝试添加其他 Click 事件以查看它们如何与之交互,并且它们似乎工作正常。

this.Click += (s, e) =>
    MessageBox.Show("DataGridView Clicked!", "", MessageBoxButtons.OK);
this.CellClick += (s, e) => 
    MessageBox.Show("Cell Clicked!", "", MessageBoxButtons.OK);
this.MouseClick += (s, e) =>
    MessageBox.Show("DataGridView Mouse Clicked!", "", MessageBoxButtons.OK);
this.CellContentClick += (s, e) =>
    MessageBox.Show("Cell Content Clicked!", "", MessageBoxButtons.OK);

CustomDataGridView的创建:

public partial class MainForm : Form {      
    public MainForm() {
        InitializeComponent();

        CustomDataGridView dgv = new CustomDataGridView();
        dgv.Location = new Point(100, 100);
        dgv.Width = 500;
        dgv.Height = 300;
        this.Controls.Add(dgv);

        dgv.Rows.Add("text1", "", "");
        dgv.Rows.Add("text2", "", "");
        dgv.Rows.Add("text3", "", "");
        dgv.Rows.Add("text4", "", "");
        dgv.Rows.Add("text5", "", "");
    }
}

【问题讨论】:

    标签: c# winforms events button datagridview


    【解决方案1】:

    尽量不要在CellMouseLeave事件处理程序中设置ButtonVisible属性。

    由于您正在为整个DataGridView 控件创建一个使用单个Button 并简单地更改它的位置,因此您不需要处理CellMouseLeave 事件。这对我有用:

    public class CustomDataGridView : DataGridView
    {
        private Button deleteButton = new Button();
    
        public CustomDataGridView()
        {
            this.Columns.Add("Column1", "Column1");
            this.Columns.Add("Column2", "Column2");
            this.Columns.Add("Column3", "Column3");
    
            this.CellMouseEnter += this_CellMouseEnter;
    
            deleteButton.Height = this.RowTemplate.Height - 1;
            deleteButton.Width = this.RowTemplate.Height - 1;
            deleteButton.Text = "";
            deleteButton.Visible = false;
    
            deleteButton.MouseClick += (s, e) =>
                MessageBox.Show("Delete Button Clicked!", "", MessageBoxButtons.OK);
    
            this.Controls.Add(deleteButton);
        }
    
        private void this_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                deleteButton.Visible = true;
                Rectangle rect = this.GetCellDisplayRectangle(2, e.RowIndex, true);
                deleteButton.Location = new Point(rect.Right - deleteButton.Width - 1, rect.Top);
            }
        }
    }
    

    【讨论】:

    • 哇!这么简单的解决方案!我不太确定为什么把它拿出来改变了很多,但我很高兴你明白了!!!!谢谢!
    • @BlakeThingstad 因为当鼠标移过按钮时,它会触发 CellMouseLeave 事件,该事件会隐藏按钮,该事件会触发 CellMouseEnter 事件,该事件会显示按钮等。您的代码太忙了显示和隐藏按钮以实际执行任何操作。
    • @LarsTech 这很有道理。谢谢你的解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多