[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

remove empty rows in Gridview

Is there a way to remove rows from Gridview if the entrie rows are rows are empty?  I do not know in advance the name of the columns or how many columns are are going to be.
0
VBdotnet2005
Asked:
VBdotnet2005
  • 5
1 Solution
 
RickCommented:
Just make sure that you check its Data Source before Data Binding.
0
 
Craig WagnerSoftware ArchitectCommented:
Because you mention GridView I'm assuming you're talking about an ASP.NET application using WebForms.

Are you binding the GridView to something or filling it programmatically? If you're binding it, why not filter the datasource before doing the bind (which I think is what the previous responder was getting at). There are several ways to approach that, depending on what the datasource is (objects, datatable, etc).
0
 
VBdotnet2005Author Commented:
The source is Excel. When I bind to Gridview, I get some empty rows. I want to be able to remove the entire row if they are blank.
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!

 
RickCommented:
See if this works:




Protected Sub Gridview1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound

        If e.Row.RowType = DataControlRowType.EmptyDataRow Then

          e.Row.Style.Add("display", "none")       

        End If

End Sub

Open in new window

0
 
RickCommented:
If not, you might need to check each cell:
Sub Gridview1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound

dim bolEmptyRow as boolean = false


for i as integer = 0 to e.row.cells.count - 1

if e.row.cells.item(i).text.trim = "" then
  bolEmptyRow = true
end if

if bolEmptyRow = true then
  e.row.style.add("display", "none")
end If

next


End Sub

Open in new window

0
 
RickCommented:
Oops... you might need to declare bolEptyRow as global then modify the code above:
Partial Class Utilities_SkuLookUp_SkuDetail
    Inherits System.Web.UI.Page

dim bolEmptyRow as boolean = False
dim iRow as integer = 0


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' ...
End Sub


Sub Gridview1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound

for i as integer = 0 to e.row.cells.count - 1

if e.row.cells.item(i).text.trim = "" then
  bolEmptyRow = true
end if

next

iRow = e.row.rowindex
DeleteRow()

End Sub


Sub DeleteRow()

if bolEmptyRow = true then

  Gridview1.rows(iRow).style.add("display", "none")

end If

End Sub

End Class

Open in new window

0
 
RickCommented:

Partial Class Utilities_SkuLookUp_SkuDetail
    Inherits System.Web.UI.Page

dim bolEmptyRow as boolean = False
dim iRow as integer = 0


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' ...
End Sub


Sub Gridview1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound

bolEmptyRow = false

for i as integer = 0 to e.row.cells.count - 1

if e.row.cells.item(i).text.trim = "" then
  bolEmptyRow = true
end if

next

iRow = e.row.rowindex
DeleteRow()

End Sub


Sub DeleteRow()

if bolEmptyRow = true then

  Gridview1.rows(iRow).style.add("display", "none")

end If

End Sub

End Class

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now