【问题标题】:Create Right Click Menu on dataGridView for entire row在dataGridView上为整行创建右键菜单
【发布时间】:2016-06-26 17:22:18
【问题描述】:

我有一个简单的dataGridView,里面装满了东西,现在我希望能够右键单击左侧以突出显示整行,这样我就可以插入或删除整行。我一直在寻找这个,但大多数时候它是针对单个单元格的,我正在寻找一个用于整行的上下文菜单。

还有一个类似的问题,但最终不完全是我想要做的。

提前谢谢你!!

【问题讨论】:

  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example
  • 我已经尝试了几件事,但似乎没有什么是我真正想要完成的,而且我的代码变得越来越混乱。我真正想做的就是能够在程序运行时将一行插入到 dataGridView 对象中。您可以通过突出显示行并点击“删除”来删除行,但点击“插入”什么也不做。 :(
  • 我们不是拿着水晶球的魔术师。我们看不到您的代码。我们看不到您的申请。我们看不出有什么问题。您必须提供一个工作示例。否则,stackoverflow 不是问这类问题的网站。如果您阅读了帮助,通常这类问题会因题外话/太宽泛/不清楚您的要求而被关闭。祝你好运。
  • 我知道,自 2008 年以来我一直在使用堆栈溢出。我不会问很多问题,通常我可以弄清楚一些东西,但这是相当新的。也许我会买一本 C# 书。大声笑
  • 可能你在stackexchange的错误站点。我不知道。 Stackoverflow 是帮助特定问题的特定问题。所有与此不符的,都是离题的或太宽泛的。如果您使用 S.O.正如你所说的8年,我不知道你为什么要问这个问题。可能你永远不会得到答案

标签: c# winforms datagridview row right-click


【解决方案1】:

我找到了一种非常简单的方法来做我想做的事。它是这样的:

首先,绑定一个 mouseDown 事件:

dataGridView1.MouseDown += new MouseEventHandler(this.dataGridView1_MouseClick);

然后,创建响应 mouseDown 事件的例程..

 private void dataGridView1_MouseClick(Object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextMenu cm = new ContextMenu();
            this.ContextMenu = cm;
            cm.MenuItems.Add(new MenuItem("&Cut", new System.EventHandler(this.cut_Click)));
            cm.MenuItems.Add(new MenuItem("&Copy", new System.EventHandler(this.copy2_Click)));
            cm.MenuItems.Add(new MenuItem("&Paste", new System.EventHandler(this.paste2_Click)));



            cm.Show(this, new Point(e.X, e.Y));
        }

    }

然后添加您的事件例程,例如:

private void cut_Click(Object sender, EventArgs e)
    {
        if (this.dataGridView1.GetCellCount(DataGridViewElementStates.Selected) > 0)
        {
            try
            {
                Clipboard.SetDataObject(this.dataGridView1.GetClipboardContent());
            }
            catch (System.Runtime.InteropServices.ExternalException)
            {
                MessageBox.Show("Clipboard could not be accessed. Please try again.");
            }
        }

        if (this.dataGridView1.SelectedRows.Count > 0)
        {
            dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
        }

    }

这会将复制的单元格添加到剪贴板。然后粘贴它们,例如:

private void paste2_Click(Object sender, EventArgs e)
    {
        char[] rowSplitter = { '\r', '\n' };
        char[] columnSplitter = { '\t' };

        // Get the text from clipboard
        IDataObject dataInClipboard = Clipboard.GetDataObject();
        string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);

        // Split it into lines
        string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);

        // Get the row and column of selected cell in grid
        int r = dataGridView1.SelectedCells[0].RowIndex;
        int c = dataGridView1.SelectedCells[0].ColumnIndex;

        // Add rows into grid to fit clipboard lines
        if (dataGridView1.Rows.Count < (r + rowsInClipboard.Length))
        {
            dataGridView1.Rows.Add(r + rowsInClipboard.Length - dataGridView1.Rows.Count);
        }

        // Loop through the lines, split them into cells and place the values in the corresponding cell.
        for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
        {
            // Split row into cell values
            string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);

            // Cycle through cell values
            for (int iCol = 0; iCol < valuesInRow.Length; iCol++)
            {

                // Assign cell value, only if it within columns of the grid
                if (dataGridView1.ColumnCount - 1 >= c + iCol)
                {
                    DataGridViewCell cell = dataGridView1.Rows[r + iRow].Cells[c + iCol];

                    if (!cell.ReadOnly)
                    {
                        cell.Value = valuesInRow[iCol+1];
                    }
                }
            }
        }
    }

我使用 [col+1] 是因为列表中的第一项始终是空白 ("")。希望这可以帮助某人。这个对我有用! :D

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2011-11-02
    • 2013-02-11
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多