日期:2014-05-17  浏览次数:21458 次

System.ArgumentOutOfRangeException: 索引超出范围,求教
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小
参数名: index

源错误: 

行 169: e.Row.Attributes["onmouseover"] = "ItemOver(this)";  
行 170: string id = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
行 171: e.Row.Attributes.Add("onclick", "alert('" + id + "');"); //鼠标单击取行号  
行 172: e.Row.Attributes.Add("style", "cursor:hand");

DataKeyNames设置了,不知道错在哪里了,问的问题太多了,没有分了,感谢大家,谢谢!!!

C# code

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillGridView();
        }
    }
    // 填充GridView
    protected void fillGridView()
    {
        DataSet ds = new DataSet();
        ds = GetData();
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[]{"id"};
        GridView1.DataBind();
        getRefSet(ds);
    }
    // 分页的相关参数设置
    protected void getRefSet(DataSet ds)
    {
        lblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
        lblPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";
        lblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";

        if (ds.Tables[0].Rows.Count == 0)
        {
            lnkbtnFirst.Visible = false;
            lnkbtnPre.Visible = false;
            lnkbtnNext.Visible = false;
            lnkbtnLast.Visible = false;

            lblCurrentIndex.Visible = false;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
        }
        else if (GridView1.PageCount == 1)
        {
            lnkbtnFirst.Visible = false;
            lnkbtnPre.Visible = false;
            lnkbtnNext.Visible = false;
            lnkbtnLast.Visible = false;
        }
        // 设置LinkButton此时对应的参数
        lnkbtnFirst.CommandArgument = "1";
        lnkbtnPre.CommandArgument = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
        lnkbtnNext.CommandArgument = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
        lnkbtnLast.CommandArgument = GridView1.PageCount.ToString(); 
    }
   // 跳转到指定页面,页面参数由txtJumpPage获取
    protected void lnkbtnJumpPage_Click(object sender, EventArgs e)
    {
        GridView1.PageIndex = int.Parse(txtJumpPage.Text) - 1;
        lblCurrentIndex.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
        fillGridView();
        GridView1.DataBind();

    }   
   // 首页,上一页,下一页,尾页公用的点击程序  
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;
        fillGridView();
        GridView1.DataBind();

    } 
    // 转换SQL查询结果为DataSet
    private DataSet GetData()
    {
        OracleConnection conn = new OracleConnection("Data Source=wgyx;user=wgyx;password=wgyx;");
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        String strSql = "select id,fuid,the from sys_organ";
        cmd.CommandText = strSql;
        cmd.CommandType = CommandType.Text;
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            OracleDataAdapter ad = new OracleDataAdapter();
            ad.SelectCommand = cmd;
            ad.Fill(ds);

        }
        catch (Exception ex)
        {