简体   繁体   中英

check for empty gridview row C#

How to check for empty gridview row. I have tried the following

 for (int i = 0; i < 5; i++)
 {
     if(i=0 &&
       (Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name").Text!="" && 
       gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null)
        {
          // do something
        }

 }

I have getting an error stating that System.Web.UI.control does not contain definition for text.

How to check if the row exists and is empty or null?

Thanks

This:

if(i=0 && (Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name").
Text!="" &&   gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null)  

Should be This:

if((i==0) && (gvMaster.Rows[0].Cells[0].FindControl("dealer_name") !=null  &&   
(((Label)gvMaster.Rows[0].Cells[0].FindControl("dealer_name")).Text  
.ToString().Trim()!=""))

Explanation :

1.for comparing values you should use == instead of single = .
2.You have to Cast the Control before reading the property values, Cast the Control as Label .
3. Trim the values before comparing strings for avoiding white space problems.
4.first do a null check before accessing the Control properties, because if control is not found it throws an Exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM