在gridview中分页。指定的参数超出了有效值的范围。参数名称:index [英] Paging in gridview. Specified argument was out of the range of valid values. Parameter name: index

查看:121
本文介绍了在gridview中分页。指定的参数超出了有效值的范围。参数名称:index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GridView2_RowDataBound的第一行收到此错误,这是





string str =Category:+ ... .......






aspx code

I am getting this error on first line of GridView2_RowDataBound which is


string str = " Category :" +........



aspx code

<asp:GridView ID="GridView2" runat="server" GridLines="None"
               onrowdatabound="GridView2_RowDataBound" AllowPaging="true" align="center"
        onpageindexchanging="GridView2_PageIndexChanging"  PageSize="4" >
               <AlternatingRowStyle BackColor="White" />
               <RowStyle BackColor="#CCCCCC" />
           </asp:GridView>







.cs代码






.cs code

protected void Page_Load(object sender, EventArgs e)
        {
 if (!IsPostBack)
            {
            Recentinternship();
}
}
 
 protected void Recentinternship()
        {
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            string str = "select B.CompanyLogo as ' ',A.Title,A.InternshipStartDate,A.ApplicationDeadline,A.Duration,A.InternshipCity,A.Category,A.Stipend,A.InternshipID,A.Degree,A.Branch,A.CompanyID,A.Title,A.CompanyName from Internship AS A INNER JOIN  Companies AS B ON A.CompanyID=B.CompanyID ";
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader dr = cmd.ExecuteReader();
 
            GridView2.DataSource = dr;
            GridView2.DataBind();
            //GridView2.BottomPagerRow.Visible = true;

 
            dr.Close();
 
            con.Close();
 
        }
 

 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string str = " Category :" + e.Row.Cells[6].Text + "<br/>Duration :" + e.Row.Cells[4].Text + "<br/>InternshipCity :" + e.Row.Cells[5].Text + "<br/>Stipend :" + e.Row.Cells[7].Text + "<br/>InternshipStartDate :" + e.Row.Cells[2].Text + "<br/>ApplicationDeadline :" + e.Row.Cells[3].Text + "<br/>Required Degree :" + e.Row.Cells[9].Text + "<br/>Required Branch :" + e.Row.Cells[10].Text;
            e.Row.Cells[1].Text = str;
 
            //e.Row.Cells[1].Visible = false;
            //e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;
 

            if (GridView2.Rows.Count > 0)
            {
                GridView2.HeaderRow.Cells[0].Text = " ";
                GridView2.HeaderRow.Cells[1].Text = " ";
                GridView2.HeaderRow.Cells[2].Text = " ";
 

 

            }
            Image img = new Image();
            img.ID = "abc";
            img.ImageUrl = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(img);
 
            Button btn = new Button();
            //btn.Text = "preview";
            e.Row.Cells[2].Controls.Add(btn);
            btn.CausesValidation = false;
            btn.Text = "View and Apply";
            if (Session.Count > 0)
            {
 

                btn.PostBackUrl = "Apply_internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "&CompanyID=" + e.Row.Cells[11].Text + "&Title=" + e.Row.Cells[12].Text + "&CompanyName=" + e.Row.Cells[13].Text + "";
            }
 
            else
            {
 
                btn.PostBackUrl = "Apply_login.aspx";
            }
 
        }
 
  protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            Recentinternship();
        }

推荐答案

string str = " Category :" + e.Row.Cells[6].Text + "<br/>Duration :" + e.Row.Cells[4].Text + "<br/>InternshipCity :" + e.Row.Cells[5].Text + "<br/>Stipend :" + e.Row.Cells[7].Text + "<br/>InternshipStartDate :" + e.Row.Cells[2].Text + "<br/>ApplicationDeadline :" + e.Row.Cells[3].Text + "<br/>Required Degree :" + e.Row.Cells[9].Text + "<br/>Required Branch :" + e.Row.Cells[10].Text;



在这里,您通过 index 读取特定 Cells 的值,我想,您已经写了一些无效的索引。请注意, index 始终从0开始。



调试时检查中的每个值立即窗口知道这些 Cell 索引中是否有正确或错误。


Here you are reading values of particular Cells by index and I guess, you have written some invalid index. Note that index always starts from 0.

While debugging check each value in Immediate Window to know if any of these Cell Index is right or wrong.


看到你在这个事件中缺少一行

See you are missing one line inside this event
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{//You are missing this line here
if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Now you can get the lines
      string str = " Category :" + e.Row.Cells[6].Text + "<br />Duration :" + e.Row.Cells[4].Text + "<br />InternshipCity :" + e.Row.Cells[5].Text + "<br />Stipend :" + e.Row.Cells[7].Text + "<br />InternshipStartDate :" + e.Row.Cells[2].Text + "<br />ApplicationDeadline :" + e.Row.Cells[3].Text + "<br />Required Degree :" + e.Row.Cells[9].Text + "<br />Required Branch :" + e.Row.Cells[10].Text;
            e.Row.Cells[1].Text = str;
 
            //e.Row.Cells[1].Visible = false;
            //e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;
 
 
            if (GridView2.Rows.Count > 0)
            {
                GridView2.HeaderRow.Cells[0].Text = " ";
                GridView2.HeaderRow.Cells[1].Text = " ";
                GridView2.HeaderRow.Cells[2].Text = " ";
 
 
 
 
            }
            Image img = new Image();
            img.ID = "abc";
            img.ImageUrl = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(img);
 
            Button btn = new Button();
            //btn.Text = "preview";
            e.Row.Cells[2].Controls.Add(btn);
            btn.CausesValidation = false;
            btn.Text = "View and Apply";
            if (Session.Count > 0)
            {
 
 
                btn.PostBackUrl = "Apply_internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "&CompanyID=" + e.Row.Cells[11].Text + "&Title=" + e.Row.Cells[12].Text + "&CompanyName=" + e.Row.Cells[13].Text + "";
            }
 
            else
            {
 
                btn.PostBackUrl = "Apply_login.aspx";
            }
 
    }
}



现在看代码。现在我给出了一个rowtype条件

获取更多详细信息

msdn [ ^ ]


这篇关于在gridview中分页。指定的参数超出了有效值的范围。参数名称:index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆