Victor Charles
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
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
ASKER
Hi,
How do you loop through the DataGrid to make sure the column is emply before making it invisible?
Thanks,
Victor
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").Va lue = Nothing
Next
row.Cells("ColumnName").Va
Next
ASKER
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").Va lue = Nothing then
DataGridView1.Columns(1).V isible = False
Else
DataGridView1.Columns(1).V isible = True
Endif
Next
How do you achexecute the codeieve on the last row instead of for each row?
Thanks,
Victor
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").Va
DataGridView1.Columns(1).V
Else
DataGridView1.Columns(1).V
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").Va lue IsNot Nothing then
flag = True
Exit For
End If
Next
DataGridView1.Columns(1).V isible = flag
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").Va
flag = True
Exit For
End If
Next
DataGridView1.Columns(1).V
ASKER
Hi,
It works. How do I modify the code to apply to all the columns in the grid?
Thanks.
V.
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").Va lue IsNot Nothing then
flag = True
Exit For
End If
Next
col.Visible = flag
Next
For Each col As DataGridViewColumn in DataGridView1.Columns
flag=false
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells("ColumnName").Va
flag = True
Exit For
End If
Next
col.Visible = flag
Next
ASKER
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").Valu e IsNot Nothing Then
flag = True
Exit For
Else
flag = False
End If
Next
C1Screen3.Columns("QUANTIT Y").Visibl e = flag
V.
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").Valu
flag = True
Exit For
Else
flag = False
End If
Next
C1Screen3.Columns("QUANTIT
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.
Else
flag = False
You rare basically using the same code as you did before. Remove the Else and it should work.
ASKER
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").Valu e IsNot Nothing Then
flag = True
Exit For
End If
Next
C1Screen3.Columns("QUANTIT Y").Visibl e = flag
Dim flag As Boolean
For Each row As DataGridViewRow In C1Screen3.Rows
If row.Cells("QUANTITY").Valu
flag = True
Exit For
End If
Next
C1Screen3.Columns("QUANTIT
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm sorry for the late reply, will keep testing and get back to you.
Thanks
V.
Thanks
V.
ASKER
Hi,
It's working with the code below, for some reason "If row.Cells("QUANTITY").Valu e IsNot Nothing Then" does not work when the column is empty.
For Each row As DataGridViewRow In C1Screen3.Rows
If (row.Cells("QUANTITY").Val ue.ToStrin g) <> "" Then
'If row.Cells("QUANTITY").Valu e IsNot Nothing Then
flag = True
Exit For
Else
flag = False
End If
Next
C1Screen3.Columns("QUANTIT Y").Visibl e = flag
Thank You.
V.
It's working with the code below, for some reason "If row.Cells("QUANTITY").Valu
For Each row As DataGridViewRow In C1Screen3.Rows
If (row.Cells("QUANTITY").Val
'If row.Cells("QUANTITY").Valu
flag = True
Exit For
Else
flag = False
End If
Next
C1Screen3.Columns("QUANTIT
Thank You.
V.
or
DataGridView1.Columns(1).V
where 1 is the position of the column