C# C:位置上没有行

C# C:位置上没有行,c#,data-binding,C#,Data Binding,我的代码遇到了一些奇怪的问题。在我的表中,我有58条记录,当我使用messagebox运行它时,我得到57条作为MaxRecord,这是正确的,假设第一条记录以0开头 当我点击Last_按钮滚动到最后一条记录时,它说 第57位没有行 我的代码有问题吗?谢谢 namespace ClassLibrary1 { public partial class myfunction : Form { public int CarNumber { get; set; }

我的代码遇到了一些奇怪的问题。在我的表中,我有58条记录,当我使用messagebox运行它时,我得到57条作为MaxRecord,这是正确的,假设第一条记录以0开头

当我点击Last_按钮滚动到最后一条记录时,它说

第57位没有行

我的代码有问题吗?谢谢

namespace ClassLibrary1
{
    public partial class myfunction : Form
    {
        public int CarNumber { get; set; }
        public kilnRec CurrentKilnRec { get; set; }
        public int CurrentIndex { get; set; }
        public int LastIndex { get; set; }
        public int MaxIndex { get; set; }
        public string CarNo { get; set; }

        public myfunction()
        {
            InitializeComponent();
        }

        private void myfunction_Shown(object sender, EventArgs e)
        {
            try
            {
                LoadDB();
                ResetButton();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                throw ex;
            }
        }

        private void myfunction_Load(object sender, EventArgs e)
        {
            LastIndex = 0;
        }

        public OleDbConnection DBConnection { get; set; }
        public DataTable mytableTable { get; set; }

        private void LoadDB()
        {
            DBConnection = new OleDbConnection(DbManager.DbConnectionString);
            DBConnection.Open();
            var oleDBCmd = new OleDbCommand("SELECT * FROM mytable WHERE rec_no >= -9 AND rec_no <= 48 ORDER BY rec_no", DBConnection)
            {
                CommandType = CommandType.Text
            };
            var oleDBDataAdapter = new OleDbDataAdapter(oleDBCmd);
            var dataset = new DataSet();
            oleDBDataAdapter.Fill(dataset);
            mytableTable = dataset.Tables[0];
            MaxIndex = dataset.Tables[0].Rows.Count;
            MessageBox.Show(Convert.ToString(MaxIndex));
            CurrentIndex = LastIndex;
            CurrentKilnRec = new kilnRec();
            SetRecord();
        }

        private void SetRecord()
        {
            var CurrentRow = mytableTable.Rows[CurrentIndex];
            this.tbCarPos.Text = Convert.ToString(CurrentRow["rec_no"]);
            this.tbCarNumber.Text = Convert.ToString(CurrentRow["carno"]);
            this.tbProdName.Text = Convert.ToString(CurrentRow["prodname"]);
            this.tbQuantity.Text = Convert.ToString(CurrentRow["quantity"]);
            this.tbWeight.Text = Convert.ToString(CurrentRow["weight"]);
        }

        private void ResetButton()
        {
            bt_First.Enabled = true;
            bt_Prior.Enabled = true;
            bt_Next.Enabled = true;
            bt_Last.Enabled = true;

            if (CurrentIndex == 0)
            {
                bt_First.Enabled = false;
                bt_Prior.Enabled = false;
                bt_Next.Enabled = true;
                bt_Last.Enabled = true;
            }

            if (CurrentIndex == MaxIndex)
            {
                bt_First.Enabled = true;
                bt_Prior.Enabled = true;
                bt_Next.Enabled = false;
                bt_Last.Enabled = false;
            }
        }

        private void bt_First_Click(object sender, EventArgs e)
        {
            CurrentIndex = 0;
            SetRecord();
            ResetButton();
        }

        private void bt_Prior_Click(object sender, EventArgs e)
        {
            CurrentIndex--;
            SetRecord();
            ResetButton();
        }

        private void bt_Next_Click(object sender, EventArgs e)
        {
            CurrentIndex++;
            SetRecord();
            ResetButton();
        }

        private void bt_Last_Click(object sender, EventArgs e)
        {
            CurrentIndex = MaxIndex;
            SetRecord();
            ResetButton();
        }
    }
}

MaxIndex是itemsrows的总数。从0开始,该索引可以上升到MaxIndex-1

如果MaxIndex为57,则索引从0到56,总计为57


因此,在代码中,使用MaxIndex-1转到最后一项/行,使用相同的CurrentIndex==MaxIndex-1启用第一个按钮。

每隔一个按钮工作正常?这是正常的。MaxIndex=dataset.Tables[0].Rows.Count;给你一个计数。因此,对于基于0的集合。最后一项是Count-1Ok,我想如果我的数据库中正好有58条记录,那么我的最后一条记录将是MaxIndex=57,第一条记录将是MaxIndex=0。我认为没有recno==0。因此,您的查询将返回48+9条记录。感谢Alexandre的解释。