Hi,
I'm new to C# and I have just started a pretty big conversion from Clipper... an early hurdle has been, what should have been, a really simple form with a table in SQL displayed in a DataGridView. My problem has been concurrency violations, particularly when I've tried to delete a record programatically - which I need to do because a) I want to do some validation first and b) for some reason the interactive delete from 'Allow Users to Delete' will not delete the last record in the table (it deletes it from the grid, but not the underlying table).
So, DataGridView, linked (bound) to a DataTable and the only way that actually seems to work is not to 'Delete' the record from the datatable, but to make a note of the identity (if that is available - new records don't have one in a DatagridView!) use a remove and then issue a SQL command to delete the underlying record.
Thus I end up with this:
This allows my user to select a number of records, then check that none of them are the 'new record' and then processes the list as I described above.
As I'm planning to use this, very simple, form as the template for the other ones I need - I want to be sure I have the right approach. Am I in the ball park?
In my example, I'm not using the identity field to delete the records (as I said I don't seem to have one for newly added records - at least not in the grid rows) but the Descripton field, which is also unique, and I'm doing a replace in the search variable to weed out single quotes - which are allowed in the field.
Regards
Griff
Keep [Smile]ing
There are 10 kinds of people in the world, those who understand binary and those who don't.
I'm new to C# and I have just started a pretty big conversion from Clipper... an early hurdle has been, what should have been, a really simple form with a table in SQL displayed in a DataGridView. My problem has been concurrency violations, particularly when I've tried to delete a record programatically - which I need to do because a) I want to do some validation first and b) for some reason the interactive delete from 'Allow Users to Delete' will not delete the last record in the table (it deletes it from the grid, but not the underlying table).
So, DataGridView, linked (bound) to a DataTable and the only way that actually seems to work is not to 'Delete' the record from the datatable, but to make a note of the identity (if that is available - new records don't have one in a DatagridView!) use a remove and then issue a SQL command to delete the underlying record.
Thus I end up with this:
Code:
private void btnDeleteElement_Click(object sender, EventArgs e)
{
int i = 0;
foreach (DataGridViewRow drv in dgvElements.SelectedRows)
{
if (drv.IsNewRow )
{
MessageBox.Show("You Cannot Delete the 'New record line'", "Problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
drv.Selected = false;
i = -1;
break;
}
else
{
i++;
}
}
if(i > 0)
{
if (MessageBox.Show("Delete " + i.ToString() + " Record(s)", "Are You Sure", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
SqlCommand command = validConnection.CreateCommand();
foreach (DataGridViewRow drv in dgvElements.SelectedRows)
{
string strSearch = drv.Cells[1].Value.ToString();
// use a select filter, because it works better when you delete records
foreach (DataRow row in dataTable.Select("Description='" + strSearch.Replace("'", "''") + "'"))
{
dataTable.Rows.Remove(row);
command.Parameters.Clear();
command.Parameters.AddWithValue("@Description", strSearch);
command.CommandText = "Delete from tblElements where Description = @Description ";
command.ExecuteNonQuery();
}
}
}
}
}
This allows my user to select a number of records, then check that none of them are the 'new record' and then processes the list as I described above.
As I'm planning to use this, very simple, form as the template for the other ones I need - I want to be sure I have the right approach. Am I in the ball park?
In my example, I'm not using the identity field to delete the records (as I said I don't seem to have one for newly added records - at least not in the grid rows) but the Descripton field, which is also unique, and I'm doing a replace in the search variable to weed out single quotes - which are allowed in the field.
Regards
Griff
Keep [Smile]ing
There are 10 kinds of people in the world, those who understand binary and those who don't.