You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过代码为弹窗表格行赋值并从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

  1. Retrieve the underlying DataTable from the dialog's serialtable control. Most WPF/Silverlight data grids bound to a DataTable use a DataView as their ItemsSource, so we can cast to that and get the Table property.
  2. Initialize rows in the dialog's table matching the quantity required from the SubVwr row (based on QtyRcvd).
  3. Assign values to each new row using the DataRow object directly—just like how you update DateRcvd and QtyRcvd in 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 serialtable is bound to a DataTable that 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.TryParse check adds robustness in case QtyRcvd isn't a valid integer—adjust this error handling to fit your app's needs.
  • If your SerialInputDialogWindow already initializes the serialtable with rows, you can skip the Clear() and NewRow() steps and instead loop through existing rows to update values (but make sure the count matches QtyRcvd).

内容的提问来源于stack exchange,提问作者user9236349

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:30:54