How do i best search a DataTable if some rows has been deleted.

T

Tony Johansson

Hello!

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();

_bindingSource.DataSource = _dataTable;
dgvStock.DataSource = _bindingSource;

When I want to delete a row from the DataGridView I select the row and press
the delete button.
I have the event handler below
Private void btnDelete_Click(object sender, EventArgs e)
{
_dataTable.DefaultView.Delete(dgvStock.CurrentCell.RowIndex);
}

This work good.

I have also a text field in my form which is named txtProdNr where a can
input a productnumber to search for in the DataGridView.
This work good unless I haven't remove any row from the DataGridView.
Here is the event handler for my search button.
private void BtnSearch_Click(object sender, EventArgs e)
{
for (int i = 0; i < _dataTable.Rows.Count; i++)
{
if ((txtProdNr.Text.ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
dgvStock.Rows.Selected = true;
}
}

I would like to rewrite this event handler for the search button so it works
every time.
So I should only be able to find those productnumber that is visible(not
deleted) in the DataGridView.

Can anybody tell me how this should be written.

//Tony
 
T

Tony Johansson

Hello!

The data that is displayed in the DataGridView is coming from a plain
textfile.
I must also add that I have a name textbox in the form so when I hit the
search button either
the name textbox or the product number textbox is filled in.

So when I hit the search button a search is done through the rows that has
not been deleted
from the dataGridView. So either I search for the given textbox name or the
given
productnumbertextbox in the DataGridView.

There is more info below.

Can anybody tell me how this should be written.

//Tony
 
T

Tony Johansson

Hello!

The data that is displayed in the DataGridView is coming from a plain
textfile.
I must also add that I have a name textbox in the form so when I hit the
search button either
the name textbox or the product number textbox is filled in.

So when I hit the search button a search is done through the rows that has
not been deleted
from the dataGridView. So either I search for the given textbox name or the
given
productnumbertextbox in the DataGridView.

There is more info below.

Can anybody tell me how this should be written.

//Tony
 
M

Mr. Arnold

Tony Johansson said:
Hello!

The data that is displayed in the DataGridView is coming from a plain
textfile.
I must also add that I have a name textbox in the form so when I hit the
search button either
the name textbox or the product number textbox is filled in.

So when I hit the search button a search is done through the rows that has
not been deleted
from the dataGridView. So either I search for the given textbox name or
the given
productnumbertextbox in the DataGridView.

There is more info below.

Can anybody tell me how this should be written.

//Tony

Well, you're going to have to get this data into grid, or a collection,
with an extra field, a hidden field on the grid. You can set the field
called "Delete" to "N" - not deleted and "Y" - is deleted, a row marked for
deletion implementation.

If you select a row in the grid for deletion, then you mark the row for
deletion by setting the "Delete" field to "Y".

You select on rows to show where the "Delete" = "N".

Sometimes, it's just best to keep things simple and not over complicate it.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4063 (20090508) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

Mr. Arnold

Tony Johansson said:
Hello!

The data that is displayed in the DataGridView is coming from a plain
textfile.
I must also add that I have a name textbox in the form so when I hit the
search button either
the name textbox or the product number textbox is filled in.

So when I hit the search button a search is done through the rows that has
not been deleted
from the dataGridView. So either I search for the given textbox name or
the given
productnumbertextbox in the DataGridView.

There is more info below.

Can anybody tell me how this should be written.

//Tony

Well, you're going to have to get this data into grid, or a collection,
with an extra field, a hidden field on the grid. You can set the field
called "Delete" to "N" - not deleted and "Y" - is deleted, a row marked for
deletion implementation.

If you select a row in the grid for deletion, then you mark the row for
deletion by setting the "Delete" field to "Y".

You select on rows to show where the "Delete" = "N".

Sometimes, it's just best to keep things simple and not over complicate it.





__________ Information from ESET NOD32 Antivirus, version of virus signature database 4063 (20090508) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
T

Tony Johansson

Hello!

I will describe my problem fully so that you understand it completely.

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
_bindingSource.DataSource = _dataTable;
dgvStock.DataSource = _bindingSource;

The data that is displayed in the DataGridView is coming from a plain
textfile. Two of the columns in the DataGridView is called Name and ProdNr

When I want to delete a row from the DataGridView I select the row and press
the delete button this work perfectly so this is no problem..
I have the event handler for the delete button below
Private void btnDelete_Click(object sender, EventArgs e)
{
_dataTable.DefaultView.Delete(dgvStock.CurrentCell.RowIndex);
}


I have also a textBox called txtProdNr where a can input a productnumber to
search for in the DataGridView.
I have also a textbox called txtName where I can input a name to search for
in the DataGridView.
When I want to search I enter into the txtName OR into the txtProdNr but not
both at the same time.
The event handler that is used for the search button is called
BtnSearch_Click and is shown below.

This event handler BtnSearch_Click works perfectly as long as I haven't
deleted any rows from DataGridView.
When I have deleted a row from the DataGridView I get the runtime error
"Deleted row information cannot be accessed through the row."

I have looked at some example to use but those examples is using primary key
I think I can't set that both the name and productnumber to be used as
primary key because I don't use both at the same time
As you can see in the event handler below I just loop through the datatable
and search for a match

private void BtnSearch_Click(object sender, EventArgs e)
{
if (txtName.Text != "" ^ txtProdNr.Text != "")
{
for (int i = 0; i < _dataTable.Rows.Count; i++)
{
if ((txtName.Text != "" && txtName.Text.ToUpper() ==
_dataTable.Rows["Name"].ToString()) ||
(txtProdNr.Text != "" && txtProdNr.Text.ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
dgStock.Rows.Selected = true;
}
}
}


So my question is :
I would like to rewrite this event handler for the search button so it works
every time.
So when I give a name OR productnumber in the textbox the row that match
should be selected.

Can anybody tell me how this should be written.

//Tony
 
T

Tony Johansson

Hello!

I will describe my problem fully so that you understand it completely.

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
_bindingSource.DataSource = _dataTable;
dgvStock.DataSource = _bindingSource;

The data that is displayed in the DataGridView is coming from a plain
textfile. Two of the columns in the DataGridView is called Name and ProdNr

When I want to delete a row from the DataGridView I select the row and press
the delete button this work perfectly so this is no problem..
I have the event handler for the delete button below
Private void btnDelete_Click(object sender, EventArgs e)
{
_dataTable.DefaultView.Delete(dgvStock.CurrentCell.RowIndex);
}


I have also a textBox called txtProdNr where a can input a productnumber to
search for in the DataGridView.
I have also a textbox called txtName where I can input a name to search for
in the DataGridView.
When I want to search I enter into the txtName OR into the txtProdNr but not
both at the same time.
The event handler that is used for the search button is called
BtnSearch_Click and is shown below.

This event handler BtnSearch_Click works perfectly as long as I haven't
deleted any rows from DataGridView.
When I have deleted a row from the DataGridView I get the runtime error
"Deleted row information cannot be accessed through the row."

I have looked at some example to use but those examples is using primary key
I think I can't set that both the name and productnumber to be used as
primary key because I don't use both at the same time
As you can see in the event handler below I just loop through the datatable
and search for a match

private void BtnSearch_Click(object sender, EventArgs e)
{
if (txtName.Text != "" ^ txtProdNr.Text != "")
{
for (int i = 0; i < _dataTable.Rows.Count; i++)
{
if ((txtName.Text != "" && txtName.Text.ToUpper() ==
_dataTable.Rows["Name"].ToString()) ||
(txtProdNr.Text != "" && txtProdNr.Text.ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
dgStock.Rows.Selected = true;
}
}
}


So my question is :
I would like to rewrite this event handler for the search button so it works
every time.
So when I give a name OR productnumber in the textbox the row that match
should be selected.

Can anybody tell me how this should be written.

//Tony
 
P

Pavel Minaev

I will describe my problem fully so that you understand it completely.

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
_bindingSource.DataSource = _dataTable;
 dgvStock.DataSource = _bindingSource;

The data that is displayed in the DataGridView is coming from a plain
textfile. Two of the columns in the DataGridView is called Name and ProdNr

When I want to delete a row from the DataGridView I select the row and press
the delete button this work perfectly so this is no problem..
I have the event handler for the delete button below
Private void btnDelete_Click(object sender, EventArgs e)
{
     _dataTable.DefaultView.Delete(dgvStock.CurrentCell.RowIndex);

}

I have also a textBox called txtProdNr where a can input a productnumber to
search for in the DataGridView.
I have also a textbox called txtName where I can input a name to search for
in the DataGridView.
When I want to search I enter into the txtName OR into the txtProdNr but not
both at the same time.
The event handler that is used for the search button is called
BtnSearch_Click and is shown below.

This event handler BtnSearch_Click works perfectly as long as I haven't
deleted any rows from DataGridView.
When I have deleted a row from the DataGridView I get the runtime error
"Deleted row information cannot be accessed through the row."

I have looked at some example to use but those examples is using primary key
I think I can't set that both the name and productnumber to be used as
primary key because I don't use both at the same time
As you can see in the event handler below I just loop through the datatable
and search for a match

private void BtnSearch_Click(object sender, EventArgs e)
{
    if (txtName.Text != "" ^ txtProdNr.Text != "")
    {
        for (int i = 0; i < _dataTable.Rows.Count; i++)
        {
            if ((txtName.Text != "" && txtName.Text.ToUpper() ==
_dataTable.Rows["Name"].ToString()) ||
                (txtProdNr.Text != "" && txtProdNr.Text..ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
               dgStock.Rows.Selected = true;
        }
     }

}

So my question is :
I would like to rewrite this event handler for the search button so it works
every time.
So when I give a name OR productnumber in the textbox the row that match
should be selected.


You should do what the error message tells you, and check for deleted
rows:

foreach (DataRow row in _dataTable.Rows)
  {
if (row.RowState == DataRowState.Deleted) continue;
...
}

And a few side nits: use "foreach" instead of "for" when possible (as
I did above), and don't use ToUpper() for case-insensitive comparison
- use String.Equals with StringComparison parameter. Also note that if
you're doing the search based on user input, then you probably want
the comparison to be locale-dependent and not ordinal (so you
correctly compare all possible representations of letters with
dicritics and other such things) - for this, again, use String.Equals
and String.Compare as needed - for your case it's probably
StringComparison.CurrentCultureIgnoreCase.
 
P

Pavel Minaev

I will describe my problem fully so that you understand it completely.

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
_bindingSource.DataSource = _dataTable;
 dgvStock.DataSource = _bindingSource;

The data that is displayed in the DataGridView is coming from a plain
textfile. Two of the columns in the DataGridView is called Name and ProdNr

When I want to delete a row from the DataGridView I select the row and press
the delete button this work perfectly so this is no problem..
I have the event handler for the delete button below
Private void btnDelete_Click(object sender, EventArgs e)
{
     _dataTable.DefaultView.Delete(dgvStock.CurrentCell.RowIndex);

}

I have also a textBox called txtProdNr where a can input a productnumber to
search for in the DataGridView.
I have also a textbox called txtName where I can input a name to search for
in the DataGridView.
When I want to search I enter into the txtName OR into the txtProdNr but not
both at the same time.
The event handler that is used for the search button is called
BtnSearch_Click and is shown below.

This event handler BtnSearch_Click works perfectly as long as I haven't
deleted any rows from DataGridView.
When I have deleted a row from the DataGridView I get the runtime error
"Deleted row information cannot be accessed through the row."

I have looked at some example to use but those examples is using primary key
I think I can't set that both the name and productnumber to be used as
primary key because I don't use both at the same time
As you can see in the event handler below I just loop through the datatable
and search for a match

private void BtnSearch_Click(object sender, EventArgs e)
{
    if (txtName.Text != "" ^ txtProdNr.Text != "")
    {
        for (int i = 0; i < _dataTable.Rows.Count; i++)
        {
            if ((txtName.Text != "" && txtName.Text.ToUpper() ==
_dataTable.Rows["Name"].ToString()) ||
                (txtProdNr.Text != "" && txtProdNr.Text..ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
               dgStock.Rows.Selected = true;
        }
     }

}

So my question is :
I would like to rewrite this event handler for the search button so it works
every time.
So when I give a name OR productnumber in the textbox the row that match
should be selected.


You should do what the error message tells you, and check for deleted
rows:

foreach (DataRow row in _dataTable.Rows)
  {
if (row.RowState == DataRowState.Deleted) continue;
...
}

And a few side nits: use "foreach" instead of "for" when possible (as
I did above), and don't use ToUpper() for case-insensitive comparison
- use String.Equals with StringComparison parameter. Also note that if
you're doing the search based on user input, then you probably want
the comparison to be locale-dependent and not ordinal (so you
correctly compare all possible representations of letters with
dicritics and other such things) - for this, again, use String.Equals
and String.Compare as needed - for your case it's probably
StringComparison.CurrentCultureIgnoreCase.
 
M

Mr. Arnold

Tony Johansson said:
Hello!

I will describe my problem fully so that you understand it completely.

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
_bindingSource.DataSource = _dataTable;
dgvStock.DataSource = _bindingSource;

The data that is displayed in the DataGridView is coming from a plain
textfile. Two of the columns in the DataGridView is called Name and ProdNr

I'll keep it simple. You take manual control of the DataTable building the
row columns manually, populating data in the columns manually and adding a
row to the Datatable manually, which is being populated from the textfile
with manual reading of the textfile.

You create a new column on the DataTable row called 'Delete' and you set the
'Delete' column to "N", each time you add a new row to the Datatable from a
read of the textfile to populate the row.

You bind your manually created Datatable to the grid, but you hide the
'Delete' column which is pointing to a row in the Datatable.

You select a row on the grid for deletion, then you set 'Delete' on the
corresponding Datatable row to "Y".


Now it's like this.

private void BtnSearch_Click(object sender, EventArgs e)
{
if (txtName.Text != "" ^ txtProdNr.Text != "")
{
for (int i = 0; i < _dataTable.Rows.Count; i++)
{

if (_datatable.Rows.["Delete"].ToString != "Y")
{

if ((txtName.Text != "" && txtName.Text.ToUpper() ==
_dataTable.Rows["Name"].ToString()) ||
(txtProdNr.Text != "" && txtProdNr.Text.ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
dgStock.Rows.Selected = true;
}
}
}
}

It may be that you take complete control of populating the grid doing it
manually, by reading the Datatable rows manually and populating the grid.

You mark the Datatable row for deletion, you check to see if it has been
marked for deletion, and you physically delete the row from that Datatable
at a later time, not while the grid is working with the Datatable.








__________ Information from ESET NOD32 Antivirus, version of virus signature database 4063 (20090508) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

Mr. Arnold

Tony Johansson said:
Hello!

I will describe my problem fully so that you understand it completely.

I have a DataGridView that is connected to a DataTable like this.
DataTable _dataTable = new DataTable();
BindingSource _bindingSource = new BindingSource();
_bindingSource.DataSource = _dataTable;
dgvStock.DataSource = _bindingSource;

The data that is displayed in the DataGridView is coming from a plain
textfile. Two of the columns in the DataGridView is called Name and ProdNr

I'll keep it simple. You take manual control of the DataTable building the
row columns manually, populating data in the columns manually and adding a
row to the Datatable manually, which is being populated from the textfile
with manual reading of the textfile.

You create a new column on the DataTable row called 'Delete' and you set the
'Delete' column to "N", each time you add a new row to the Datatable from a
read of the textfile to populate the row.

You bind your manually created Datatable to the grid, but you hide the
'Delete' column which is pointing to a row in the Datatable.

You select a row on the grid for deletion, then you set 'Delete' on the
corresponding Datatable row to "Y".


Now it's like this.

private void BtnSearch_Click(object sender, EventArgs e)
{
if (txtName.Text != "" ^ txtProdNr.Text != "")
{
for (int i = 0; i < _dataTable.Rows.Count; i++)
{

if (_datatable.Rows.["Delete"].ToString != "Y")
{

if ((txtName.Text != "" && txtName.Text.ToUpper() ==
_dataTable.Rows["Name"].ToString()) ||
(txtProdNr.Text != "" && txtProdNr.Text.ToUpper() ==
_dataTable.Rows["ProdNr"].ToString()))
dgStock.Rows.Selected = true;
}
}
}
}

It may be that you take complete control of populating the grid doing it
manually, by reading the Datatable rows manually and populating the grid.

You mark the Datatable row for deletion, you check to see if it has been
marked for deletion, and you physically delete the row from that Datatable
at a later time, not while the grid is working with the Datatable.








__________ Information from ESET NOD32 Antivirus, version of virus signature database 4063 (20090508) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

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