我有一个直接与设计器创建的表适配器绑定的表单。我还创建了一个列表框,其中包含每条记录的键值。我的想法是允许用户单击特定键以在编辑屏幕中加载所需的记录。我可以在单击时访问列表条目,但我不知道使用什么命令移动到正确的行。
我想知道是否真的值得使用这样的绑定表单,或者只是在代码中执行,类似于我创建列表框的方式。有什么建议么?请参阅下面的代码:
public partial class Customer : Form
{
public string dbConString = "Data Source=localhost\\BALLMILL;Initial Catalog=Ballmill;Integrated Security=True";
public SqlConnection dbCon = null;
public SqlDataReader dbRdr = null;
public SqlCommand dbCommand = null;
public Customer()
{
InitializeComponent();
}
private void cUSTOMERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.cUSTOMERBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.bML_WMS245GDataSet);
}
private void Customer_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'bML_WMS245GDataSet.CUSTOMER' table. You can move, or remove it, as needed.
this.cUSTOMERTableAdapter.Fill(this.bML_WMS245GDataSet.CUSTOMER);
SqlConnection dbCon = new SqlConnection(dbConString);
SqlDataReader rdrCustomers = null;
try
{
dbCon.Open();
SqlCommand sqlCustomers = new SqlCommand("SELECT CustomerCode FROM Customer", dbCon);
rdrCustomers = sqlCustomers.ExecuteReader();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error accessing database");
return;
}
while (rdrCustomers.Read())
{
listCustomers.Items.Add(rdrCustomers["CustomerCode"].ToString());
}
}
private void listCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(listCustomers.SelectedItem.ToString(), listCustomers.SelectedIndex.ToString());
}
}