打印本文 打印本文 关闭窗口 关闭窗口
asp.net中显示DataGrid控件列序号的几种方法
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3689  更新时间:2009/4/23 10:46:04  文章录入:mintao  责任编辑:mintao
              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] [5] 

打印本文 打印本文 关闭窗口 关闭窗口