Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm currently working on a project wheere, for each category and subcategory of my products, I need to do some inspections. Here's how my winforms works:

1.I click on the first combobox which indicates categories :[^]

2. I select a subcategory on the second combobox[^]

3.The datagridview is generated, and generates with itself some data measure samples. Depending on the inspection criteria, I get 1,3,5 or 7 measures. I get the maximum number of measures n, then generate columns n times, and do not give the right to n-k columns for each k<n.

<a href="">[^]

What I have tried:

The issue I'm facing is that, it only fills the first time, and not the other times. Can anyone please help me out with it?
Updated 6-Jul-23 0:48am

First off, never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
You may get away with it at the moment if the Combo boxes aren't user editable, but when someone requests a change in six months time, are you going to remember that code? Always use parameters!

Secondly, Without you DB there is no way we can run that chunk of code to try and work out what might be happening - there is too much data dependence in there with all the "magic strings" for it to be viable for use to duplicate your problem.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer
LiterallyGutsFromBerserk 27-Jun-23 6:16am    
Thank you for introducing me to this great tool. I've come to find out that the issue is in the loop that encapsulates the coloring line when the winforms is refreshed, which begs a whole new question : how exactly do the foreach(row in datagridview.rows) work? they seem to be different from the classic foreach loops
OriginalGriff 27-Jun-23 6:30am    
In what way?
LiterallyGutsFromBerserk 27-Jun-23 6:44am    
Actually the loop has nothing to do with the issue. It's the fill backcolor value that doesn't refresh each time I change my index
OriginalGriff 27-Jun-23 7:05am    
That's probably because you don't Invalidate the DGV - that's what triggers a redraw.
Have you seen this:
Colouring DataGridView Cells According to their Content in WinForms[^] - it may help.
LiterallyGutsFromBerserk 27-Jun-23 7:19am    
does that mean I have to include the code that helps me color my cells in the cellpainting method?
As OriginalGriff said in their comment, the solution to this issue is to simply edit the formatting from the cellformatting method in order to avoid changes being overridden.

In my own case, I declared an global integer "numColsGen"(number of columns generated),which helped me know how many columns I had generated. Then, in cell_formatting, depending on b's value, I formatted my cell in a certain way.

Here is the code to it :

if (numColsGen== 5)
                if (dataGridView2.Rows[e.RowIndex].Cells[$"donnees{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value.ToString() is "1")
                    if ((dataGridView2.Columns[e.ColumnIndex].HeaderText is "Donnee2") || (dataGridView2.Columns[e.ColumnIndex].HeaderText is "Donnee3") || (dataGridView2.Columns[e.ColumnIndex].HeaderText is "Donnee4") || (dataGridView2.Columns[e.ColumnIndex].HeaderText is "Donnee5"))

                        e.CellStyle.BackColor = System.Drawing.Color.Gray;
                        dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;

                    else if (dataGridView2.Rows[e.RowIndex].Cells[$"donnees{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value.ToString() is "3")
                        if ((dataGridView2.Columns[e.ColumnIndex].HeaderText is "Donnee4") || (dataGridView2.Columns[e.ColumnIndex].HeaderText is "Donnee5"))
                            e.CellStyle.BackColor = System.Drawing.Color.Gray;
                            dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly = true;


Thanks for your help and post, OriginalGriff!
Share this answer
Graeme_Grant 4-Jul-23 10:37am    
'b' is a vague variable name. If you come back to this code in 12 months, 2 years, 5 years, will you know what 'b' is for? Give it a name that is self descriptive. That way, when you come back at a later date, the code will read like a book and you will know immediately what it is for rather than sit there scratching your noggin thinking "what is this for again???"... the days of counting you bytes due to limited memory in your Commodore 64 are a thing of the past ...
LiterallyGutsFromBerserk 4-Jul-23 10:39am    
I changed it, thanks for your input

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