Solved

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

Posted on 2014-02-20
13
271 Views
Last Modified: 2014-02-23
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
0
Comment
Question by:vcharles
  • 7
  • 6
13 Comments
 
LVL 40
ID: 39875162
DataGridView1.Columns("ColumnName").Visible = False

or

DataGridView1.Columns(1).Visible = False

where 1 is the position of the column
0
 

Author Comment

by:vcharles
ID: 39875196
Hi,

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

Thanks,

Victor
0
 
LVL 40
ID: 39875215
For Each row As DataGridViewRow In DataGridView1.Rows
                  row.Cells("ColumnName").Value = Nothing
            Next
0
 

Author Comment

by:vcharles
ID: 39875433
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
0
 
LVL 40
ID: 39875785
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
0
 

Author Comment

by:vcharles
ID: 39879273
Hi,

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

Thanks.

V.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 40
ID: 39879301
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
0
 

Author Comment

by:vcharles
ID: 39879467
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.
0
 
LVL 40
ID: 39879478
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.
0
 

Author Comment

by:vcharles
ID: 39879493
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
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39879520
Is that exactly the code that you run? Note that in the code that also loops through the columns, I reset the flag to False when we change column.

If what you show in your last post is exactly the code that you are running, then you will need to use the debugger to understand what happens.

Put a breakpoint on the line that sets flag = True. When you hit the break, look at the value in row.Cells("Quantity").Value. There might be a space or a hidden character in one of the cells. Sometimes, when users enter data, they cut and paste information that has a carriage return in it. This shows as an empty cell because the cell can show only one line while the data contains 2.
0
 

Author Comment

by:vcharles
ID: 39880200
I'm sorry for the late reply, will keep testing and get back to you.

Thanks

V.
0
 

Author Comment

by:vcharles
ID: 39881482
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now