Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this is to delete the data row with checkbox in datagridview



private void btndelete_Click(object sender, EventArgs e)
{

try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
object cell = row.Cells["dgvdel"].Value;
if (cell =="yes")
{

if (MessageBox.Show("Want to delete","Don't", MessageBoxButtons.YesNo) == DialogResult.OK) ;
{
javidDataSet.sample_data.Rows[row.Index].Delete();
sample_dataTableAdapter.Update(javidDataSet);
}

}
}
}

catch (Exception ex)
{
}


}

its not entering into the messagebox loop...
Posted
Updated 10-Sep-15 21:06pm
Comments
F-ES Sitecore 11-Sep-15 3:14am    
Is this a website?
Xaavier 11-Sep-15 3:18am    
no this is for winform.. after the comparison of cell values "yes".its not entering into messagebox...
Sergey Alexandrovich Kryukov 11-Sep-15 3:23am    
catch (Exception e) {}
is a big abuse.
Hard-coded "dgvdel" and especially "yes" is also a big abuse. Apparently, you have some other "yes" in code. Do you understand that this is not maintainable?

General recipe is: use the debugger.

—SA
Xaavier 11-Sep-15 3:29am    
actually i set true value of the column of the delete(column) as "yes"
without implementing try block and exception its not deleting.

1 solution

We can't solve this for you - we can't run that code in isolation and get teh same results you do with your data.

So start with the debugger.
Put a breakpoint on this line:
C#
if (cell =="yes")
And run your app. when it hits the breakpoint, it will stop, and let you look at exactly what is going on.
Look at the content of "cell" - is it a string? Does it contain "yes" in that exact text, or is the string "Yes", or "YES"?

I suspect that because cell is an object, it's doing a reference comparison between the value returned from your cell and the string "yes" - in which case try this instead:
C#
foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    string cell = (string) row.Cells["dgvdel"].Value;
    if (cell.ToLower() == "yes")
        {

You are almost certainly getting a warning for this when you compile:
Possible unintended reference comparison
If so, then you should always treat warnings as errors to avoid this kind of problem.
 
Share this answer
 
Comments
Xaavier 11-Sep-15 3:43am    
now i am getting executed but it shows Warning Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'
F-ES Sitecore 11-Sep-15 3:52am    
If your cell is a string then you're better not using object at all

string cell = row.Cells["dgvdel"].Value.ToString();
OriginalGriff 11-Sep-15 4:01am    
As I said you should!
The difference is like comparing two people: the simplest way is to do a Value Comparison - "are the names the same?"
So if look at the names of each, and they are both "John Smith" then the value comparison says "true". But does that mean they are the same person? No - if you do a Reference Comparison "Are these the same person?" then you will get "false".
Strings are normally compared using a Value comparison: each character in each string is compared, and they are the same if and only if every character pair matches identically.
But when one side is an object, you get a Reference comparison instead: the "pointer to the memory" for each is compared instead of the actual characters in the strings.

As I said: treat warnings as errors - the compiler does know better than you (and me!)
I have all my projects set to do this automatically - my projects will not compile with any warnings.
Open your project in the Solution Explorer pane, and double click the "Properties" branch.
In the resulting page, select the "Build" tab on the left.
Make sure that "Warning level" is set to "4" (the highest) and that "Treat warnings as errors" is set to "All".
Save and close the page.

Now when you compile, it won't let you do things like that which may have unintended side effects.

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