Reputation: 1659
I'm adding a button to my DataGridView when a row is hovered over to allow the user to delete the row. I previously had this as an extra column, but now I just want the button to show up when hovered over. The button moves itself to the row that is hovered over and is placed at the end of the last column.
Why won't the button allow me to click it (the event never fires)?
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;
}
}
I've tried adding in other Click events to see how they interact with this and they seem to be working correctly.
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);
Creation of 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", "", "");
}
}
Upvotes: 1
Views: 99
Reputation: 169400
Try not to set the Visible
property of the Button
in the CellMouseLeave
event handler.
Since you are creating an using a single Button
for the entire DataGridView
control and simply change the position of it, you don't need to handle the CellMouseLeave
event. This works for me:
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);
}
}
}
Upvotes: 2