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
Solved

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

Posted on 2014-02-20
13
274 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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