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
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