简体   繁体   中英

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?

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

在此输入图像描述

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!");
    }
}

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...
    }

有两个与单击行标题相关的事件。

  1. RowHeaderMouseClick
  2. RowHeaderMouseDoubleClick
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataGridViewRow dr = dataGridView1.SelectedRows[0];
                this.Hide();
                frmStock frm2 = new frmStock();
                frm2.Show();
                frm2.txtStockID.Text = dr.Cells[0].Value.ToString();
                frm2.txtConfigID.Text = dr.Cells[1].Value.ToString();
                frm2.txtProductname.Text = dr.Cells[2].Value.ToString();
                frm2.txtFeatures.Text = dr.Cells[3].Value.ToString();
                frm2.txtPrice.Text = dr.Cells[4].Value.ToString();
                frm2.txtQty.Text = dr.Cells[5].Value.ToString();
                frm2.txtTotalPrice.Text = dr.Cells[6].Value.ToString();
                frm2.dtpStockDate.Text = dr.Cells[7].Value.ToString();
                frm2.btnUpdate.Enabled = true;
                frm2.btnDelete.Enabled = true;
                frm2.btnSave.Enabled = false;
                frm2.label8.Text = label1.Text;
           }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM