C # DataGridView. What should I do when right-clicking RowHeader and right-clicking the menu ?,

Source: Internet
Author: User

C # DataGridView. What should I do when right-clicking RowHeader and right-clicking the menu ?,
    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)        {            if( e.ColumnIndex==-1)            {                MessageBox.Show("This is the line Header");            }        }     private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)        {            if(e.Button==MouseButtons.Right)            {                // Add a shortcut menu            }        }Certainly, the code upstairs is not tested. Note: You need to right-click the title line and a shortcut menu appears.
Besides, CellClick does not return the right-click response. Therefore, the right-click response is only available in the CellMouseClick event. (RowHeaderMouseClick also responds, but I don't know why, my VS2005 does not respond, so it is not recommended)
Private void dgv_CellMouseClick (object sender, DataGridViewCellMouseEventArgs e)
{
// Right-click or not
If (e. Button = MouseButtons. Right)
{
// Obtain the row and column information of the click. Related Function query MSDN
DataGridView. HitTestInfo hitinfo = dgv_verify.HitTest (e. X, e. Y );
// If RowIndex <0, it is the title line.
If (hitinfo. RowIndex <0)
{
// If you only need to specify the column display menu, add the hitinfo. ColumnIndex judgment.
ContextMenuStrip1.Show (MousePosition. X, MousePosition. Y );
}
}
}

========================================================== ======================================
The above code has been tested and runs normally under VS2005 and VS2008.
Ask me another question. From: http://bbs.csdn.net/topics/370259768

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.