Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made deleting Msgbox...

1. it was deleted successfully
2. But when i give no again it was deleted 

Problem is: if i choose "NO" dont delete the record in datagridview as well as datasource and keep as it is


What I have tried:

<pre>If True Then

                If MessageBox.Show("Do You Want To Remove This Row", "Remove Row", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = DialogResult.Yes Then
                    DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
                Else
                    MessageBox.Show("Row Not Removed", "Remove Row", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    DataGridView1.Rows.Add(DataGridView1.CurrentRow.Index)
                End If
            End If
Posted
Updated 3-Mar-20 5:42am
v3

Firstly, this line:
VB
If True Then
Is irrelevant: the code that follows it will always be executed.

Secondly, why are you adding a row if it's not to be removed?
VB
Else
    MessageBox.Show("Row Not Removed", "Remove Row", MessageBoxButtons.OK, MessageBoxIcon.Information)
    DataGridView1.Rows.Add(DataGridView1.CurrentRow.Index)
End If

Thirdly, you shouldn't really remove rows from the DGV, but from the DataSource it's bound to.

The code you show otherwise looks OK - so I'd start with the debugger and try to find out exactly is happening - put a breakpoint on the first line of the method, and single step through to see exactly where it is going and what variables contain.
 
Share this answer
 
Comments
Member 14621280 3-Mar-20 10:47am    
Any better commends feedback from anyone....appreciated
Member 14621280 3-Mar-20 10:49am    
I cant understand the way of telling originalGriff
OriginalGriff 3-Mar-20 10:58am    
You know how to use the debugger, don't you?
Member 14621280 wrote:
Any better commends feedback from anyone....appreciated
To be honest I can't think of a better way of putting that @OriginalGriff already has, but I'll try

However, picking up on Griff's question
Quote:
You know how to use the debugger, don't you?
just in case you don't here is a link to a good article on using the debugger Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

You should change the code in line with what Griff suggested
VB
'Remove this if statement! If True Then

If MessageBox.Show("Do You Want To Remove This Row", "Remove Row", MessageBoxButtons.YesNo, MessageBoxIcon.Stop) = DialogResult.Yes Then
    'Only do this if you are manually updating your datagridview
    DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
    'otherwise update your data source here and rebind the datagridview
Else
    MessageBox.Show("Row Not Removed", "Remove Row", MessageBoxButtons.OK, MessageBoxIcon.Information)
'Remove this line DataGridView1.Rows.Add(DataGridView1.CurrentRow.Index)
End If
'Remove this End If
See solution 1 here for how to rebind how to refresh datagridview in vb.net[^]
 
Share this answer
 
Comments
Member 14621280 3-Mar-20 13:13pm    
I tried this also but if I click No again it's deleting....
Member 14621280 4-Mar-20 0:42am    
If MsgBox("Are you sure to Delete?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "Warning") = MsgBoxResult.Yes Then
MsgBox("Yes clicked")

Else
MsgBox("No clicked")
End If


I have try this but i couldn't remember when i click No button it won't delete

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900