rowState doesn't work as expected

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
 
P

Pavel Minaev

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 priceis
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 dataGridViewWareHouseso
it's works good so far.

4.But now assume that I enter a new row into the dataGridViewWareHouse sowe
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 ?

You misunderstand the intention behind DataRow.RowState. It's not an
indicator of user actions - rather, it's supposed to be used by
DbDataAdapter to do a diff between the original state of the DataSet
as retrieved from the database, and its modified state. Thus, it
doesn't matter if the row was added by you programmatically, or by
user via a DataGridView (which, in the end, also adds it
programmatically, of course) - the row is marked as "added" in both
cases, so that DataAdapter knows to perform SQL INSERT when asked to
flush changes. For a row with state "modified", it would have to
perform UPDATE instead.

You _can_ use it for your own purposes if you want, such as tracking
user actions. To do so, after you programmatically add or change the
row, call AcceptChanges method on it - this will reset Added or
Modified RowState to Unchanged, and then any user edit will set the
row to Modified only.
 
C

Cor Ligthert[MVP]

Bind that datatable to your DataGridView

BindingSource bs = new BindingSource();
bs.DataSource=dt;
dgv.DataSource=bs;
(That bindingSource in between is to solve some problems there where)


And before you want to see if something is changed use bs.EndEdit();
That is because you don't know if somebody has used the enter row box in the
DataGridView

Cor

Tony Johansson said:
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
 

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

Similar Threads


Top