如果行包含空单元格,如何在gridview中显示和隐藏按钮字段 [英] How to show and hide a button field in gridview if a row contains an empty cell

查看:87
本文介绍了如果行包含空单元格,如何在gridview中显示和隐藏按钮字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i have a gridview in my asp.net application. for example 
SN  name   Date     Action
1  mike          button(do something). 
This is just one out of my gridview rows. i want for every row that has an empty cell, button field cell for the row should show. if there's no empty cell in that row, the button should hide.What i get with my code is that all the button visible becomes false which i donot want.





我尝试了什么:





What I have tried:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Height="326px" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5" style="text-align: left; margin-left: 169px" Width="1069px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">

                       <Columns>
                           <asp:BoundField HeaderText="S/N" DataField="SN" />
                           <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                           <asp:BoundField HeaderText="Address" DataField="Address" />
                           <asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
                           <asp:BoundField HeaderText="Sex" DataField="Sex" />
                           <asp:BoundField HeaderText="Reason" DataField="Reason" />
                           <asp:BoundField HeaderText="SignIn" DataField="SignIn_Time" />
                           <asp:BoundField HeaderText="SignOut" DataField="Signout_Time" />

                           <asp:TemplateField HeaderText="Action" Visible="True">
                               <ItemTemplate>
                                   <asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut"  CommandArgument='<%#Eval("SN") %>' CssClass="active"/>
                               </ItemTemplate>
                           </asp:TemplateField>
                       </Columns>

               <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />













代码落后









code behind

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {
            if (e.Row.RowType == DataControlRowType.DataRow)
              {
                 for (int i =0 ; i < e.Row.Cells.Count; i ++)
                   {
                if (e.Row.Cells[i].Text == "&nbsp;")
                   {
                Button status = (Button)e.Row.FindControl("out");
                  status.Visible = true;

                    }
                   else {
               Button status = (Button)e.Row.FindControl("out");
               status.Visible = false;

                  }
                  }
           }


       }

推荐答案

字段值的数据项可以在rowdatabound事件期间逐个获取。不要混淆运行迭代循环来获取每个值。如果你有'四'记录计数的查询结果它将自动运行4次rowdatabound事件。



Data Item of Field Value can be obtained in one by one during rowdatabound event.Don't be confused to run a iteration loop to get each values.If you have 'four' record count of query result it will automatically run in 4 times rowdatabound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    switch (e.Row.RowType)
    {
            case DataControlRowType.DataRow:
                DataRowView myDataRowView = (DataRowView)e.Row.DataItem;
	            if (String.IsNullOrEmpty(myDataRowView["yourfieldnames"].ToString()))
	            {
		           Button status = (Button)e.Row.FindControl("out");
	               if (status != null)
	               {
			           status.visible = false;
		           }
	            }
				break;
     }
}


您是否尝试调试代码以验证它是否达到 if-condition



也尝试验证这样的空值:



Have you tried debugging your code to verify if it hits your if-condition?

Also try to validate for empty values like this instead:

Button status = (Button)e.Row.FindControl("out");
if (string.IsNullOrWhiteSpace(e.Row.Cells[i].Text))
        status.Visible = true;
else
        status.Visible = false;





详情请见:String.IsNullOrWhiteSpace方法(字符串)(系统) [ ^ ]


这篇关于如果行包含空单元格,如何在gridview中显示和隐藏按钮字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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