datagridview循环滚动显示
首先设置dataGridView的SelectionMode值为FullRowSelect,
MultiSelect值为false.
其次记录不要太少.因为记录太少的时候,FirstDisplayedScrollingRowIndex会一直为0.对其进行赋值等操作
都会无效
要实现循环滚动,须添加计时器事件,计时器代码如下:
private void timer1_Tick(object sender, EventArgs e)
{ //dgvcasebystreet为DataGridVew控件
int index = this.dgvcasebystreet.FirstDisplayedScrollingRowIndex;
this.dgvcasebystreet.Rows[index].Selected = true; // 设置为选中.
this.dgvcasebystreet.FirstDisplayedScrollingRowIndex++;
//如果数据太少.就不滚动
if (index == this.dgvcasebystreet.FirstDisplayedScrollingRowIndex)
{
return;
}
//将刚才定位的数据插入到dtSource的末尾.这是为了实现循环滚动.
dtSource.ImportRow(dtSource.Rows[index]);
//rowCount为记录总数
if (this.dgvcasebystreet.FirstDisplayedScrollingRowIndex > rowCount - 1)
{
//初始时,将数据取出同时放在dtSource 与dtTmpSource中,
//现在dtSource中,已增加了很多数据.
//因而在所有原数据都遍历完,开始下一次遍历时,将原数据覆盖现在的dtSource ;
dtSource = dtTmpSource;
this.dgvcasebystreet.FirstDisplayedScrollingRowIndex = 0;
}
}
//下面注释掉代码,只是用来说明程序中是如何绑定数据的.
//BindingSource bindSrc = new BindingSource();
//bindSrc.Clear();
//bindSrc.DataSource = dtSource;//该dtSource为存放数据的dataTable对象
//this.dgvcasebystreet.DataSource = bindSrc;