右键单击列标题时标识列

时间:2014-01-27 12:40:27

标签: c# .net winforms syncfusion gridgroupingcontrol

我有一个Syncfusion的GridGroupingControl。我想对列进行自定义操作,比如说自定义“隐藏列”。

在列的标题或单元格右侧点击(我顺便显示上下文菜单)时,有一种方法可以识别列名称吗?

我有* ContextMenuStrip_Opening *,但它没有提供有关该列的任何信息。 我也有* myGrid_TableControlCellClick *,但这一个动作只在单元格上,只在左侧点击...

2 个答案:

答案 0 :(得分:0)

好吧,想一想

    int selected_colum; // "selected_colum " need be a global var

    private void dtg_contatos_MouseDown(object sender, MouseEventArgs e)
    {
        try
        {
            if (e.Button == MouseButtons.Right)
            {
                dtg_contatos.ClearSelection();
                var hti = dtg_contatos.HitTest(e.X, e.Y);
                dtg_contatos.Columns[hti.ColumnIndex].Selected = true;
                selected_colum = hti.ColumnIndex;  // here you set to global var de colum to use at contextmenustrip click


                dtg_contatos.Columns[selected_colum].Visible = false; // this you will place at contextmenustrip to hide the column
            }
        }
        catch
        {
        }
    }

答案 1 :(得分:0)

你可以在GridGroupingControl上处理TableControlMouseDown事件,检查鼠标右键,然后从鼠标事件位置获取ColumnDescriptor,并保存列名(也许可以在菜单中显示HeaderText):

    this.gridGroupingControl1.TableControlMouseDown += gridGroupingControl1_TableControlMouseDown;

    private string rightClickCol;

    void gridGroupingControl1_TableControlMouseDown(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlMouseEventArgs e)
    {
        if (e.Inner.Button == System.Windows.Forms.MouseButtons.Right)
        {
            rightClickCol = string.Empty;

            Syncfusion.Windows.Forms.Grid.Grouping.GridColumnDescriptor columnDescriptor = 
                gridGroupingControl1.TableControl.GetHeaderColumnDescriptorAt(e.Inner.Location);

            if (columnDescriptor != null)
                rightClickCol = columnDescriptor.Name;
        }
    }

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
    {
        if (!string.IsNullOrEmpty(rightClickCol))
        {
            hideColumnToolStripMenuItem.Text = "Hide " + rightClickCol;
            hideColumnToolStripMenuItem.Enabled = true;
        }
        else
        {
            hideColumnToolStripMenuItem.Text = "Hide Column";
            hideColumnToolStripMenuItem.Enabled = false;
        }
    }

    private void hideColumnToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(rightClickCol))
            gridGroupingControl1.TableDescriptor.VisibleColumns.Remove(rightClickCol);
    }