When I intend to save a DataGridView how would I use the DataTable

T

Tony Johansson

Hello!

I'm new to use this DataGridView.
I use a textfile to store my rows from the DataGridView

I have a DataGridView representing the DataTable.
When I push the Save Button I call the event handler below.
The DataGridView control is called dataGridViewWareHouse.

In my first attempt I didn't us a DataTable so the code I used to save my
DataGridView rows into
the textfile was to loop through the rows for the DataGridView and have a
code something like
//Start old code
string myAddedRow =
dataGridViewWareHouse.Rows[row].AccessibilityObject.Value;
string[] myRow = myAddedRow.Split(new char[] { ';' });

Product myProduct = new Product(myRow[(int)columnType.Name],
int.Parse(myRow[(int)columnType.Price]),
int.Parse(myRow[(int)columnType.ProductNummer]),
int.Parse(myRow[(int)columnType.Count]),
myRow[(int)columnType.Type]);
FileManager.Save(myProduct);
//end old code


I just wonder when I now use a DataTable which I call _dataTable how
would I write now to get the whole row from the DataTable or should
I still read the dataGridViewWareHouse ?

private void BtnSave_Click(object sender, EventArgs e)
{
foreach (DataRow dr in _dataTable.Rows)
{
if (dr.RowState == DataRowState.Added)
{

}
else if (dr.RowState = DataRowState.Unchanged)
continue;
}
}

//Tony
 
C

Cor Ligthert[MVP]

Basically it is easy

foreach (DataRow) row in dt
{
for each (DataColumn) col in dt
{
copy to field to textrow using row[col.ColumnName]
}
write the textrow
}

Cor
 
T

Tony Johansson

Hello!

When I use the construction
foreach (DataColumn col in dt)
I get the following compile error

Error 1 foreach statement cannot operate on variables of type
'System.Data.DataTable' because 'System.Data.DataTable' does not contain a
public definition for 'GetEnumerator' F:\C#\Ovningar\MediaShop\Lager.cs 103
16 MediaShop

Have I missed something here

//Tony

Cor Ligthert said:
Basically it is easy

foreach (DataRow) row in dt
{
for each (DataColumn) col in dt
{
copy to field to textrow using row[col.ColumnName]
}
write the textrow
}

Cor
Tony Johansson said:
Hello!

I'm new to use this DataGridView.
I use a textfile to store my rows from the DataGridView

I have a DataGridView representing the DataTable.
When I push the Save Button I call the event handler below.
The DataGridView control is called dataGridViewWareHouse.

In my first attempt I didn't us a DataTable so the code I used to save my
DataGridView rows into
the textfile was to loop through the rows for the DataGridView and have a
code something like
//Start old code
string myAddedRow =
dataGridViewWareHouse.Rows[row].AccessibilityObject.Value;
string[] myRow = myAddedRow.Split(new char[] { ';' });

Product myProduct = new Product(myRow[(int)columnType.Name],

int.Parse(myRow[(int)columnType.Price]),

int.Parse(myRow[(int)columnType.ProductNummer]),

int.Parse(myRow[(int)columnType.Count]),

myRow[(int)columnType.Type]);
FileManager.Save(myProduct);
//end old code


I just wonder when I now use a DataTable which I call _dataTable how
would I write now to get the whole row from the DataTable or should
I still read the dataGridViewWareHouse ?

private void BtnSave_Click(object sender, EventArgs e)
{
foreach (DataRow dr in _dataTable.Rows)
{
if (dr.RowState == DataRowState.Added)
{

}
else if (dr.RowState = DataRowState.Unchanged)
continue;
}
}

//Tony
 
P

Pavel Minaev

Hello!

When I use the construction
 foreach (DataColumn col in dt)
I get the following compile error

Error 1 foreach statement cannot operate on variables of type
'System.Data.DataTable' because 'System.Data.DataTable' does not contain a
public definition for 'GetEnumerator' F:\C#\Ovningar\MediaShop\Lager.cs 103
16 MediaShop

Have I missed something here

Yes; or rather, Cor posted his reply in pseudocode without explaining
as much (in a C# group - tsk, tsk!). The correct syntax here is:

foreach (DataColumn col in dt.Columns)

and

foreach (DataRow row in dt.Rows)

Better yet, you should forget this whole thing, and just use data
binding. Drop a BindingSource onto your form, set its DataSource
property to your DataSet (which you may also drag onto the form as a
component, in which case you can link them up in the designer), and
set DataGridView.DataSource to the BindingSource.
 
C

Cor Ligthert[MVP]

Pavel,

I knew this when I was away.

It has to be

foreach (DataColumn dc in dt.Rows)
{
foreach (DataRow dr in dt.Rows)
{
'copy dr(dc); 'Because that is faster then the name, the name I use as I
copy a datatable
}
write streamreaderrow;
}
Was what it should have to be although again this direct typed in message

:)


I only can say sorry,

The advise to use a bindingsource I gave already in the previous message.
But Tony only asks, never gives replies.

Cor
 
C

Cor Ligthert[MVP]

Duh

\\\
foreach (DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
'item(x) = dr(dc);
}
}
///
Seems to be a long journey

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top