473,418 Members | 3,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

DataGridView - UserDeletingRow and UserDeletedRow (Bug???)

I have a dilemma when trying to delete a row from the DataGridView. Here
is a sample of my code:

private void dgv_EQUPS_UserDeletingRow(object sender,
DataGridViewRowCancelEventArgs e)
{
fDeleteRow = false;

if (chx_DeleteFlag.Checked)
{
DialogResult Ans = MessageBox.Show("Do you want to delete
this Row?", "Delete Row",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);

if (Ans == DialogResult.Yes)
{
fDeleteRow = true;
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
else
{
e.Cancel = true;
}

chx_DeleteFlag.Checked = false;

}

private void dgv_EQUPS_UserDeletedRow(object sender,
DataGridViewRowEventArgs e)
{

if (fDeleteRow)
{
try
{
sql_DA_EQUPS.Update(dsEQUPS, dsEQUPS.EQU_PS.TableName);
}
catch (System.Data.SqlClient.SqlException ex)
{
MessageBox.Show(ex.Message);
}
fDeleteRow = false;
}

}

Here how my code work…
Delete will only work if the chx_DeleteFlag checkbox is checked off.
When a Delete key is pressed on one of the rows in the DataGridView, it
generates a UserDeletingRow Event (dgv_EQUPS_UserDeletingRow) and ask a
question whether or not to Proceed. If Yes, then the fDeleteRow is set (I
used this as a safety value even though it is not needed) and also set the
e.Cancel Flag to false. From this, it will cause another event to be
generated called UserDeletedRow (dgv_EQUPS_UserDeletedRow) which I used to
update the SQL Database Table. As simple as this is, it works great when
dealing with multiple rows of data in the DataGridView.

Here’s the problem…
If there there is only one row of data left in the DataGridView and you try
to delete it, it does not generate the UserDeletedRow event and thus my SQL
Table is not updated. When the DataGridView is refreshed, that data still
exist. Just to verify, I checked the table in the SQL Database and found
that data is still there.

Here’s another problem…
I removed the UserDeletedRow and replaced it with RowsRemoved (this is
where the fDeleteRow flag comes in handy):

private void dgv_EQUPS_RowsRemoved(object sender,
DataGridViewRowsRemovedEventArgs e)
{
if (fDeleteRow)
{
try
{
sql_DA_EQUPS.Update(dsEQUPS, dsEQUPS.EQU_PS.TableName);
}
catch (System.Data.SqlClient.SqlException ex)
{
MessageBox.Show(ex.Message);
}
fDeleteRow = false;
}
}

This works great with single or multiple rows of data in the DataGridView
but I end up with a NullReferenceException. When I debugged this, the
NullReferenceException message goes away if I commented out the
sql_DA_EQUPS.Update statement.

I am verfy much stuck and haven’t found any reason why I cannot delete the
one remaining row in the DataGridView.

Any help would be great.

Thanks … Terry


Apr 25 '06 #1
0 6897

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: dbuchanan | last post by:
Hello, Is this a bug? Is there some kind of work around? I want to add default values for a few columns in my datagridview I found the "DefaultValuesNeeded" event for the datagridview I...
1
by: Bob | last post by:
During a form's development I noticed that when I changed bound dataset defintions with the dataset designer (for instance adding a column to one), when I come back and look at the form the...
0
by: Bob | last post by:
I attached the properties list of the column in a datagridview. The datagridview is bound. Note that the cell visible property is set to false and while looking at the datagridview in the VS IDE...
3
by: manjub | last post by:
Hi, I am trying to add ability to delete multiple rows from a DataGridView in my application. When user selects multiple rows and presses delete button, I want to show a message asking to user...
3
by: Mike | last post by:
Dear group, I'm currrently investigating a bug within a piece of our software whereby if a DataGridView (bound directly to a DataTable) is ordered by column headers (containing lookup combo...
1
by: Amit | last post by:
How do I display a confirmation dialog when the user tries to delete a row? When the user selects a row in the DataGridView and hits the delete key Thanks, Amit
2
by: Rick Shaw | last post by:
Hi, I have a problem with the datagridview not refreshed when the application first appear on the screen. The datagridview display data from a table in the dataset. At the same time, I've added...
4
by: Michael Torville | last post by:
Does anyone know if it's possible to prevent a user from entering a particular "DataGridView" cell. I simply want to redirect them into the next available cell but nothing I try works. Setting the...
0
by: noom | last post by:
Hello I`m trying to use userDeletingRows event but can`t deem to make it work. I tried : this.dataGridView1.UserDeletedRow += new DataGridViewRowCancelEventArgs - but I get an error message...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.