?
Solved

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

Posted on 2014-02-20
13
Medium Priority
?
278 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
Industry Leaders: 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!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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