C# 列出数据表的值
我从一个excel文档中创建了一个列表,其中每隔一行的零件号从第一行开始,价格从第二行开始 假设我像这样初始化一个数据表C# 列出数据表的值,c#,list,datatable,C#,List,Datatable,我从一个excel文档中创建了一个列表,其中每隔一行的零件号从第一行开始,价格从第二行开始 假设我像这样初始化一个数据表 DataTable priceListTable = new DataTable(); priceListTable.Columns.Add("ItemNumber", typeof(string)); priceListTable.Columns.Add("Price", typeof(Float)); 我的列表(称为
DataTable priceListTable = new DataTable();
priceListTable.Columns.Add("ItemNumber", typeof(string));
priceListTable.Columns.Add("Price", typeof(Float));
我的列表(称为记录列表)如下所示
001-001
1.45
001-002
3.49
如何获取列表的前两行以填充数据表的列?string ItemNumber=“ItemNumber”;
string ItemNumber = "ItemNumber";
string Price = "Price";
DataTable priceListTable = new DataTable();
DataRow row;
priceListTable.Columns.Add(ItemNumber);
priceListTable.Columns.Add(Price);
int counter = 0;
foreach(string s in recordList)
{
myTableSize++;
}
foreach(string s in recordList)
{
if (counter < myTableSize)
{
row = priceListTable.NewRow();
row[ItemNumber] = recordList[counter];
row[Price] = recordList[counter + 1];
priceListTable.Rows.Add(row);
counter++;
counter++;
}
字符串Price=“Price”;
DataTable priceListTable=新DataTable();
数据行;
priceListTable.Columns.Add(ItemNumber);
priceListTable.Columns.Add(价格);
int计数器=0;
foreach(记录列表中的字符串s)
{
myTableSize++;
}
foreach(记录列表中的字符串s)
{
if(计数器
这里有一个解决方案
从第二项开始,一次循环列出两项。这确保您始终有一对要使用的项
for (int i = 1; i < list.Count; i += 2)
{
DataRow row = table.NewRow();
row["ItemNumber"] = list[i-1];
row["Price"] = list[i];
table.Rows.Add(row);
}
for(int i=1;i
您是否有任何可以展示的实现方法?我甚至不知道从哪里开始。我正在考虑foreach循环,但需要两个值(列表中的两行)使得这不可能,对吗?