单击DataGridView中的行标题的事件

时间:2012-06-20 09:12:45

标签: c# .net winforms datagridview

仅在DataGridView的行标题上专门处理鼠标点击的事件是什么?

如果没有,那么处理此类事件的替代方法是什么?

4 个答案:

答案 0 :(得分:8)

有一个新的Winforms项目并复制粘贴以下代码: -

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

答案 1 :(得分:4)

您可以通过以下代码获取行标题:

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 

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

答案 2 :(得分:0)

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

  1. RowHeaderMouseClick
  2. RowHeaderMouseDoubleClick

答案 3 :(得分:-1)

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