views:

32

answers:

2

I have a WIndows Forms Application. I needed a DataGridView so i added one. 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. and then comes a row this just null for every column.

EDIT: May i explain what i wanted to do. I have objects with 4 propreties. 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. My problem is that is get Data which not exists. I have tried it with 4 rows now. So i filed in 4 rows in the grid.

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)
+1  A: 

It is a new row, check for if (item.IsNewRow) continue;

Orsol
okay now the row with all values null are gone! thanks for that. But i still get the row with alle values 1 edit: okay this leads to the wrong direction :( i need both. The new and old rows.
Markus
+1  A: 

make for your DataTable dt, dt.AcceptChanges(); and start loop in foreach(DataRow dr in dt.Rows)

Serkan Hekimoglu
i dont have this table in state i only create it on the fly for creating the grid.May i explain what i wanted to do. I have objects with 4 propreties. I have list of this objects. And i want that the user edits and manuplates that list.
Markus
so best way is, create your datatable manually. set your column names, types. make a row object, put it in loop, set your row fields, and bind it to gridView. Everychanges made on gridView can be seen on your dataTable rows. If you loop in your dt.Rows you can get changes.
Serkan Hekimoglu
my god.... iwas so stupid i my read method was like that var1= row["var1] var2= row["var1] var3= row["var1] var4= row["var1]
Markus