我即将放弃.连续5个小时尝试了很多东西.没有什么事发生在我身上.
我有一个C#应用程序,可以添加记录,从数据库中删除记录.它还可以显示文本框中的项目,我可以使用下一个和上一个按钮导航项目.如下面的Next按钮,
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); } }
MaxRows就是这样
MaxRows = ds1.Tables["Laptops"].Rows.Count;
我上面调用的方法是NavigateRecords,此代码在这里
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()); }
我的Access数据库中有一个AutoNumber ID字段,用户应该能够跳过(通过在文本框中输入ID)并在不同的文本框中显示相应的记录(请参阅NavigateRecords)
我的问题是,我怎么做呢?我需要在NavigateRecords的文本框中显示正确的行,这也将更新全局设置为0的"inc"变量.例如,如果用户启动程序并跳过说id 7,它将显示该记录但是当它这样做的时候,inc也应该用行号更新,这样如果我点击下一步,它将带我到id 8而不是id 2 ...如果这是有道理的.到目前为止我有这个.
private void btnSkip_Click(object sender, EventArgs e) { string searchFor = txtSkip.Text; DataRow[] Skip; int results = 0; Skip = ds1.Tables["Laptops"].Select("ID='" + searchFor + "'"); results = Skip.Length; if (results > 0) { DataRow dr1; for (int i = 0; i < MaxRows; i++) { // who knows what to do here.... or getting every row is even the right thing to do... } } else { MessageBox.Show("No item found"); } }
sq33G.. 6
修改您的第二个代码示例:
private void btnSkip_Click(object sender, EventArgs e) { string searchFor = txtSkip.Text; DataRow[] target = ds1.Tables["Laptops"].Select("ID='" + searchFor + "'"); // I suspect that this should really not be a string, so ("ID=" + searchFor); if (target.Count() == 1) { inc = ds1.Tables["Laptops"].Rows.IndexOf(target[0]); NavigateRecords(); } else { //there can be no more than one row, so this means no matches MessageBox.Show("No item found"); } }
...这假设ID是表中的唯一标识符.
修改您的第二个代码示例:
private void btnSkip_Click(object sender, EventArgs e) { string searchFor = txtSkip.Text; DataRow[] target = ds1.Tables["Laptops"].Select("ID='" + searchFor + "'"); // I suspect that this should really not be a string, so ("ID=" + searchFor); if (target.Count() == 1) { inc = ds1.Tables["Laptops"].Rows.IndexOf(target[0]); NavigateRecords(); } else { //there can be no more than one row, so this means no matches MessageBox.Show("No item found"); } }
...这假设ID是表中的唯一标识符.