protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindgrvTopic();
            }
        }        protected void grvTopic_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvTopic.PageIndex = e.NewPageIndex;
            BindgrvTopic();
        }
        protected void BindgrvTopic()
        {
                this.grvTopic.DataKeyNames = new string[] { "TOPICID" }; 
               grvTopic.DataSource = profileservice.GetAll();
                grvTopic.DataBind();
            }
            catch (Exception exc)
            {
                Logger.Write(exc.Message.ToString(), "TopicView");
            }
        }
        protected void grvTopic_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int topicId = 0;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.DataItemIndex > -1)
                {
                    topicId = Convert.ToInt32(grvTopic.DataKeys[e.Row.DataItemIndex].Value);                    ImageButton btn1 = e.Row.Cells[0].FindControl("btn1") as ImageButton;
                    Label lbl = e.Row.Cells[0].FindControl("lbl") as Label;
                    Image img1 = e.Row.Cells[0].FindControl("img1") as Image;
                    Image img2 = e.Row.Cells[0].FindControl("img2") as Image;
                    if (topicService.GetByTopicId(topicId).TopicLevel.ToString() == ConstService.MAINTOPICLEVEL)
                    {
                        if (topicService.GetSubTopic(topicService.GetByTopicId(topicId).TopicName).Count > 0)
                        {
                            btn1.Visible = true;
                        }
                        else
                        {
                            img2.Visible = true;
                        }
                    }
                    else
                    {
                        e.Row.Visible = false;
                        img1.Visible = true;
                    }
                }
            }
        }
分页之后,红色的那里就会报错,说argument out of range
求各位达人帮帮我~~

解决方案 »

  1.   

     topicId = Convert.ToInt32(grvTopic.DataKeys[e.Row.DataItemIndex].Value); 
    是这行,不好意思,发了两遍
      

  2.   

     topicId = Convert.ToInt32(grvTopic.DataKeys[e.Row.DataItemIndex].Value); 
    是这行,不好意思,发了两遍
      

  3.   

    分页后每次只是把当前页面记录数的数据绑定到GridView上。DataItemIndex是指,比如你有79条数据,那就是0~79。而你想,如果PageSize=10的情况下,GridView.DataKeys[这里的Index合理范围是:0~9]。OK?
      

  4.   

    SORRY,80条数据时,DataItemIndex:0~79。GridView.DataKeys[这里应该是e.Row.DisplayIndex]
      

  5.   

    在你没有利用所谓的利用存储过程的自定义分页时,GridView每次翻页都会从后台数据库把所有记录都读出来。比如你只显示10条记录/页在GridView上,但每次它都会在后台把所有数据读出来,然后根据PageIndex,把符合的10条绑定到GridView上。注意,只有10条!而DataItemIndex是全部数据的索引
      

  6.   

    GridView1.Datakeys[e.Row.RowIndex],再次SORRY。我有时也搞混。