1:如果dataGridView滚动条滚动后坐标会不会改变
2:我要得到dataGridView的非鼠标点击方式的坐标  ,然后花一条直线??

解决方案 »

  1.   

    方法如下:只需传进datagridview和鼠标单击的位置,就可以获取其所在的单元格坐标,无论datagridview是否有滚动条都行,int GetRowIndexAt(Point mouseLocation, DataGridView dataGridView)
    {
    int rowIndex = -1;
    int rowOffset = 0;
    int gridWidth = 1;
    int padding = 1;
    if (dataGridView.ColumnHeadersVisible)
    {
    rowOffset += dataGridView.ColumnHeadersHeight;
    padding = 0;
    }
    if (rowOffset + padding < mouseLocation.Y)  // 超过表列头的范围(不含顶头的边框)   
    {
    int curRow = dataGridView.FirstDisplayedScrollingRowIndex;
    for (int k = 0; k <= dataGridView.DisplayedRowCount(true); k++)
    {
    if (curRow >= dataGridView.Rows.Count)
    {
    break;
    }
    if (dataGridView.Rows[curRow].Visible)
    {
    rowOffset += dataGridView.Rows[curRow].Height;
    }
    if (rowOffset + padding + gridWidth > mouseLocation.Y)  // y为当前边框位置   
    {
    rowIndex = curRow;
    break;
    }
    curRow++;
    }
    }
    return rowIndex;
    }
      

  2.   

    至于画直线很简单,直接用GDI+:
    graphics.drawLine(new Pen(1px,red),x0,y0,x1,y1);其中x0和y0是起点的x和y坐标,x1和y1是终点的横坐标和纵坐标。
    pen是画笔,定义所画横线的宽度和颜色。
      

  3.   

    加载?
    什么加载?
    如果是窗体加载,则在窗体加载的事件中画呀:
    using System.Drawing;
    private void Form1_Load(object sender, EventArgs e)
    {
    Graphics g=new Graphics();//似乎不能直接这样用,可能是这样:Graphics g=dataGridView.CreateGraphics();
    g.DrawLine(new Pen(1px,red),new Point(x0,y0),new Point(x1,y1));
    }
      

  4.   

    这些都会问题是x0,y0,x1,y1如何知道??