tags:

views:

1228

answers:

4

Hello

I'v got some problem, I use DataTable to store my data in dataGridView. Data was inputed like this:

        dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("par", typeof(string));
        dt.Columns.Add("max", typeof(int));
        dt.Columns.Add("now", typeof(int));


        dt.Rows.Add(new object[] { id++,i + " " + j++, strarr[0],

strarr[1] }); // ... etc etc

        dataGridView1.DataSource = dt;

now I want to do some changes, I use code:

dt.Rows[1].ItemArray[3] = "VALUE";

When I debug, ItemArray represents the row I want to, so its okey, but still I can't do any changes, whats wrong ? How to update DataTable ??? I'm googling and nothing :(

A: 

dt.Rows[1].ItemArray gives you a copy of item arrays. When you modify it, you're not modifying the original.

You can simply do this:

dt.Rows[1][3] = "Value";

ItemArray property is used when you want to modify all row values.

ex.:

dt.Rows[1].ItemArray = newItemArray;
najmeddine
A: 

Try the SetField method:

table.Rows[i].SetField(column, value);
table.Rows[i].SetField(columnIndex, value);
table.Rows[i].SetField(columnName, value);

This should get the job done and is a bit "cleaner" than using Rows[i][j].

Yannick Compernol
A: 

OMG it was so simple, thx a lot, :D

fazzy
fazzy: "OMG it was so simple, thx a lot, :D" is not an answer. It's a comment. Every answer has a "add comment" button which you can use to add such comments. Also, don't forget to upvote the answer which solved your problem. To upvote, click the up arrow on the left hand side of answer (The up arrow on the "0" with up and down arrows).
Yogesh
A: 

I dont see any add comment buttons, Im not registered, I dont have OpenID, so I cant wote :/ Is there a solution for that ?

fazzy