Problem: Whenever you want to write your own double click event for datagridview control, chances are, you will actually end up in edit mode.
How to solve: DataGridView EditMode property.
From the MSDN,
DataGridView EditMode Property : Gets or sets a value indicating how to begin editing a cell.
The catch is:
All DataGridViewEditMode values except for EditProgrammatically also allow a user to double-click a cell to begin editing it.
Aha, that’s why the problem occur!! So, how do i use Edit EditProgrammatically?
MSDN give the following explaination:
Editing begins only when the BeginEdit method is called.
So I implemented the following event handler to call BeginEdit method.
private void dataGridView_ContextMenuViewEditCell(object sender, EventArgs e)
{
dataGridView.BeginEdit(true);
}
Then use contexmenu to register the event.
dataGridView.ContextMenuStrip.Items.Add(
new ToolStripMenuItem(Resources.Common_Label_Edit, Resources.Image_16_Edit,
new EventHandler(this.dataGridView_ContextMenuViewEditCell)));
And finnally, contexmenu only pop up when right click, so focus needs to be set, same as left click.
DataGridView.HitTestInfo hti = this.dataGridView.HitTest(e.X, e.Y);
if (hti.RowIndex >= 0 && hti.RowIndex < dataGridView.Rows.Count)
{
dataGridView.CurrentCell = dataGridView[hti.ColumnIndex,hti.RowIndex];
}
2 comments:
Just like Waterproof Tile Gap Filler locks gaps securely, EditMode.EditProgrammatically in DataGridView ensures controlled, precise data editing without accidental changes.
At Malabis, our goal is simple — to bring you products that make everyday life better. Whether it's smart kitchen tools, travel must-haves, eco-conscious items, or reliable cleaning accessories, we focus on delivering quality, convenience, and style in everything we offer.
We believe good products should speak for themselves. That’s why we keep things honest and straightforward — no distractions, just great products and a smooth shopping experience designed with you in mind. https://malabis.pk/
Post a Comment