我的代码遇到了一些奇怪的问题。在我的表中,我有58条记录,当我使用消息框运行时,我得到57作为MaxRecord,这是正确的,假设第一条记录以0开始。
当我点击Last_Button
滚动到最后一条记录时,它会显示
第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();
}
}
}
答案 0 :(得分:0)
您的MaxIndex是项目(行)的总数。从0开始,此索引可以达到MaxIndex - 1。
如果MaxIndex为57,则索引为0到56(总计为57)。
因此,在您的代码中,使用MaxIndex-1转到最后一项/行,使用相同的CurrentIndex == MaxIndex-1启用“第一个”按钮。