vb.net - how to count the rows with blank cells in datagridview -


i'm user of vb.net 2008 , developing system our department. need regarding count rows have empty cells in datagridview. count of rows must displayed in label.

here's code.

for = 0 dgvmonitoringboard.rows.count - 1     if dgvmonitoringboard.rows(i).cells(24).value.tostring = " "         x += 1     end if next  lblfortransfer.text = "items transfer purchasing:" & x 

a little different approach @equisde. use linq case. return count of how many rows either have dbnull.value or empty string...

here's 1 liner...

 dim count integer = datagridview1.rows.cast(of datagridviewrow).where(function(r) r.cells.cast(of datagridviewcell).tolist.any(function(c) c.value dbnull.value orelse string.isnullorempty(cstr(c.value).trim))).count 

here's top down - sometime's easier read...

   dim count integer = datagridview1.rows.cast(of datagridviewrow) _                           .where(function(r) r.cells.cast(of datagridviewcell).tolist _                           .any(function(c) c.value dbnull.value _                           orelse string.isnullorempty(cstr(c.value).trim))).count 

then can do...

 lblfortransfer.text = "items transfer purchasing: " & count.tostring 

side note: try not use + string concatenation use & string concatenation. can put in function , pass datagridview way can re-use anywhere need , return value.

update per request

here shared function wrote takes datagridview object , optional column index searching you. function can used anywhere use it... note: exclude new rows if have allow adding rows true, if it's not allowed doesn't matter.

  public shared function emptycount(byval dgrid datagridview, optional byval intcolumn integer = -1) integer     dim count integer = 0      if dgrid isnot nothing andalso dgrid.rows.count > 0         if intcolumn >= 0 'specific column...             if intcolumn <= dgrid.columns.count                 count = dgrid.rows.cast(of datagridviewrow).where(function(rs) not rs.isnewrow) _                     .select(function(r) r.cells(intcolumn)).where(function(r) r.value dbnull.value _                      orelse string.isnullorempty(cstr(r.value))).count             end if         else 'any columns...             count = dgrid.rows.cast(of datagridviewrow).where(function(rs) not rs.isnewrow) _                      .where(function(r) r.cells.cast(of datagridviewcell).tolist _                      .any(function(c) c.value dbnull.value _                      orelse string.isnullorempty(cstr(c.value).trim))).count         end if     end if      return count end function 

usage example

 'include columns...  lblfortransfer.text = "items transfer purchasing: " & emptycount(yourdatagridviewname).tostring   'specific column...  lblfortransfer.text = "items transfer purchasing: " & emptycount(yourdatagridviewname, 24).tostring 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -