现在用得前台是  <ItemTemplate>
              <%# Container.DataItemIndex + 1%>
            </ItemTemplate>
得出的序号是
1
2
3
4
5
现在想得到序号
5
4
3
2
1
该怎么做?在网上找了很多方法都行不通,如<%#this.GridView2.Rows.Count - Container.DataItemIndex%>得到的结果是
0
0
0
0
0求大侠们帮忙了~

解决方案 »

  1.   

    你在数据源那里处理
    DataTalble那里增加一列,遍历给num赋值就行了
      

  2.   

    <%#this.GridView2.PageSize - Container.DataItemIndex %>
    试试
      

  3.   

    做个全局变量
    public int AllCount=0;在你绑定前给ALLCount附值为获取到的所有行使用
    <%#ALLCount- Container.DataItemIndex %>  把..
      

  4.   

    gridview没有ItemTemplate吧.在repeater中可以this.Repeater1.Items.Count得到行数
      

  5.   

    this.GridView2.Rows.Count--
    能行不
      

  6.   


    这个貌似不行啊。。得出的结果和Container.DataItemIndex 是一样的,两个一相减就等于零了刚才睡了一觉,多谢大家回帖啊。。
      

  7.   


    我的那个绑定语句是在前台<asp:gridviw></gridview>中间定义的,您说的全局变量AllCount要在cs文件里面定义吗?具体怎么赋值给它呢?
      

  8.   

    我在前台gridview的属性中,设定OnRowDataBound="GridView2_RowDataBound",后台中写 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
            }
            if (e.Row.RowIndex != -1)
            {
                int id = GridView2.Rows.Count-e.Row.RowIndex;
                e.Row.Cells[0].Text = id.ToString();
            }    }
    最后得出的序号还是全部为“0”,请问怎样赋值给allcount呢Gridview2.rows.count取的好像也不是所有显示信息的条数求助,这问题真棘手。。网友“zgke”的方法
      

  9.   

    笨办法就是先得到总数,然后用总数-Container.DataItemIndex
      

  10.   


    现在我的思路已经有了,加了一个label ,zongshu,试了两种方法好像都不行
    1、SqlDataReader dr = bm.selectSTconditions(conditions);
            while (dr.Read())
                count++;
            zongshu.Text = count.ToString(); 
    //对sqldatareader遍历,然后得出count值,然后赋给label
            SqlDataReader dr = bm.selectSTconditions(conditions);但是遇到的问题是,好像sqldatareader遍历到结尾之后,再绑定就会出现问题gridview没有一个记录能显示出来了另一种方法我是这样处理的——
    SqlDataReader dr = bm.selectSTconditions(conditions);
    GridView2.DataSource = dr;
    zongshu.Text = dr.rows.count.tostring();但是这样的结果也不行,因为在gridview.datasource绑定的时候就会初始化gridview,而此时我的zongshu.text还没有值,使得下面出错
     protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
              
                int id2 = Convert.ToInt32(zongshu.Text); //此地出错,zongshu.text没有值
                int id1 =  id2 - e.Row.RowIndex; //用总数减去当前数,使得序号倒序显示
                e.Row.Cells[0].Text = id1.ToString(); 
            }    }怎么解决呢,哪种简单一些。。
      

  11.   

    在Sql语句类操作不行吗?select idenity(1,1),× from table 
      

  12.   

    // public static int i = 0;
        protected void grdvOutbox_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = Convert.ToString(grdvOutbox.Rows.Count + 1);
                //e.Row.Cells[0].Text = Convert.ToString(++i);
            }
        }
    用外边那个全局变量也可以完成,就是注释掉的那两行