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;
using System.Diagnostics;
namespace WinFormDGVTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dvge.DataSource = GetData();
trackBar1.Minimum = dvge.VScrll.Minimum;
trackBar1.Maximum = dvge.VScrll.Maximum;
// dataGridViewEx1.isShowHScrll.Value = 1;
Rectangle rect = dvge.DisplayRectangle;
int h = rect.Height;
int showRow = h / dvge.RowTemplate.Height;
Trace.WriteLine(showRow);
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
DataRow dr = null;
for (int i = 0; i < 50; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "我是" + i.ToString();
dt.Rows.Add(dr);
}
return dt;
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
dvge.VScrll.Value = trackBar1.Value;
int val = (int)(trackBar1.Value * ((float)dvge.Rows.Count / dvge.VScrll.Maximum));
dvge.FirstDisplayedScrollingRowIndex = val >= dvge.Rows.Count ? dvge.Rows.Count - 1 : val;
}
/// <summary>
/// 临时保存的X坐标
/// </summary>
private int nx;
/// <summary>
/// 临时保存的Y坐标
/// </summary>
int ny;
/// <summary>
/// 临时保存的当前选择的行
/// </summary>
int nowindex;
/// <summary>
/// 显示的行数
/// </summary>
private int ShowRowCount
{
get { return dvge.DisplayRectangle.Height / dvge.RowTemplate.Height; }
}
private int _InNextH;
/// <summary>
/// 要进入下一页的条件
/// </summary>
public int InNextH
{
get
{
if (_InNextH < 1)
{
_InNextH = dvge.DisplayRectangle.Height / 4;
}
return _InNextH;
}
}
public int IntervalTime { get; set; }
private void dvge_MouseDown(object sender, MouseEventArgs e)
{
nx = e.X;
ny = e.Y;
nowindex = dvge.FirstDisplayedScrollingRowIndex;
Cursor = Cursors.SizeAll;
dvge.MouseMove += new MouseEventHandler(dvge_MouseMove);
}
private void dvge_MouseUp(object sender, MouseEventArgs e)
{
Cursor = Cursors.Default;
dvge.MouseMove -= new MouseEventHandler(dvge_MouseMove);
int charge = ny - e.Y;
if (Math.Abs(charge) < InNextH) return;
int sroll = nowindex + ShowRowCount * (charge > 0 ? 1 : -1);
if (sroll<0)
{
sroll = 0;
}
sroll = sroll > (dvge.Rows.Count - 1) ? (dvge.Rows.Count - 1) : sroll;
dvge.FirstDisplayedScrollingRowIndex = sroll;
}
void dvge_MouseMove(object sender, MouseEventArgs e)
{
int charegRow = (ny - e.Y) / dvge.RowTemplate.Height;
int teindex = nowindex + charegRow;
if (teindex < 0)
{
teindex = 0;
}
dvge.FirstDisplayedScrollingRowIndex = teindex;
}
int chargeNum;
private void SetScroll(int Nowval)
{
chargeNum=dvge.FirstDisplayedScrollingRowIndex-Nowval;
if (Math.Abs(chargeNum) < 2)
{
dvge.FirstDisplayedScrollingRowIndex = Nowval;
return;
}
Timer tm = new Timer();
tm.Interval = IntervalTime;
tm.Tick += new EventHandler(tm_Tick);
tm.Enabled = true;
}
void tm_Tick(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinFormDGVTest
{
public class DataGridViewEx : DataGridView
{
public ScrollBar VScrll
{
get { return this.VerticalScrollBar; }
}
public ScrollBar HScrll
{
get { return this.HorizontalScrollBar; }
}
}
}