日期:2014-05-18  浏览次数:20806 次

在datagridview中画一根线,线的终点超出datagridview显示范围,点击滚动条,线条无法保持原样(附代码)
在datagridview中添加一根线,线的终点超出datagridview显示范围,点击滚动条,线条无法保持原样(附代码)
如何使得线条保持原样?
还请各位帮忙看看,谢谢了。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {

  for (int i = 0; i < 20; i++)
  {
  dataGridView1.Columns.Add(i.ToString(),"clo"+i.ToString());
  }
  dataGridView1.Rows.Add(50);
  for (int i = 0; i < dataGridView1.Rows.Count; i++)
  {
  dataGridView1.Rows[i].HeaderCell.Value = "PD" + i.ToString();
  }
  }

  private void dataGridView1_Paint(object sender, PaintEventArgs e)
  {
  Pen p = new Pen(Color.Black, 2);

  int startX;
  int startY;
  int endX;
  int endY;

  startX = dataGridView1.Rows[1].HeaderCell.Size.Width;
  startY = dataGridView1.Columns[1].HeaderCell.Size.Height;

  endX = dataGridView1.Size.Width;
  endY = dataGridView1.Size.Height;

  Point start = new Point(startX, startY);
  Point end = new Point(endX, endY);
  e.Graphics.DrawLine(p, start, end);
  }
  }
}


------解决方案--------------------
真是由于这种不佳效果所以舍弃不用
------解决方案--------------------
要算好位置