Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event for clicking on row headers in DataGridView

What is the event that exclusively handles mouse clicks made only on Row Headers of DataGridView?

If there are none, what would be an alternative of handling this type of event?

like image 834
l46kok Avatar asked Dec 20 '22 20:12

l46kok


2 Answers

Have a new Winforms Project and copy-paste the code below :-

enter image description here

public partial class Form1 : Form
{
    public Form1()
    {
        var list = new List<Books>
                       {
                           new Books() {Title = "Harry Potter", TotalRating = 5},
                           new Books() {Title = "C#", TotalRating = 5}
                       };
        InitializeComponent();
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = list;
        dataGridView1.RowHeaderMouseClick += new DataGridViewCellMouseEventHandler(OnRowHeaderMouseClick);
    }

    void OnRowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        MessageBox.Show("Clicked RowHeader!");
    }
}
like image 106
Angshuman Agarwal Avatar answered Dec 31 '22 12:12

Angshuman Agarwal


You can get the row header by following code:

Private Sub dataGridView1_RowHeaderMouseClick( _
    ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) _
    Handles dataGridView1.RowHeaderMouseClick

    Me.dataGridView1.SelectionMode = _
        DataGridViewSelectionMode.RowHeaderSelect
    Me.dataGridView1.Rows(e.RowIndex).Selected = True

End Sub 

or

void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        //
        // Do something on double click, except when on the header.
        //
        if (e.RowIndex == -1)
        {
        //this is row header...
            some code here.
        }
       Code...
    }
like image 20
Brijesh Patel Avatar answered Dec 31 '22 14:12

Brijesh Patel