Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,
I am making a c# winform application using an sql database which i bound to my datagridview.After I loaded the collumns I needed, there are rows where I put a null value, and I'd like to remove those each time the cell value in one of my loaded columns is null. Can anyone help me?
The code down below is what I tried so far, but there seems to be an error.

What I have tried:

C#
<pre>private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView2.Columns.OfType<DataGridViewColumn>().ToList().ForEach(col => col.Visible = false);

            dataGridView2.Columns["lotDataGridViewTextBoxColumn"].Visible = true;
            dataGridView2.Columns["ItemDataGridViewTextBoxColumn"].Visible = true;
            dataGridView2.Columns[$"{comboBox1.Text+comboBox2.Text}DataGridViewTextBoxColumn"].Visible = true;
foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                if(dataGridView2.Rows[row.Index].Cells[$"{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value?.ToString() is null)
                {
                    CurrencyManager currencyManager1 = (CurrencyManager)BindingContext[dataGridView2.DataSource];
                    currencyManager1.SuspendBinding();
                    currencyManager1.ResumeBinding();

                }
            }
            



        }
Posted
Updated 15-Jun-23 2:37am
v2
Comments
Graeme_Grant 15-Jun-23 6:11am    
Best solution is in your SQL query, ensure no null rows are returned.
LiterallyGutsFromBerserk 15-Jun-23 6:13am    
I don't know how to do that, since I bound my database in the winform datagridview task menu. How do I access the query from there?

here is the code btw: this.toleranceTableAdapter1.Fill(this.dataSet2.Tolerance);

Richard MacCutchan 15-Jun-23 6:14am    
"there seems to be an error."
And exactly what error would that be?
LiterallyGutsFromBerserk 15-Jun-23 6:18am    
Unable to make uncommitted newline invisible.
Richard MacCutchan 15-Jun-23 6:20am    
And where is that happening? You need to use the debugger to trace which line of code is doing this. It sounds like there is a disconnect between the DataGridView and its BindingSource.

You cannot call ToString (or any other method) on a null value, and any call to ToString (other than the static Convert.ToString) method will always return at the very least an empty string: no successful call to object.ToString will ever return a null value.
So this code:
if(dataGridView2.Rows[row.Index].Cells[$"{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value?.ToString() is null)
Will never evaluate to true: it may throw a null reference exception though.

As Graeme has said, the best solution is to exclude the null value rows from your database access via the SQL query you load your DataAdapter / DataSet from.
 
Share this answer
 
Comments
Richard Deeming 15-Jun-23 7:17am    
Did you miss the null-conditional operator in that call?
(row).Value?.ToString() is null

If Value is null, the ToString method will not be called, and the expression will evaluate to null.

Of course, it would be simpler to write (row).Value is null instead. :)
LiterallyGutsFromBerserk 15-Jun-23 7:23am    
Thank you for your reply. I found a solution thanks to your explanation, which is to turn my "null" cells into an empty string.
OriginalGriff 15-Jun-23 7:38am    
A better solution is to change the SELECT query - then you don't have to faff about at all ...
After a bit of working on my code, I found a simple solution. If your datatable data window show you NULL, it's not the c# null, but rather an equivalent to "" in c#.

Therefore, in order to remove all "null" columns, you simply need to write :

if(dataGridView2.Rows[row.Index].Cells[$"{comboBox1.Text + comboBox2.Text}DataGridViewTextBoxColumn"].Value?.ToString() is null)



Thank you for the explanation Richard and OriginalGriff.
 
Share this answer
 
Comments
CHill60 15-Jun-23 8:58am    
It would still be better to change the SELECT query so that those rows are not loaded in the first place. Add to your query
WHERE ColumnName IS NOT NULL
PIEBALDconsult 15-Jun-23 9:24am    
Please don't try to answer your own question. Just use the Improve Question button to add it there.

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