下面就mobile没有提供DataGrid滚动事件如何解决一例,在mobile中编辑DataGrid数据遇到挺多麻烦,因为DataGrid无滚动事件,那么下面间接方式处理DataGrid的滚动事件。(须设置vScrollBar的LargeChange为1,否则达不到预期效果)
1、在DataGrid右侧滚动条位置放置一个vScrollBar挡住DataGrid默认的滚动条
2、在DataGrid的数据绑定事件中设置vScrollBar的Maximum为DataGrid的数据行数
3、DataGrid的CurrentCellChanged事件中给vScrollBar的Value赋值为当前选中行的Index
4、vScrollBar的ValueChanged事件中给DataGrid的当前选中Index赋值为vScrollBar的Value
下面是示范代码:

C# Code
[http://www.xueit.com]
private void Form1_Load(object sender, EventArgs e)
{
vScrollBar.Value = 0;
vScrollBar.Minimum = 0;
vScrollBar.SmallChange = 1;
vScrollBar.LargeChange = 1;
vScrollBar.Enabled = false;
}
private void BindDataGrid(object sender, EventArgs e)
{
SqlCeConnection con = new SqlCeConnection(@"data source=program filesdb.sdf");
SqlCeDataAdapter da = new SqlCeDataAdapter("select * from table1", con);
DataSet ds = new DataSet();
da.Fill(ds);
dataGrid.DataSource = ds.Tables[0];
vScrollBar.Maximum = ds.Tables[0].Rows.Count - 1;
vScrollBar.Enabled = true;
}
private void dg_CurrentCellChanged(object sender, EventArgs e)
{
vScrollBar.Value = dataGrid.CurrentRowIndex;
}
private void vScrollBar_ValueChanged(object sender, EventArgs e)
{
dataGrid.CurrentRowIndex = vScrollBar.Value;
}
精彩图集