戏子痴心猪
一头执着演着戏的猪···

第一次写博文哈···

这只是一个自己的方法笔记,用来从数据库读取数据,以绑定ASPxGridView的选择状态。

代码
private void SelectionState()
{
//根据条件从数据库读取数据,最后得到一个集合
td_examtype et = new td_examtype();
et.examtype_Profession
= cbProfession.SelectedItem.Value.ToString();
et.examtype_Type
= Convert.ToInt32(cbType.SelectedItem.Value) + 1;
et.examtype_Year
= DateTime.Now.Year.ToString();
List
<td_examtype> list = Bll_examtype.GetExamType(et);
//循环遍历ASPxGridView的行,匹配数据,如果成功,设置Selection,让这一行变成选中状态。
for (int i = 0; i < ASPxGridView1.VisibleRowCount; i++)
{
DataRowView row
= ASPxGridView1.GetRow(i) as DataRowView;
if (list.Where(e => e.examtype_StudentCard == row[0].ToString()).SingleOrDefault() != null)
{
ASPxGridView1.Selection.SelectRow(i);
}

}
}

其中,ASPxGridView1.GetRow(i)得到的是一个对象,至于是什么对象,视你绑定的数据源而定,因为我绑定的是一个datatable,这里得到的就是datarowview,如果不知道要把它转换成什么对象,可以试试row.GetType().toString(),得到它的数据类型,然后转换就可以了。当然了,你也可以把绑定的数据源全部转换成datatable,这样就可以不用担心类型转换的问题了。另记得线清空ASPxGridView已有的选中状态,ASPxGridView1.Selection.UnselectAll();,再调用这个方法,不然每次更新的时候它只是清空了当前页的绑定状态哦。

注:可能看不全的那个地方的代码是:if (list.Where(e => e.examtype_StudentCard == row[0].ToString()).SingleOrDefault() != null)

posted on 2011-01-27 15:49  戏子痴心猪  阅读(1029)  评论(0编辑  收藏  举报