views:

43

answers:

2

How to change the first record to be displayed when a Windows Form opens?

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?

I'm using Designer, so that the data is delivered via a BindingSource.

+1  A: 

Assuming you are using a DataGridView, try

dataSet.Tables[0].AsEnumerable().Select(c => c.Field<string>("AColumn") == "keyN");

on the BindingComplete event.

James
+2  A: 

I like this pattern for programmatically selecting a record on a control bound to a BindingSource:

int position = yourBindingSource.Find("YourFieldName", yourRecordValue);
if (position >= 0) {
    yourBindingSource.Position = position;
}
Jay Riggs