How you insert the line (and therefore all the code) isn't really relevant to your questions. What you see is the expected behavior of the standard logic. You can try it in standard forms, such as purchase orders. Select a line (not the first one) and press Refresh.
I'm assuming that your data sources are delayed-linked.
1. When the header gets refresh and the query for lines gets executed, the information about the selected line isn't preserved.
2. Either the first or the last record gets selected, depending on the configuration of the data source.
3. As I already mentioned, it has nothing to do with line insertion; the problem is the same even if you select an existing line and no insertion is involved. You'd need to change the logic of the data refresh action.
4. Nothing to check - it's the expected behavior.
Although your code isn't related to your question, let me use this opportunity to show you how you can improve it.
A half of your code can be thrown away when you realize that the form already contains (automatically created) variables for data sources and their buffers. Let's use them:
public void newLine()
{
if (!orderHeader.OrderId)
{
warning("Please create header first.");
return;
}
orderLine.clear();
orderLine.OrderId = header.OrderId;
orderLine.SomeCode = header.SomeCode;
orderLine.insert();
// Force UI to show the new record
orderLine_ds.research(true);
orderLine_ds.findRecord(orderLine);
}
You also should call validateWrite() method, to verify that the record can be saved, otherwise you risk inserting inconsistent data. You should move your validation of OrderId there, if you require using your custom message instead of just letting the standard validation handle it for you.