这是我一直关注的一些背景。
http://www.homeandlearn.co.uk/csharp/csharp_s12p9.html
这将转到数据库的最后一条或第一条记录。我想通过在文本框中输入 ID 号跳到 Access 数据库中用户想要的记录,然后正确的行将被放入文本框中。
我想我可以使用上述网站的这段代码。我已经实现了上面网站上的所有其他内容。
全局变量
int inc = 0;
稍后我将在我的“跳过”按钮中调用的导航记录
private void NavigateRecords()
{
DataRow dRow = ds1.Tables["Laptops"].Rows[inc];
txtMaker.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(1).ToString();
txtModel.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(2).ToString();
txtPrice.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(3).ToString();
txtBids.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(4).ToString();
txtScreen.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(5).ToString();
txtCPU.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(6).ToString();
txtMemory.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(7).ToString();
txtHD.Text = ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(8).ToString();
picLaptops.Image = Image.FromFile(ds1.Tables["Laptops"].Rows[inc].ItemArray.GetValue(9).ToString());
}
到目前为止我的跳过按钮...
private void btnSkip_Click(object sender, EventArgs e)
{
NavigateRecords();
}
我很难做到这一点。我知道我想要什么,但缺乏实现它的技术技能。这是非常令人沮丧的。我不知道该怎么办。
如果有人能解决并向我展示代码,我就能理解它并在其他地方使用它。
如果有帮助,这里是下一个按钮的示例,用于转到下一条记录。
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("You have reached the end of available items", "End of Available Items", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
最佳答案
使用数据绑定(bind),而不是手动为控件分配值。
- 创建模型类
public class MyClass
{
public string Maker { get; set; }
public double price { get; set; }
// and so on, for all your fields
}
在 Visual Studio 的“数据源”资源管理器中为 MyClass 添加一个对象数据源。
将字段从数据源拖到您的表单中。 Visual Studio 会自动将 BindingSource 和 BindingNavigator 添加到您的表单。
以以下形式将您的数据分配给 BindingSource:
this.bindingSource1.DataSource = myData;
其中 myData 是 MyClass 对象的一些枚举。
您也可以对数据库源执行此操作。但就我个人而言,我更喜欢将我的数据放在类里面。比直接操作数据集或表单字段更容易处理。
关于c# - 跳转到c#中的数据库记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8371996/