DataGrid分页显示与DataGride实现排序功能
2007-12-29 16:30
786 查看
在这里主要实现DataGrid的一些显示表中的数据提供分页显示与DataGride实现排序功能
第一步取得DataTable PersonFunction是一个操作类,如果要获得连接对象调用createConnection方法
public static DataTable selectAllPerson()
{
DataSet ds = null;
try
{
SqlConnection conn = PersonFunction.createConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from personInfo", conn);
ds = new DataSet();
sda.Fill(ds, "personInfo");
}
catch (Exception ex) {
ex.Message.ToString();
}
return ds.Tables["personInfo"];
}
第二部:将数据填充DataGrid控件
private void fillDataGrid()
{
try
{
DataGrid1.DataSource = PersonFunction.selectAllPerson();
DataGrid1.DataBind();
}
catch (Exception ex) {
ex.Message.ToString();
}
}
第三部:调用 在Page_load中调用
protected void Page_Load(object sender, EventArgs e)
{
this.fillDataGrid();
}
实现分页功能
在DataGrid控件中右上方有一个三角型按扭点击找到"属性生成器"在属性生成器中设置下面属性,在"分页"项中有复选框"允许分页"
需要选中就可以了
设置事件
DataGrid中双击PageIndexChanged事件
protected void dgShowPersonInfo_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
}
设置排序
DataGrid设置
在DataGrid控件中右上方有一个三角型按扭点击找到"属性生成器"在属性生成器中设置下面属性,
在"常规"项中有复选框"允许排序"需要选中就可以了
事件设计
选中SortCommand事件
在事件中实现的代码
protected void dgShowPersonInfo_SortCommand(object source, DataGridSortCommandEventArgs e)
{
SqlConnection conn = PersonFunction.createConnection();
DataSet ds = new DataSet();
DataView dv = new DataView();
try
{
conn.Open();
string selectString = " select * from personInfo ";
SqlDataAdapter da = new SqlDataAdapter(selectString,conn);
da.Fill(ds,"person");
dv = ds.Tables["person"].DefaultView;
dv.Sort = e.SortExpression;
this.dgShowPersonInfo.DataSource = dv;
this.dgShowPersonInfo.DataBind();
}
catch
{
Response.Write("Error");
Response.End();
}
finally {
conn.Close();
}
}
第一步取得DataTable PersonFunction是一个操作类,如果要获得连接对象调用createConnection方法
public static DataTable selectAllPerson()
{
DataSet ds = null;
try
{
SqlConnection conn = PersonFunction.createConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from personInfo", conn);
ds = new DataSet();
sda.Fill(ds, "personInfo");
}
catch (Exception ex) {
ex.Message.ToString();
}
return ds.Tables["personInfo"];
}
第二部:将数据填充DataGrid控件
private void fillDataGrid()
{
try
{
DataGrid1.DataSource = PersonFunction.selectAllPerson();
DataGrid1.DataBind();
}
catch (Exception ex) {
ex.Message.ToString();
}
}
第三部:调用 在Page_load中调用
protected void Page_Load(object sender, EventArgs e)
{
this.fillDataGrid();
}
实现分页功能
在DataGrid控件中右上方有一个三角型按扭点击找到"属性生成器"在属性生成器中设置下面属性,在"分页"项中有复选框"允许分页"
需要选中就可以了
设置事件
DataGrid中双击PageIndexChanged事件
protected void dgShowPersonInfo_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.DataGrid1.DataBind();
}
设置排序
DataGrid设置
在DataGrid控件中右上方有一个三角型按扭点击找到"属性生成器"在属性生成器中设置下面属性,
在"常规"项中有复选框"允许排序"需要选中就可以了
事件设计
选中SortCommand事件
在事件中实现的代码
protected void dgShowPersonInfo_SortCommand(object source, DataGridSortCommandEventArgs e)
{
SqlConnection conn = PersonFunction.createConnection();
DataSet ds = new DataSet();
DataView dv = new DataView();
try
{
conn.Open();
string selectString = " select * from personInfo ";
SqlDataAdapter da = new SqlDataAdapter(selectString,conn);
da.Fill(ds,"person");
dv = ds.Tables["person"].DefaultView;
dv.Sort = e.SortExpression;
this.dgShowPersonInfo.DataSource = dv;
this.dgShowPersonInfo.DataBind();
}
catch
{
Response.Write("Error");
Response.End();
}
finally {
conn.Close();
}
}
相关文章推荐
- jquery+ashx无刷新GridView数据显示插件(实现分页、排序、过滤功能)
- jquery+ashx无刷新GridView数据显示插件(实现分页、排序、过滤功能)
- Flex实现分页显示功能(mx:DataGrid) 推荐
- 实现iPhone电子书的分页显示功能(附代码)
- 实现了选择、跳页、排序、导出EXCEL等功能的自定义DataGrid(原码)
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- EasyUI Datagrid 分页的情况下实现点击表头的小三角图标对数据库中所有数据重新排序
- datatables+java实现服务器端分页,排序,查询,列的显示隐藏
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除、导出、突出显示等常见应用实现(cs)
- discuz学习记录:前面的数据用EasyUI DataGrid显示,实现了服务器端分页
- 实现 iPhone 电子书的分页显示功能的代码
- 夺命雷公狗---Redis---7-案例操作2(用户列表页,实现分页显示已经关注功能)完
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- DataGrid实现过多信息鼠标移动到记录上显示,可分页(转)
- 操作DataTable数据实现排序、检索、合并、分页、统计等功能
- (分页、排序等)asp.net 2.0下一个标准GRIDVIEW功能的实现
- 【用Struts2实现列表显示和分页功能】