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

C#中 一开始点击“上一行”就溢出,求解?!
左图是我的代码 
中图是我的表 
右图是我一开始运行我的表时候 点击“last”就出错了,但是如果我先按一下“next”再按一下“last”是没有问题的。这是为什么呢? 
帮一下忙,我改怎么修改我的代码呢 

如下是左图代码 
i--; 
if ((i < db.Rows.Count)==false ) 
{ i++; MessageBox.Show("最后一条"); } 

账号栏.Text = db.Rows[i][0].ToString(); 
密码栏.Text = db.Rows[i][1].ToString();

------解决方案--------------------
C# code

        private int _rowIndex;
        public int RowIndex
        {
            get
            {
                return this._rowIndex;
            }
            set
            {
                if (value < 0)
                    this._rowIndex = 0;
                else if (value >= db.Rows.Count)
                    this._rowIndex = db.Rows.Count - 1;
                else
                    this._rowIndex = value;
            }
        }

        private void next_Click(object sender, EventArgs e)
        {
            this.RowIndex++;
        }

        private void last_Click(object sender, EventArgs e)
        {
            this.RowIndex--;
            if (this.RowIndex == db.Rows.Count)
            {
                MessageBox.Show("最后一条");
            }
            帐号栏.Text = db.Rows[this.RowIndex][0].ToString();
            密码栏.Text = db.Rows[this.RowIndex][1].ToString();
        }

------解决方案--------------------
C# code

        private int _rowIndex;
        public int RowIndex
        {
            get
            {
                return this._rowIndex;
            }
            set
            {
                if (value < 0)
                    this._rowIndex = 0;
                else if (value >= db.Rows.Count)
                    this._rowIndex = db.Rows.Count - 1;
                else
                    this._rowIndex = value;
            }
        }

        //第一条
        private void firlt_Click(object sender, EventArgs e)
        {
            this.RowIndex = 0;
        }

        //前一条
        private void pre_Click(object sender, EventArgs e)
        {
            this.RowIndex--;
        }

        //下一条
        private void next_Click(object sender, EventArgs e)
        {
            this.RowIndex++;
        }

        //最后一条
        private void last_Click(object sender, EventArgs e)
        {
            this.RowIndex = db.Rows.Count - 1;

            帐号栏.Text = db.Rows[this.RowIndex][0].ToString();
            密码栏.Text = db.Rows[this.RowIndex][1].ToString();
        }

------解决方案--------------------

 private void last_Click(object sender, EventArgs e)
{
if(i==0)
return;
i--;
if ((i < db.Rows.Count)==false )
{ i++; MessageBox.Show("最后一条"); }

账号栏.Text = db.Rows[i][0].ToString();
密码栏.Text = db.Rows[i][1].ToString();
}