c# - C#输入数组长于使用removeAt后的列数
问题描述
我很清楚,在尝试合并 ZRP 和 08-0729-01-MAR 时,从数据表中显式删除一列后会导致此问题,因此无论如何要删除该列而不会出现异常?请帮忙
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string filepath = @"C:\Users\2563911\source\repos\test.txt";
using (System.IO.TextReader tr = File.OpenText(filepath))
{
string line;
while ((line = tr.ReadLine()) != null)
{
if (!line.StartsWith("-"))
{
if (line.Length < 82 || line.Contains("MY01"))
{
string[] items = line.Replace("MY01", "").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
try
{
if (dt.Columns.Count == 0)
{
// Create the data columns for the data table based on the number of items
// on the first line of the file
for (int i = 0; i < items.Length; i++)
{
dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
}
}
dt.Rows.Add(items);
foreach (DataRow dr in dt.Rows)
{
dr[2] = dr[1].ToString() + dr[2].ToString();
}
dt.Columns.RemoveAt(1);
}
catch (System.ArgumentException c)
{
MessageBox.Show(c.Message);
break;
}
}
}
}
//show it in gridview
this.DataGridView.DataSource = dt;
}
}
}
示例文本文件:
MY01 CISCO ZPRP 08-0729-01-MAR 08-0729-01
MY01 CISCO ZPRP 08-0729-01-MAR 08-0729-01
MY01 CISCO ZPRP 08-0729-01-MAR 08-0729-01
MY01 CISCO ZPRP 10- 2919-01$1
解决方案
推荐阅读
- ruby-on-rails - 如何正确书写<%= t('.title_#{Time.now.wday}') %>?
- vim - 如何在 vim 中的文件名开头添加“#”?
- methods - 返回值的 Rust Lifetime 应该等于 struct 的生命周期
- c++ - 尝试构建 torchscript 扩展会导致 INVALID TYPE: Only int64_t and bool is supported as an integer argument type 错误
- vb.net - For i = StartIndex To 1 Step -1(索引超出了数组的边界(i-1)
- embedded - 为什么 SPI 序列与 MSB 顺序 EFR32 不匹配
- vim - 如何恢复被位置列表取代的文件的内容?
- php - 如何在 apache2 - ubuntu 上托管 .php 文件?
- go - 检查是否需要迁移
- javascript - 日期时间选择器:禁用当天晚于当前时间的时间并启用前几天的时间