Best way to check if listview is empty?
12,190
Solution 1
Icarus is right about the order of binding mattering, but I'd like to point out that lv.Items.Any()
is a more efficient way of seeing if there are any items to display. No sense in counting the whole list if you just want to know if at least 1 thing is in it.
Solution 2
lv.Items.Count
would return 0 only if you check before you bind the data. If you check after you bind the data, it returns the number of rows.
Author by
Diego Alves Do Carmo
Updated on June 05, 2022Comments
-
Diego Alves Do Carmo almost 2 years
Believe it or not I searched on google but I was not able to find how to see if a listview is empty. I want to hide some controls if so. I tried lv.items.count but it always returns 0. Whether or not the control is empty.
-
Diego Alves Do Carmo almost 11 yearsI used it in the Load event of the page the result was always 0. Now I placed it inside the databound of the listiview and it worked
-
Diego Alves Do Carmo almost 11 yearsnice I didn't know this method