C# 跳到c语言中的数据库记录
以下是我一直关注的一些背景 这将转到数据库的最后一条或第一条记录。我想通过在文本框中输入ID号,跳到access数据库中用户想要的记录,然后将正确的行放入文本框中 我想我可以使用上述网站上的代码。我已经从上面的网站实现了所有其他功能 全局变量C# 跳到c语言中的数据库记录,c#,database,ms-access,record,skip,C#,Database,Ms Access,Record,Skip,以下是我一直关注的一些背景 这将转到数据库的最后一条或第一条记录。我想通过在文本框中输入ID号,跳到access数据库中用户想要的记录,然后将正确的行放入文本框中 我想我可以使用上述网站上的代码。我已经从上面的网站实现了所有其他功能 全局变量 int inc = 0; 导航记录,稍后我将在跳过按钮中调用它 private void NavigateRecords() { DataRow dRow = ds1.Tables["Laptops"].Rows[inc]; t
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);
}
}
根据你的描述,我想这就是你想要的
void btnNext_Click(object sender, EventArgs e) {
int id = Int32.Parse(yourIdTextBox.Text);
DataRow row = ds1.Tables["Laptops"].Rows.OfType<DataRow>()
.SingleOrDefault(r => (int)r.ItemArray[your id index] == id);
if (row == null)
{
//user typed in invalid row id
} else
processRow(row);
}
void processRow(DataRow row){
txtMaker.Text = row.ItemArray.GetValue(1).ToString();
txtModel.Text = row.ItemArray.GetValue(2).ToString();
txtPrice.Text = row.ItemArray.GetValue(3).ToString();
txtBids.Text = row.ItemArray.GetValue(4).ToString();
txtScreen.Text = row.ItemArray.GetValue(5).ToString();
txtCPU.Text = row.ItemArray.GetValue(6).ToString();
txtMemory.Text = row.ItemArray.GetValue(7).ToString();
txtHD.Text = row.ItemArray.GetValue(8).ToString();
picLaptops.Image = Image.FromFile(row.ItemArray.GetValue(9).ToString());
}
根据你的描述,我想这就是你想要的
void btnNext_Click(object sender, EventArgs e) {
int id = Int32.Parse(yourIdTextBox.Text);
DataRow row = ds1.Tables["Laptops"].Rows.OfType<DataRow>()
.SingleOrDefault(r => (int)r.ItemArray[your id index] == id);
if (row == null)
{
//user typed in invalid row id
} else
processRow(row);
}
void processRow(DataRow row){
txtMaker.Text = row.ItemArray.GetValue(1).ToString();
txtModel.Text = row.ItemArray.GetValue(2).ToString();
txtPrice.Text = row.ItemArray.GetValue(3).ToString();
txtBids.Text = row.ItemArray.GetValue(4).ToString();
txtScreen.Text = row.ItemArray.GetValue(5).ToString();
txtCPU.Text = row.ItemArray.GetValue(6).ToString();
txtMemory.Text = row.ItemArray.GetValue(7).ToString();
txtHD.Text = row.ItemArray.GetValue(8).ToString();
picLaptops.Image = Image.FromFile(row.ItemArray.GetValue(9).ToString());
}
使用数据绑定,而不是手动为控件指定值 创建一个模型类 公共类MyClass { 公共字符串生成器{get;set;} 公共双价{get;set;} //等等,你所有的领域 } 在Visual Studio的数据源资源管理器中为MyClass添加对象数据源 将数据源中的字段拖到表单中。Visual Studio会自动将BindingSource和BindingNavigator添加到表单中 将数据分配给BindingSource,格式如下: this.bindingSource1.DataSource=myData; 其中myData是MyClass对象的一些枚举
您也可以对数据库源执行此操作。但就我个人而言,我更喜欢把我的数据放在课堂上。比直接操作数据集或表单字段更容易处理。使用数据绑定,而不是手动为控件赋值 创建一个模型类 公共类MyClass { 公共字符串生成器{get;set;} 公共双价{get;set;} //等等,你所有的领域 } 在Visual Studio的数据源资源管理器中为MyClass添加对象数据源 将数据源中的字段拖到表单中。Visual Studio会自动将BindingSource和BindingNavigator添加到表单中 将数据分配给BindingSource,格式如下: this.bindingSource1.DataSource=myData; 其中myData是MyClass对象的一些枚举
您也可以对数据库源执行此操作。但就我个人而言,我更喜欢把我的数据放在课堂上。比直接操作数据集或表单字段更容易处理。我遇到了这个错误。无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Data.DataRow”。存在显式转换您是否缺少强制转换?它可以工作,但我应该更具体一点。这是我的错。如果我在跳转到某个id后按“下一步”或“上一步”按钮,是否仍然需要修改您的代码以使inc不会从0开始?例如,如果我跳到ID号9,它将显示该项目,但当我单击“下一步”时,它将转到ID号2,而不是ID号10,如果这使唤醒获得此错误。无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Data.DataRow”。存在显式转换您是否缺少强制转换?它可以工作,但我应该更具体一点。这是我的错。如果我在跳转到某个id后按“下一步”或“上一步”按钮,是否仍然需要修改您的代码以使inc不会从0开始?例如,如果我跳到ID号9,它将显示该项目,但当我单击“下一步”时,它将转到ID号2,而不是ID号10,如果这有道理的话。格林:不客气。另外,由于您是StackOverflow的新手,我想通知您,您可以通过勾选答案旁边的勾号来接受对您帮助最大的答案。格雷金斯:不客气。另外,由于您是StackOverflow的新手,我想通知您,您可以通过勾选答案旁边的勾号来接受对您帮助最大的答案。