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;