Solved

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

Posted on 2014-02-20
13
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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