您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

asp.net中显示DataGrid控件列序号的几种方法

作者:作者:未…    信息学院来源:网络收集    点击数:    更新时间:2006-8-28 我要参与讨论

  dEventArgs e)

{

DataGrid1.CurrentPageIndex = e.NewPageIndex;

DataGridDataBind();

}

//获取当前项

protected int GetRecordIndex(int itemIndex)

{

return (DataGrid1.CurrentPageIndex * DataGrid1.PageSize + itemIndex + 1);

}

private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

DataGrid dg = (DataGrid)sender;

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{

e.Item.Cells[0].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();

e.Item.Cells[1].Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();

}

}



private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

{

DataGrid dg = (DataGrid)sender;

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{

((Label)e.Item.FindControl("itemIndex")).Text = (dg.CurrentPageIndex * dg.PageSize + e.Item.ItemIndex + 1).ToString();

}

}

}

}



数据层代码如下:

using System;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;



namespace AspnetPaging

{

public class DataAccess

{

private static string connString = ConfigurationSettings.AppSettings["ConnString"];

private DataAccess()

{



}



public static DataSet GetCustomersData()

{

SqlConnection conn = new SqlConnection(connString);

SqlCommand comm = new SqlCommand("GetCustomers",conn);

comm.CommandType = CommandType.StoredProcedure;

SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);

DataSet ds = new DataSet();

dataAdapter.Fill(ds);

return ds;

}

}

}



总结,上面的四种方法前两种其实处理起来是一样的,只是处理的时间不同而已;对于第三种我认为最简单,直接在前台页面绑定,不需要额外的辅助;对于第四种的方法绑定到前台我认为最为灵活,需要注意的是GetRecordIndex方法需要protected或public,使它的继承类能访问的到。

上一页  [1] [2] [3] [4] 

在google里搜索更多asp.net中显示DataGrid控件列序号的几种方法

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明