我有一个与设计器创建的表适配器直接绑定的表单。我还创建了一个列表框,其中包含每个记录的键值。我的想法是允许用户单击特定的键以在编辑屏幕中加载所需的记录。我可以单击访问列表条目,但是我不知道该使用什么命令移动到正确的行。
我想知道是否值得使用这样的绑定形式,或者只是用代码来完成(类似于我创建列表框的方式)。有什么建议么?请参阅下面的代码:
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());
}
}
答案 0 :(得分:0)
搜索设计器代码后,我找到了答案。
cUSTOMERBindingSource.Position = listCustomers.SelectedIndex;
这将自动将导航器移至所选行。