Connecting Tech Pros Worldwide Forums | Help | Site Map

Adding rows to table not connected to database

Arek
Guest
 
Posts: n/a
#1: Nov 17 '05
I need to have a simple table. User and application will add rows to it.
I've used DataGrid but I can't add rows by application. I've got such code:
DataRow row = dataTable.NewRow();
row.BeginEdit();
row.ItemArray.SetValue("0", 0);
row.ItemArray.SetValue("0", 1);
row.ItemArray.SetValue("0", 2);
row.ItemArray.SetValue("0", 3);
row.EndEdit();
dataRow.ImportRow(row);
dataGrid.DataSource = dataTable;
But this have no effect - dataGrid is empty. Is in C# another component
to display tables? How should it be to work properly?
Thanks for help.

Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Adding rows to table not connected to database


Arek,

Well, the row is added, but the values themselves are empty. ItemArray
returns a copy of the array of items which represent the values in the
columns. Because of this, when you call SetValue, you are changing the
value of the array on four different arrays.

What you need to do is use the indexer for the row, and set the value
with the column name, like this:

DataRow row = dataTable.NewRow();
row.BeginEdit();
row[0] = 0;
row[1] = 0;
row[2] = 0;
row[3] = 0;
row.EndEdit();
dataRow.ImportRow(row);
dataGrid.DataSource = dataTable;

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Arek" <guardusers@poczta.onet.pl> wrote in message
news:dj0nqa$of$1@news.onet.pl...[color=blue]
>I need to have a simple table. User and application will add rows to it.
>I've used DataGrid but I can't add rows by application. I've got such code:
> DataRow row = dataTable.NewRow();
> row.BeginEdit();
> row.ItemArray.SetValue("0", 0);
> row.ItemArray.SetValue("0", 1);
> row.ItemArray.SetValue("0", 2);
> row.ItemArray.SetValue("0", 3);
> row.EndEdit();
> dataRow.ImportRow(row);
> dataGrid.DataSource = dataTable;
> But this have no effect - dataGrid is empty. Is in C# another component to
> display tables? How should it be to work properly?
> Thanks for help.[/color]


Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Adding rows to table not connected to database




Use this code :
DataRow row = dataTable.NewRow();
row[0] = 0;
row[1] = 1;
row[2] = 2;
row[3] = 3;
dataTable.Rows.Add( row);
dataGrid.DataSource = dataTable;
dataGrid.Bind();

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Arek" <guardusers@poczta.onet.pl> wrote in message
news:dj0nqa$of$1@news.onet.pl...[color=blue]
>I need to have a simple table. User and application will add rows to it.
>I've used DataGrid but I can't add rows by application. I've got such code:
> DataRow row = dataTable.NewRow();
> row.BeginEdit();
> row.ItemArray.SetValue("0", 0);
> row.ItemArray.SetValue("0", 1);
> row.ItemArray.SetValue("0", 2);
> row.ItemArray.SetValue("0", 3);
> row.EndEdit();
> dataRow.ImportRow(row);
> dataGrid.DataSource = dataTable;
> But this have no effect - dataGrid is empty. Is in C# another component to
> display tables? How should it be to work properly?
> Thanks for help.[/color]


Arek
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Adding rows to table not connected to database


Nicholas Paldino [.NET/C# MVP] napisał(a):[color=blue]
> Arek,
>
> Well, the row is added, but the values themselves are empty. ItemArray
> returns a copy of the array of items which represent the values in the
> columns. Because of this, when you call SetValue, you are changing the
> value of the array on four different arrays.
>
> What you need to do is use the indexer for the row, and set the value
> with the column name, like this:
>
> DataRow row = dataTable.NewRow();
> row.BeginEdit();
> row[0] = 0;
> row[1] = 0;
> row[2] = 0;
> row[3] = 0;
> row.EndEdit();
> dataRow.ImportRow(row);
> dataGrid.DataSource = dataTable;
>
> Hope this helps.
>
>[/color]
Oh ... I blame myself :) That was so easy ... Thank you!
Closed Thread


Similar C# / C Sharp bytes