如何通过代码为弹窗表格行赋值并从SubVwr获取数据?
Correct Syntax to Assign Values to Popup Dialog Table Rows
It looks like the issue with your current code is twofold: first, the dialog's serialtable probably doesn't have any rows initialized when you try to loop through its ItemsSource, so your inner loop does nothing. Second, even if there were rows, accessing DataRowView values directly might not work as expected if you're not properly referencing the underlying data structure.
Here's how to fix this, following the pattern you used successfully with the SubVwr table:
Step-by-Step Explanation
- Retrieve the underlying DataTable from the dialog's
serialtablecontrol. Most WPF/Silverlight data grids bound to a DataTable use aDataViewas their ItemsSource, so we can cast to that and get the Table property. - Initialize rows in the dialog's table matching the quantity required from the SubVwr row (based on
QtyRcvd). - Assign values to each new row using the
DataRowobject directly—just like how you updateDateRcvdandQtyRcvdin your working example.
Corrected Code
private void requestSerials() { // Check which items require serial numbers and request them foreach (DataRow subRow in SubVwr.Tables[0].Tbl.Rows) { if (subRow["TraceReq"].ToString() == "Y") { SerialInputDialogWindow dialog = new SerialInputDialogWindow() { Topmost = true }; // Get the DataTable behind the dialog's serialtable control DataView serialDataView = dialog.serialtable.ItemsSource as DataView; if (serialDataView != null) { DataTable serialTable = serialDataView.Table; // Clear any existing rows (adjust this if your dialog persists data) serialTable.Rows.Clear(); // Get how many serials we need to collect int quantityNeeded; if (int.TryParse(subRow["QtyRcvd"].ToString(), out quantityNeeded)) { // Add a row for each serial required for (int i = 0; i < quantityNeeded; i++) { DataRow newSerialRow = serialTable.NewRow(); // Populate values from the SubVwr row to the dialog's row newSerialRow["PN"] = subRow["PN"].ToString(); // Use your actual column name // Add other fields you need to carry over, e.g.: // newSerialRow["PartDesc"] = subRow["PartDesc"].ToString(); serialTable.Rows.Add(newSerialRow); } } else { // Handle invalid QtyRcvd value if needed MessageBox.Show("Invalid quantity value for traceable item."); } } dialog.ShowDialog(); } } }
Key Notes
- Ensure the dialog's
serialtableis bound to aDataTablethat has the same column names (like "PN") you're trying to populate. If the columns don't exist, you'll get a runtime error. - The
int.TryParsecheck adds robustness in caseQtyRcvdisn't a valid integer—adjust this error handling to fit your app's needs. - If your
SerialInputDialogWindowalready initializes theserialtablewith rows, you can skip theClear()andNewRow()steps and instead loop through existing rows to update values (but make sure the count matchesQtyRcvd).
内容的提问来源于stack exchange,提问作者user9236349
相关产品推荐
相关产品推荐

