T
Tony Johansson
Hello!
Some information so you can understand my question better.
This is an easy program to understand.
I have a CSV textfile for storing different kind of products.
A product can be BOOK,DV,DVD and GAME and so on.
The textfile has this format
Name,Price,ProductNumber,NumberInStock,ProductType
where name could be the title for a book or name for a game and the price is
how much is cost and so on.
I use several classes but relevant here is probably only WareHouse.
FileManager class is a static class.
All variables that is instance fields is prefixed with a _
Just to examplify my problem lets say we have just one row in the textfile
with this contents.
The man who came in from the cold,135,1,13,Book.
Title for the bok is The man who came in from the cold
The book cost 135
The ProductNumber is 1
There is 13 identical books in stock
The ProductType is book.
Here is the sequence for my application.
******************************
1. The C-tor for WareHouse is called se below.
public WareHouse( )
{
InitializeComponent();
// Get next ProductNumber to be used when entering new rows into the
DataGridView
_currentProductNumber = FileManager.GetNextProductNumber(); }
}
2. The form load event handler is called.
I have listed all methods that is used below.
I create all the five DataColumn and add to the DataTable(_dataTable)
I get all the record from the textfile. In this case only one.
I create a DataRow and add to the DataTable(_dataTable)
I set the _dataTable to the DataSource for the dataGridViewWareHouse.
private void WareHouse_Load(object sender, EventArgs e)
{
string[] rowData;
if (dataGridViewWareHouse.RowCount > 0)
_dataTable.Rows.Clear();
if (_dataTable.Columns.Count == 0)
{
_dataTable.Columns.Add(GetNewColumn("Name", "Name", "System.String"));
_dataTable.Columns.Add(GetNewColumn("Price", "Price",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("ProdNr", "ProdNr",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("CountInStock", "CountInStock",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("ProductType", "ProductType",
"System.String"));
}
//Here in ReadFile I get all the records from the textfile
//In this example I just get one record which is
//The man who came in from the cold,135,1,13,Book
_myGenericList = FileManager.ReadFile();
foreach (string line in _myGenericList )
{
rowData = line.Split(new char[] { ',' });
CreateNewRow(rowData);
}
dataGridViewWareHouse.DataSource = _dataTable;
}
private DataColumn GetNewColumn(string columnName, string columnCaption,
string columnType)
{
DataColumn dc = new DataColumn(columnName,
System.Type.GetType(columnType));
dc.Caption = columnCaption;
return dc;
}
private void CreateNewRow(string[] rowData)
{
DataRow dataRow = _dataTable.NewRow();
int i = 0;
foreach (string fieldData in rowData)
dataRow[i++] = fieldData;
_dataTable.Rows.Add(dataRow);
}
3. The contents in the textfile is displayed in the dataGridViewWareHouse so
it's works good so far.
4.But now assume that I enter a new row into the dataGridViewWareHouse so we
have two rows.
We also change the row that was fetched from the textfile.
5. I have a button called Save in the form.and this looks like this. I push
this Save button.
private void BtnSave_Click(object sender, EventArgs e)
{
foreach (DataRow dr in _dataTable.Rows)
{
DataRowState state = dr.RowState;
}
}
Here I have come to my real problem. If I check the RowState for my two rows
both have state added.
I would have thought added for the one that I entered and modified for the
one that I changed.
Can somebody explain why my rowState doesn't work ?
Have I missed something here ?
//Tony
Some information so you can understand my question better.
This is an easy program to understand.
I have a CSV textfile for storing different kind of products.
A product can be BOOK,DV,DVD and GAME and so on.
The textfile has this format
Name,Price,ProductNumber,NumberInStock,ProductType
where name could be the title for a book or name for a game and the price is
how much is cost and so on.
I use several classes but relevant here is probably only WareHouse.
FileManager class is a static class.
All variables that is instance fields is prefixed with a _
Just to examplify my problem lets say we have just one row in the textfile
with this contents.
The man who came in from the cold,135,1,13,Book.
Title for the bok is The man who came in from the cold
The book cost 135
The ProductNumber is 1
There is 13 identical books in stock
The ProductType is book.
Here is the sequence for my application.
******************************
1. The C-tor for WareHouse is called se below.
public WareHouse( )
{
InitializeComponent();
// Get next ProductNumber to be used when entering new rows into the
DataGridView
_currentProductNumber = FileManager.GetNextProductNumber(); }
}
2. The form load event handler is called.
I have listed all methods that is used below.
I create all the five DataColumn and add to the DataTable(_dataTable)
I get all the record from the textfile. In this case only one.
I create a DataRow and add to the DataTable(_dataTable)
I set the _dataTable to the DataSource for the dataGridViewWareHouse.
private void WareHouse_Load(object sender, EventArgs e)
{
string[] rowData;
if (dataGridViewWareHouse.RowCount > 0)
_dataTable.Rows.Clear();
if (_dataTable.Columns.Count == 0)
{
_dataTable.Columns.Add(GetNewColumn("Name", "Name", "System.String"));
_dataTable.Columns.Add(GetNewColumn("Price", "Price",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("ProdNr", "ProdNr",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("CountInStock", "CountInStock",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("ProductType", "ProductType",
"System.String"));
}
//Here in ReadFile I get all the records from the textfile
//In this example I just get one record which is
//The man who came in from the cold,135,1,13,Book
_myGenericList = FileManager.ReadFile();
foreach (string line in _myGenericList )
{
rowData = line.Split(new char[] { ',' });
CreateNewRow(rowData);
}
dataGridViewWareHouse.DataSource = _dataTable;
}
private DataColumn GetNewColumn(string columnName, string columnCaption,
string columnType)
{
DataColumn dc = new DataColumn(columnName,
System.Type.GetType(columnType));
dc.Caption = columnCaption;
return dc;
}
private void CreateNewRow(string[] rowData)
{
DataRow dataRow = _dataTable.NewRow();
int i = 0;
foreach (string fieldData in rowData)
dataRow[i++] = fieldData;
_dataTable.Rows.Add(dataRow);
}
3. The contents in the textfile is displayed in the dataGridViewWareHouse so
it's works good so far.
4.But now assume that I enter a new row into the dataGridViewWareHouse so we
have two rows.
We also change the row that was fetched from the textfile.
5. I have a button called Save in the form.and this looks like this. I push
this Save button.
private void BtnSave_Click(object sender, EventArgs e)
{
foreach (DataRow dr in _dataTable.Rows)
{
DataRowState state = dr.RowState;
}
}
Here I have come to my real problem. If I check the RowState for my two rows
both have state added.
I would have thought added for the one that I entered and modified for the
one that I changed.
Can somebody explain why my rowState doesn't work ?
Have I missed something here ?
//Tony