简体   繁体   English

如何在使用foreach循环时添加到数据集中的特定列c#

[英]How to add to a specific column in a dataset while using a foreach loop c#

I'm trying to add an incrementing time! 我正在尝试添加一个递增时间! column to my datagrid view along side the data that is being pulled from the streamreader 'r'; 我的数据网格视图的列,以及从流读取器'r'中提取的数据; I have the increment working but the time goes between the rows rather than next to them. 我有增量工作但时间在行之间而不是在它们旁边。 The data is being displayed as followed: 数据显示如下:

15:46:20 十五时46分20秒
91 154 70 309 83 6451 91 154 70 309 83 6451

15:46:21 十五时46分21秒
91 154 70 309 83 6451 91 154 70 309 83 6451

When I want to get it to be like this: 15:46:21 91 154 70 309 83 6451 当我想让它像这样:15:46:21 91 154 70 309 83 6451

                while ((r.Peek() != -1))
                {
                    string timeS = "";
                    string delimeter = "\t";
                    string[] Time1;
                    allData = r.ReadLine();
                    timeS = Dtime.ToString();
                    Time1 = timeS.Split(' ');
                    timeS = Time1[1];


                    string[] rows = allData.Split("\r".ToCharArray());

                    foreach (string row in rows)
                    {
                        Dtime = Dtime.AddSeconds(inter);
                        dataset.Tables[tableName].Rows.Add(timeS);
                        string[] items = row.Split(delimeter.ToCharArray());

                        dataset.Tables[tableName].Rows.Add(items);


                    }

                    this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
                }

Cheers! 干杯!

I think the issue is this: 我认为问题是这样的:

dataset.Tables[tableName].Rows.Add(timeS);

Followed by: 其次是:

dataset.Tables[tableName].Rows.Add(items);

So that's why there are two rows being added. 这就是为什么要添加两行的原因。

There's a nicer way to do this though. 有一个更好的方法来做到这一点。 It sounds as though you want to dynamically added another column to hold the time value. 听起来好像你想动态添加另一列来保存时间值。 Is this defined in your data table, or do you add it at run time? 这是在数据表中定义的,还是在运行时添加? Either way, it needs to be there in order to hold the value. 无论哪种方式,它都需要存在才能保持价值。

I modified your code above to get the time value into the last position in the item array. 我修改了上面的代码,将时间值放到item数组的最后一个位置。 It's not pretty at all, and could be simplified dramatically. 它根本不漂亮,可以大大简化。 Here's my code: 这是我的代码:

string timeS = "Time";
string row = "Value1,Value2,Value3";
string delimeter = ",";
string[] items = row.Split(delimeter.ToCharArray());
string[] newItems = new string[items.Length + 1];

items.CopyTo(newItems, 0);
newItems[newItems.Length - 1] = timeS;

So now you have an array where the last item is the time value. 所以现在你有一个数组,其中最后一项是时间值。 You can add this into your data table: 您可以将其添加到数据表中:

dataTable.Rows.Add(newItems);

I'm sure there's a nicer way to do this, perhaps this will help you get moving in the right direction. 我相信有更好的方法可以做到这一点,也许这会帮助你朝着正确的方向前进。

Instead of adding timeS as a new row by itself, you should add it as the first item in your items array, and then add the array as a row. 您应该将它添加为items数组中的第一项,然后将该数组添加为行,而不是将timeS作为新行添加。

For example: 例如:

var items = new List<string>{timeS};
items.AddRange(row.Split(delimeter.ToCharArray()));                
dataset.Tables[tableName].Rows.Add(items.ToArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM