当用户右键单击某行时,我已在DataGridView控件的cellmousedown事件中将上下文菜单设置为apear。 打开包含网格的表单后,我必须在一行上单击2次,以便将上下文菜单设置为apear。在“第一次打开”之后,上下文菜单表现正常 - >只需单击一下即可显示我关注的控件。 我猜网格属性/方法有一个错误的设置,但是哪一个?
以下是网格格式的代码:
/* format grid */
// Initialize basic DataGridView properties.
dgv.BackgroundColor = Color.LightGray;
dgv.BorderStyle = BorderStyle.FixedSingle;
// Set property values appropriate for read-only display and limited interactivity.
dgv.AllowUserToAddRows = false;
dgv.AllowUserToDeleteRows = false;
dgv.AllowUserToOrderColumns = true;
dgv.ReadOnly = true;
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.MultiSelect = false;
dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgv.AllowUserToResizeColumns = false;
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgv.AllowUserToResizeRows = false;
dgv.RowHeadersVisible = false;
dgv.RowsDefaultCellStyle.BackColor = Color.White;
dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGoldenrodYellow;
// Set the row and column header styles.
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
dgv.ColumnHeadersDefaultCellStyle.BackColor = Color.White;
dgv.RowTemplate.MinimumHeight = 38;
dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
DataGridViewCellStyle hStyle = new DataGridViewCellStyle();
hStyle.Font = new Font("Arial", 9, FontStyle.Bold);
dgv.ColumnHeadersDefaultCellStyle = hStyle;
DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
cellStyle.Font = new Font("Arial", 8, FontStyle.Regular);
cellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
这是启动上下文菜单的代码:
private void dgv_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
// Ignore if a column or row header is clicked
if (e.RowIndex != -1 && e.ColumnIndex != -1)
{
if (e.Button == MouseButtons.Right)
{
DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
// Here you can do whatever you want with the cell
dgv.CurrentCell = clickedCell; // Select the clicked cell, for instance
// Get mouse position relative to the vehicles grid
var relativeMousePosition = dgv.PointToClient(Cursor.Position);
// Show the context menu
mnuOferta.Show(dgv, relativeMousePosition);
}
}
谢谢!