简体   繁体   English

DataGridView返回太多列

[英]DataGridView returns too many Columns

I have a WIndows Forms Application. 我有一个Windows窗体应用程序。 I needed a DataGridView so i added one. 我需要一个DataGridView,所以我添加了一个。 I asume that the user adds some rows and press a save button. 我假设用户添加了一些行,然后按了保存按钮。

At this moment i want to save all rows in the grid. 此时,我想将所有行保存在网格中。 I iterate trough all rows like that 我像这样遍历所有行

foreach (DataGridViewRow item in dataGridView.Rows)
            {
            }

Now the problem. 现在问题。 I have tested it with one row in the grid. 我已经在网格中测试了一行。 The first row (which i typed in) comes correctly. 第一行(我输入)正确显示。 then comes a nother row wich i didnt accpected. 然后是我没想到的另一排。 All values in that row have the value 1 for int and null for string. 该行中的所有值的int值为1,字符串的值为null。 and then comes a row this just null for every column. 然后出现一行,每一列都为null

EDIT: May i explain what i wanted to do. 编辑:我可以解释一下我想做什么。 I have objects with 4 propreties. 我有4个属性的对象。 I have list of this objects. 我有这个对象的清单。 And i want that the user edits and manuplates that list and the obejcts inside it, 我希望用户编辑和处理该列表及其中的对象,

EDIT2: My problem is not to get the Data. EDIT2:我的问题是不获取数据。 My problem is that is get Data which not exists. 我的问题是获取不存在的数据。 I have tried it with 4 rows now. 我已经尝试了4行。 So i filed in 4 rows in the grid. 因此,我在网格中提交了4行。

my input was 我的输入是

1 2 a 1  
12 3 b 3
12 4 c 4 
13 5 d 5

output is 输出是

1 2 a 1  
1 1 null 1 (why the heck is that one here :( )
12 3 b 3
12 4 c 4 
13 5 d 5 
null null null null (this row i could live with cause i know where it comes from)

这是一个新行,检查if (item.IsNewRow) continue;

make for your DataTable dt, dt.AcceptChanges(); 为您的DataTable设置dt,dt.AcceptChanges(); and start loop in foreach(DataRow dr in dt.Rows) 并在foreach中启动循环(dt.Rows中的DataRow dr)

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

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