这是 C# Winforms。
我在这段代码中有一个字符串列表:
List<string> WaveAndPickFields = new List<string>(new string[] { "ID", "TransactionID", "OrderNumber", "WarehouseID", "StoreOrderNumber", "OrderType", "CustomerID", "CustomerPONumber", "DepartmentCode", "LoadNumber", "ReverseLoadSequence", "BOLNumber", "MasterBOLNumber", "ProNumber", "Carrier", "CarrierSCAC", "FreightTerms", "RushOrderFlag", "OrderEntryDate", "ExpectedCarrierArrivalDate", "DatePromised", "OrderWeight", "OrderCubicVolume", "OrderCartonCounts", "ShippingShort", "PrepaidFreight", "CODAmount", "InsuranceAmount", "PIPAmount", "TotalFreightCost", "ShipToCustomerCode", "ShipToName", "ShipToAddress1", "ShipToAddress2", "ShipToAddress3", "ShipToCity", "ShipToState", "ShipToZipCode", "ShipToCountryCode", "ShipToCountryName", "BillToCustomerCode", "BillToName", "BillToAddress1", "BillToAddress2", "BillToAddress3", "BillToCity", "BillToState", "BillToZipCode", "BillToCountryCode", "BillToCountryName", "DeliveryAddressName", "DeliveryAddress1", "DeliveryAddress2", "DeliveryAddress3", "DeliveryCity", "DeliveryState", "DeliveryZipCode", "DeliveryCountryCode", "DeliveryCountryName", "DeliveryPhone", "BillFreightCustomerCode", "BillFreightAddressName", "BillFreightAddress1", "BillFreightAddress2", "BillFreightAddress3", "BillFreightCity", "BillFreightState", "BillFreightZipCode", "BillFreightCountryCode", "BillFreightCountryName", "CartonLabel", "ShipVerificationFlag", "PartialOrderFlag", "EarliestShipDate", "LatestShipDate", "EarliestDeliveryDate", "LatestDeliveryDate", "PromoCode", "Route", "Stop", "ToteFlag", "Status" });
我要做的就是遍历这段代码并将一堆字段添加到数据表中。我在这段代码中这样做:
foreach (string field in WaveAndPickFields)
{
dtWaveAndPicks.Columns.Add(field);
}
我也试过:
for (int i = 0; i < WaveAndPickFields.Count; i++)
{
DataColumn column = new DataColumn(WaveAndPickFields[i]);
dtWaveAndPicks.Columns.Add(column);
}
无论出于何种原因,这些行似乎都在执行,但整个函数都停止了,并且在没有异常错误的情况下没有其他任何执行。
dtWaveAndPicks.Columns.Add(field);
和:
dtWaveAndPicks.Columns.Add(column);
有谁知道为什么会这样?对我有用的替代方法是有一条将添加一列的行。问题是,当我可以有这样的循环时,我不想有 80 多行来添加字段。