C#中UniVerse UONET循环记录仅显示首个字段问题排查
Let’s work through this step by step to figure out why only the first field is showing up in your CheckListBox. First, here’s your code for reference:
for(int i = 1; i <= iCnt; i++) { clbRegion.Items.Add(ctrl_record.Extract(i, i, 0), false); }
We can split the problem into two key areas to narrow down the root cause: UniVerse data extraction and VS.NET CheckListBox behavior, since you’re unsure which side is causing the issue.
1. Check if the UniVerse Extract method is returning only the first field
Your Extract(i, i, 0) call is requesting a single field each time (from index i to i). If your record has four fields, you’d expect Extract(2,2,0) to return the second field, Extract(3,3,0) the third, and so on.
To test this, add a quick debug check inside your loop to see what each Extract call returns:
for(int i = 1; i <= iCnt; i++) { var fieldValue = ctrl_record.Extract(i, i, 0); System.Diagnostics.Debug.WriteLine($"Field {i}: {fieldValue}"); // Check output in the Debug window clbRegion.Items.Add(fieldValue, false); }
- If the debug output only shows values for Field 1, and Fields 2-4 are empty or
null, the problem is on the UniVerse side. This could be due to:- The record in UniVerse only contains data in the first field.
- The third parameter (
0) inExtractis restricting results to the first field. Check UniVerse’s .NET driver docs—this parameter often controls extraction modes (like delimiter type or whether to return all fields). - Your
ctrl_recordobject isn’t properly loading all four fields from the UniVerse database.
2. Verify the CheckListBox is behaving as expected
The CheckListBox will only display the exact string you pass to Items.Add(). If you’re only passing the first field’s value, that’s all it will show.
If you want to display all four fields for each item, you need to extract and combine them first. For example:
for(int i = 1; i <= iCnt; i++) { // Extract all four fields for the current record var field1 = ctrl_record.Extract(1, 1, 0); var field2 = ctrl_record.Extract(2, 2, 0); var field3 = ctrl_record.Extract(3, 3, 0); var field4 = ctrl_record.Extract(4, 4, 0); // Combine into a single readable string for display var displayText = $"{field1} | {field2} | {field3} | {field4}"; clbRegion.Items.Add(displayText, false); }
Alternatively, check if Extract has an overload that returns all fields at once (e.g., Extract(1, 4, 0) to get fields 1-4 in one call)—this would be more efficient than four separate calls.
Quick Next Steps
- Run the debug output test to confirm if
Extractis returning data for fields 2-4. - If fields 2-4 are missing, validate your UniVerse record structure and adjust the
Extractparameters as needed. - If fields 2-4 exist but aren’t showing, update your code to combine them into a single display string for the CheckListBox.
内容的提问来源于stack exchange,提问作者ScaryMinds

