Click here to Skip to main content
15,883,731 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a foreach loop for a winforms datagridview it is hosted inside a wpf usercontrol. The foreach loop skips the first condition automatically declares it a null then runs properly for every row after. The if condition is true.

C#
foreach (DataGridViewRow item in Dashboard_dataGrid.Rows)
    {


        //--[0] is the index of the checkbox Column on the Datagridview
        if (Convert.ToBoolean(item.Cells[0].Value) == true) //if switched to false it works or add true) continue;
        {
            MessageBox.Show("Selected Rows :" + item.Cells[0].RowIndex.ToString()); //just to show where I am at in the index
            sqlCon.Open();

            SqlDataAdapter abc = new SqlDataAdapter("Delete from tbl_ClientProducts Where productID = '" + item.Cells[11].Value.ToString() + "' and  userID = '" + UtilityClass.userID + "'", sqlCon);

            DataTable dtbl = new DataTable();
            abc.Fill(dtbl);
            Dashboard_dataGrid.Update(); //did not work
            Dashboard_dataGrid.Refresh(); // did not work
            sqlCon.Close();
        }

    }
    MessageBox.Show("Your product(s) have been deleted Successfully");


}

What I have tried:

I have changed the condition to false the it evaluates saying there is a null value but its not null .Net has set the cell to null

C#
if (Convert.ToBoolean(item.Cells[0].Value) == false)
Posted
Updated 8-Oct-19 10:00am
v2
Comments
Richard Deeming 7-Oct-19 14:12pm    
"Delete from tbl_ClientProducts Where productID = '" + item.Cells[11].Value.ToString() + "' and userID = '" + UtilityClass.userID + "'"

Don't do it like that!

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

SqlDataAdapter abc = new SqlDataAdapter("Delete from tbl_ClientProducts Where productID = @productID and userID = @userID", sqlCon);
abc.SelectCommand.Parameters.Add("@productID", item.Cells[11].Value);
abc.SelectCommand.Parameters.Add("@userID, UtilityClass.userID);
Member 12349597 7-Oct-19 15:32pm    
The sql injection will be addressed later this is just to get it working how I want before I move into the App security phase. So now idea about the if condition?
Maciej Los 8-Oct-19 9:44am    
You have to develop an application by paying attention on security. You can NOT "improve" application later (as you stated in "security phase)", due to lots of reasons.
Member 12349597 8-Oct-19 11:34am    
Your killing me smalls the issue is an if condition being skipped. Your security application theory has been addressed. Do you have any information that may be useful to an if condition being skipped... If not Thank you but not dealing with security at the moment.
F-ES Sitecore 8-Oct-19 4:07am    
I don't really understand the question, but the problem is probably because you are trying to change the datasource of something while iterating it. Maybe the issue is that the foreach is iterating over the old data and you are expecting it to iterate over the updated data.

0) As already noted, don't do it that way.

1) Why in the hell are you using a Winforms control in WPF? Just because it "can be done", doesn't mean it should be done.

2) Use the MVVM pattern, and perform appropriate binding(s). You should be able to move your foreach loop into your observable collection, thus abstracting it out of the UI. The whole MVVM thing is too big to fit into an answer here because there are so many variations regarding its implementation, but for sure, the form should not have the foreach code in it.
 
Share this answer
 
v2
Comments
Member 12349597 7-Oct-19 16:16pm    
MVVM in my opinion is to abstract and separation of concerns makes the code really bloated. Also if all you need is a datagridview connected to an excess datatsource why would you break the code into 3 to 4 sections for such a generic task? MVVM documentation regarding datagrid checkbox is rally bad, Plus it works great except for this if condition..... Anyone else think they have a reason
#realJSOP 8-Oct-19 10:04am    
You're not bloating code by using MVVM - you're simply moving the code you'd have to write *anyway* into a more logical location.
Hi,

foreach also evaluates the 'new row' if the grid allows the user to add rows.

Check that the row.IsNewRow is false.
 
Share this answer
 
Comments
Member 12349597 8-Oct-19 8:36am    
Thanks, user cannot add new rows.
Assuming that datagridview column[0] holds checkbox control... you have to read DataGridViewCheckBoxCell.TrueValue[^] or DataGridViewCheckBoxCell.FalseValue[^]

C#
foreach (DataGridViewRow item in Dashboard_dataGrid.Rows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[0]; //--[0] is the index of the checkbox Column on the Datagridview
        if (chk.TrueValue) 
        {
           //your logic here...
        }
    }


Note: sometimes it's necessary to check if checkbox value is null.
 
Share this answer
 
Comments
Member 12349597 9-Oct-19 8:52am    
Thank you but that did not work either. After further research I believe it is a metadata problem. It appears that in this specific case the index is 0 and the initial row is starting at 1 but it does not appear to be something I can change at the moment. The current work around is to evaluate each row instead of iterate through them. I do appreciate the reply.

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