如何同时将字符串转换并插入到List<int>中?

How to convert & insert string into List<int> at the same time?

我想从 telerik 网格视图中获取一些数据并将其值(一些单元格)转换为 List<int>(有些不是)

我只想将它插入列表

foreach (int item in _MyAmount)
{
     _MyAmount.Select(int.Parse).ToList();
     radGridView1.CurrentRow.Cells[item].Value.ToString();
}

我该怎么办?

我认为您的 foreach 循环有点问题。根据您的描述,您想要 运行 遍历 telerik gridview(大概是 radGridView1?)中的每一行,将其转换为 int,然后将其保存在 _MyAmount 中。

如果我的假设是正确的,那么你应该使用这样的东西:

foreach (var Row in radGridView1.Rows)
{
    foreach (var Cell in Row.Cells)
    {
        _MyAmount.Add((int) Cell.Value);
    }
}

这假设每行中有超过 1 个单元格。如果没有,那么您可以缩短为:

foreach (var Row in radGridView1.Rows)
{
    _MyAmount.Add((int) Row.Cells[0].Value);
}

更新

对于 RadGrid 试试这个:

foreach (var Row in radGridView1.Items)
{
    _MyAmount.Add((int) Row["UniqueName"].Text);
}

更新 2

似乎有点奇怪,它接受 'Rows' 作为 radGridView1 上的集合,但随后不接受单元格的“.Value”。我现在有点猜测,但如果你尝试像这样混合 2 会怎样:

foreach (var Row in radGridView1.Rows)
{
    _MyAmount.Add((int) Row["UniqueName"].Text);
}