Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

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.
Avatar of Rick
Rick

Just make sure that you check its Data Source before Data Binding.
Avatar of Craig Wagner
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).
Avatar of VBdotnet2005

ASKER

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

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

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

ASKER CERTIFIED SOLUTION
Avatar of Rick
Rick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial