C# winforms datagridview - contextmenu apear at 2nd right click, not at first

空扰寡人 提交于 2019-12-11 18:07:57

问题


I have set a contextmenu to apear at cellmousedown event of a DataGridView control, when the user right click on a row. After the form containing grid is opened I have to click 2 times on a row in order the contextmenu to apear. After this "first opening" the contextmenu behave normal -> it is shown after only one click whatever control I have focus on. I guess there is a wrong setting for a grid property/method, but which one?

Here is the code for grid format:

        /* 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;

And this is the code that fires the apeareance of contextmenu:

    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);
            }
        }

Thanks!

来源:https://stackoverflow.com/questions/6450992/c-sharp-winforms-datagridview-contextmenu-apear-at-2nd-right-click-not-at-fir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!