当前位置:编程学习 > C#/ASP.NET >>

索引超出范围。必须为非负值并小于集合大小。

索引超出范围。必须为非负值并小于集合大小。
参数名: index

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
    string   w = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();    }
错误指向这里


    protected void Button1_Click(object sender, EventArgs e)
    {
        int s = Convert.ToInt32(Session ["score"]);
              sqlcon.Open();
           string sql = "update [user] set score=score+20 where  UserName='" + w +"'";

                SqlCommand mycmd = new SqlCommand(sql, sqlcon);
                mycmd.Parameters.Add("@s", SqlDbType.Int);
                mycmd.Parameters["@s"].Value = s;

                mycmd.ExecuteNonQuery();
                Response.Write("<script>alert('成功');location='index.aspx'</script>");
                sqlcon.Close();
} --------------------编程问答-------------------- e.Row判断下他的类型 要不是头部貌似是e.Row.DataType(忘了怎么写啦 好像不是DataType)、

--------------------编程问答-------------------- 检查GridView的DataKeyNames 属性是否设置 --------------------编程问答-------------------- 加上
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
 } --------------------编程问答-------------------- 加上
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
 }
判断是不是数据行 --------------------编程问答--------------------
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
  {
     if (e.Row.RowIndex != -1)
       {     string w = GridView1.DataKeys[e.Row.RowIndex].Value.ToString(); }
     }
--------------------编程问答-------------------- 或判断DataControlRowType.DataRow
--------------------编程问答--------------------
引用 3 楼 porschev 的回复:
加上
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
 }

正解 --------------------编程问答--------------------
引用 4 楼 porschev 的回复:
加上
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
 }
判断是不是数据行


我是选择gridview的列值 加个按钮 更改数据库字段 程序:


public partial class jietie : System.Web.UI.Page
{
    SqlConnection sqlcon = new SqlConnection("DataBase=localhost;Initial Catalog=TCBBS;Integrated Security=SSPI;");
    string w;
    protected void Page_Load(object sender, EventArgs e)
    {
        int idStr = Convert.ToInt32(Request.QueryString["cardid"]);
       
        sqlcon.Open();
        string sql1 = "select * from revertview where cardid='" + idStr + "'";
        SqlCommand myCommand = new SqlCommand(sql1, sqlcon);

        SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

        GridView1.DataSource = result;
        GridView1.DataBind();
        sqlcon.Close();
    }
 protected void Button1_Click(object sender, EventArgs e)
    {    string w;
        int s = Convert.ToInt32(Session ["score"]);
              sqlcon.Open();
           string sql = "update [user] set score=score+20 where  UserName='" + w+"'";
                         SqlCommand mycmd = new SqlCommand(sql, sqlcon);
                mycmd.Parameters.Add("@s", SqlDbType.Int);
                mycmd.Parameters["@s"].Value = s;

                mycmd.ExecuteNonQuery();
                Response.Write("<script>alert('OK');location='index.aspx'</script>");
                sqlcon.Close();
}

        protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
        w = GridView1.DataKeys[e.Row.RowIndex].Value.ToString(); }
    }




给名字为W的加20分。加不进去啊
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,