开发者

How to prevent displaying contextmenuStrip?

开发者 https://www.devze.com 2023-04-05 17:52 出处:网络
How can I prevent displaying dataGridV开发者_Python百科iew contextMenuStrip if no rows has been selected ?Subscribe to the ToolStripDropDown.Opening Event, and you can cancel it.

How can I prevent displaying dataGridV开发者_Python百科iew contextMenuStrip if no rows has been selected ?


Subscribe to the ToolStripDropDown.Opening Event, and you can cancel it.

public partial class Form1 : Form
{
    public Form1()
    {
        this.Load += new EventHandler(this.Form1_Load);
        this.InitializeComponent();

        DataTable dt = new DataTable();
        dt.Columns.Add("col1");
        dt.Columns.Add("col2");
        dt.Rows.Add(new string[] { "test", "test" });
        dt.Rows.Add(new string[] { "test", "test" });
        dt.Rows.Add(new string[] { "test", "test" });

        this.dataGridView1.DataSource = dt;
        this.dataGridView1.ContextMenuStrip.Opening += new CancelEventHandler(this.ContextMenuStrip_Opening);            
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.dataGridView1.ClearSelection();
    }

    private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
    {
        if (this.dataGridView1.SelectedRows.Count == 0)
        {
            e.Cancel = true;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号