我有一个可以从我的SQL数据库中加载一些值的表格,它似乎正在正确加载数据。
我的表单允许用户做的一件事是在行列表中来回导航。我已经用Google搜索了一些方法来执行此操作,但是我尝试过的方法没有用。
我还是SQL和C#的新手,所以我有点迷路。有什么好方法可以让我的表单使用后退和前进导航按钮循环浏览数据?
感谢阅读。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
namespace AccountManager
{
public partial class EditCustomer : Form
{
public EditCustomer()
{
InitializeComponent();
}
private void EditCustomer_Load(object sender, EventArgs e)
{
try
{
string conString = "Data Source=.;Initial Catalog=AccountMGR;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
string selectSql = "Select [Id], [AccountRef] ,[BusinessName] ,[BusinessAddress1]" +
",[BusinessAddress2]" +
" ,[BusinessTown]" +
",[BusinessCounty]" +
",[BusinessPostcode]" +
",[BusinessCountry]" +
",[ContactTitle]" +
",[ContactFirstName]" +
",[ContactLastName]" +
",[ContactAddress1]" +
",[ContactAddress2]" +
",[ContactTown]" +
",[ContactCounty]" +
",[ContactPostCode]" +
",[ContactCountry]" +
",[ContactLandline]" +
",[ContactMobile]" +
",[ContactEmail] from Company";
SqlCommand cmd = new SqlCommand(selectSql, con);
try
{
con.Open();
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
AccountRef.Text = (read["AccountRef"].ToString());
AccountName.Text = (read["BusinessName"].ToString());
BusinessAddress1.Text = (read["BusinessAddress1"].ToString());
BusinessAddress2.Text = (read["BusinessAddress2"].ToString());
BusinessTown.Text = (read["BusinessTown"].ToString());
BusinessCounty.Text = (read["BusinessCounty"].ToString());
BusinessPostcode.Text = (read["BusinessPostcode"].ToString());
BusinessCountry.Text = (read["BusinessCountry"].ToString());
ContactTitle.Text = (read["ContactTitle"].ToString());
ContactFirstName.Text = (read["ContactFirstName"].ToString());
ContactLastName.Text = (read["ContactLastName"].ToString());
ContactAddress1.Text = (read["ContactAddress1"].ToString());
ContactAddress2.Text = (read["ContactAddress2"].ToString());
ContactTown.Text = (read["ContactTown"].ToString());
ContactCounty.Text = (read["ContactCounty"].ToString());
ContactPostcode.Text = (read["ContactPostCode"].ToString());
ContactCountry.Text = (read["ContactCountry"].ToString());
ContactLandline.Text = (read["ContactLandline"].ToString());
ContactMobile.Text = (read["ContactMobile"].ToString());
ContactEmail.Text = (read["ContactEmail"].ToString());
}
}
}
finally
{
con.Close();
}
}
finally
{
string conString = "Data Source=.;Initial Catalog=AccountMGR;Integrated Security=True;";
SqlConnection con = new SqlConnection(conString);
string selectSql = "Select Balances.id, Balances.AccountRef, Balance, PreviousBalance, LastInvoiceDate, NextInvoiceDate, InvoiceCycle from Balances inner join Company on Balances.Id = Company.Id where Company.AccountRef = @AccountRef";
SqlCommand cmd = new SqlCommand(selectSql, con);
cmd.Parameters.AddWithValue("@AccountRef", AccountRef.Text.Trim());
try
{
con.Open();
using (SqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
AccountRef.Text = (read["AccountRef"].ToString());
CustomerBalance.Text = (read["Balance"].ToString());
CustomerLastInvoiceDate.Text = (read["LastInvoiceDate"].ToString());
CustomerNextInvoiceDate.Text = (read["NextInvoiceDate"].ToString());
CustomerInvoiceCycle.Text = (read["InvoiceCycle"].ToString());
}
}
}
finally
{
con.Close();
}
}
}
}
}
答案 0 :(得分:1)
我不确定我是否完全理解,但是如果您设置每个控件的'TabIndex'属性,则可以设置按钮或按键事件来循环遍历每个控件/行。
可能看起来像这样。
private void EditCustomer_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
this.ProcessTabKey(true);
}
if (e.KeyCode == Keys.Up)
{
this.ProcessTabKey(false); // False Indicates backwards
}
}