How to change the first record to be displayed when a Windows Form opens?
如何更改Windows窗体打开时要显示的第一条记录?
I've got a form that retreives data from a table Table1
within a DataSet Dataset1
and fills a Details control with the data. When the form is executed, the first record from table1
shows up in the text fields. How do I change the code so that it displays the record with the key keyN
?
我有一个表单从DataSet Dataset1中的表Table1中检索数据,并用数据填充Details控件。执行表单时,表1中的第一条记录显示在文本字段中。如何更改代码以便使用密钥N显示记录?
I'm using Designer, so that the data is delivered via a BindingSource.
我正在使用Designer,因此数据通过BindingSource传递。
2 个解决方案
#1
I like this pattern for programmatically selecting a record on a control bound to a BindingSource:
我喜欢这种模式,以编程方式选择绑定到BindingSource的控件上的记录:
int position = yourBindingSource.Find("YourFieldName", yourRecordValue);
if (position >= 0) {
yourBindingSource.Position = position;
}
#2
Assuming you are using a DataGridView, try
假设您正在使用DataGridView,请尝试
dataSet.Tables[0].AsEnumerable().Select(c => c.Field<string>("AColumn") == "keyN");
on the BindingComplete event.
在BindingComplete事件上。
#1
I like this pattern for programmatically selecting a record on a control bound to a BindingSource:
我喜欢这种模式,以编程方式选择绑定到BindingSource的控件上的记录:
int position = yourBindingSource.Find("YourFieldName", yourRecordValue);
if (position >= 0) {
yourBindingSource.Position = position;
}
#2
Assuming you are using a DataGridView, try
假设您正在使用DataGridView,请尝试
dataSet.Tables[0].AsEnumerable().Select(c => c.Field<string>("AColumn") == "keyN");
on the BindingComplete event.
在BindingComplete事件上。