如何检查gridview中的某个列是否有空行并且能够执行操作 [英] How to check if a certain column in gridview has an empty row and be able to perform an action

查看:44
本文介绍了如何检查gridview中的某个列是否有空行并且能够执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a gridview with columns Action, Sigin Date and Signout date. The "Action Column" is a button field column. For every cell in the Signout date column, i want the button in the same row with the signout date to be enabled. If there is signout date, i want the button to be disabled. How do i do this?





我的尝试:



我已创建数据并将其绑定到我的gridview



What I have tried:

I have created and bound data to my gridview

推荐答案

ButtonField 转换为 TemplateField 列,这样你就可以控制其中的控件:



Convert your ButtonField to TemplateField column so you can have a control over the controls within it:

<asp:templatefield>

< br $>


at RowDataBound 事件,您可以切换已启用根据您的要求,按钮的属性。一个简单的例子是:





The at RowDataBound event, you could toggle the Enabled property of the Button based on your requirement. A quick example would be:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowState != DataControlRowState.Edit){// check for RowState
              if (e.Row.RowType == DataControlRowType.DataRow){ //check for RowType
    
                     // Get the signout value
                     // Note: You may need to change the index of Cells based on the column order
                     string signoutDateString = e.Row.Cells[0].Text; 

                     if(string.IsNullOrEmpty(signoutDateString)){
                            //assuming your Action Column resides within the 5th column
                            //access the LinkButton from the TemplateField using FindControl method
                            LinkButton lb = (LinkButton)e.Row.Cells[4].FindControl("lnkSignOut"); 
                            if (lb != null){
                                 lb.Enabled = false;
                            }
                      }  
               } 
        }
}


这篇关于如何检查gridview中的某个列是否有空行并且能够执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆