Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with deleting emply column in DataGrid View using VB.NET

Hi,

My DataGrid displays data in different columns depending on which chapter is being displayded. How do I make invisible blank columns after the Grid is loaded?

Thanks,

Victor
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

DataGridView1.Columns("ColumnName").Visible = False

or

DataGridView1.Columns(1).Visible = False

where 1 is the position of the column
Avatar of Victor  Charles

ASKER

Hi,

How do you loop through the DataGrid to make sure the column is emply before making it invisible?

Thanks,

Victor
For Each row As DataGridViewRow In DataGridView1.Rows
                  row.Cells("ColumnName").Value = Nothing
            Next
Hi,

I wrote the code below, but I think there's a smarter way to achieve this.


For Each row As DataGridViewRow In DataGridView1.Rows
If   row.Cells("ColumnName").Value = Nothing then
                 DataGridView1.Columns(1).Visible = False
Else
                DataGridView1.Columns(1).Visible = True
Endif
Next

How do you achexecute the codeieve on the last row instead of for each row?

Thanks,

Victor
OK, I thought you wanted to empty the column, not check if it was empty.

The way you are doing it, it changes for every row, and only the last one stays when finished.

In order to check if it is empty simply loop until you find at least one cell that contains a value. Once you find one you know that the column is not empty.

The following should do it:

Dim flag As Boolean
For Each row As DataGridViewRow In DataGridView1.Rows
       If   row.Cells("ColumnName").Value IsNot Nothing then
                 flag = True
                 Exit For
       End If
Next
DataGridView1.Columns(1).Visible = flag
Hi,

It works.  How do I modify the code to apply to all the columns in the grid?

Thanks.

V.
Dim flag As Boolean
For Each col As DataGridViewColumn in DataGridView1.Columns
flag=false
For Each row As DataGridViewRow In DataGridView1.Rows
       If   row.Cells("ColumnName").Value IsNot Nothing then
                 flag = True
                 Exit For
       End If
Next
col.Visible = flag
Next
Hi again,

I'm afraid it's not working as I thought. The Quantity column is still visible eventhough the column is blank.

 Dim flag As Boolean
        For Each row As DataGridViewRow In C1Screen3.Rows
            If row.Cells("QUANTITY").Value IsNot Nothing Then
                flag = True
                Exit For
            Else
                flag = False
            End If
        Next

        C1Screen3.Columns("QUANTITY").Visible = flag

V.
Where do you see the following in my code:

        Else
                flag = False

You rare basically using the same code as you did before. Remove the Else and it should work.
I included it to try to solve the problem, it does not work with the code below: It seems like flag = true even when the column is empty. Id there another way to test if the values are blank besides using IsNot Nothing?

  Dim flag As Boolean
        For Each row As DataGridViewRow In C1Screen3.Rows
            If row.Cells("QUANTITY").Value IsNot Nothing Then
                flag = True
                Exit For
            End If
        Next
        C1Screen3.Columns("QUANTITY").Visible = flag
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm sorry for the late reply, will keep testing and get back to you.

Thanks

V.
Hi,

It's working with the code below, for some reason "If row.Cells("QUANTITY").Value IsNot Nothing Then" does not work when the column is empty.

 For Each row As DataGridViewRow In C1Screen3.Rows
            If (row.Cells("QUANTITY").Value.ToString) <> "" Then
                'If row.Cells("QUANTITY").Value IsNot Nothing Then
                flag = True
                Exit For
            Else
                flag = False
            End If
        Next
        C1Screen3.Columns("QUANTITY").Visible = flag

Thank You.

V.