在一个窗口上有个datagridview,我用鼠标点击的时候怎么区分是点击到数据行还是列标题以及其他区域呢。独自学习的无奈啊,谢谢大家给点温暖。

解决方案 »

  1.   

    CellMouseClick事件,DataGridViewCellMouseEventArgs参数,RowIndex=0为标题行,>0为数据行
      

  2.   

    更正楼上朋友的错误:RowIndex=-1才是标题行,数据行的下标从0开始
    private void g1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex == -1)
                {
                    MessageBox.Show("点了列标题");
                }
                else if (e.ColumnIndex == -1)
                {
                    MessageBox.Show("点了行标题");
                }
                else
                {
                    MessageBox.Show("点了内容");
                }
            }
      

  3.   

    使用HitTest来检测一下就可以了。
      

  4.   


    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
    System.Windows.Forms.DataGridView.HitTestInfo info = this.dataGridView1.HitTest(e.X, e.Y);
    //System.Console.WriteLine(info.Type);
    if (info.Type == DataGridViewHitTestType.ColumnHeader)
    {
    //Console.WriteLine("点在了列头上了");
    }
    }