根据同一行中单元格的值显示和隐藏gridview按钮

时间:2016-10-11 12:32:37

标签: c# asp.net gridview

我的ASP.NET中有一个gridview。在我的gridview列中,我希望每行上的按钮显示或隐藏单元格的值是相同的单元格是空还是空。例如,我想要一个按钮在每行上显示Signout_Time为null或空。我写了下面的代码。我遇到的问题是代码以相反的方式工作。按钮显示在" Signout_Time"行上。而行上的按钮没有" Signout_Time"的值能见度变得虚假。它不应该如此。我也试过改变我的if条件,它仍然无法正常工作

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["Signout_Time"].ToString())) {
              Button status = (Button)e.Row.FindControl("out");

              if (status != null) {
                  status.Visible = true;
              }
          }

          break
     }
 }

                    <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") %>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>

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


        </asp:GridView>

1 个答案:

答案 0 :(得分:0)

您可以直接在GridView模板中设置按钮可见性:

<asp:Button Visible='<%# string.IsNullOrEmpty(Eval("Signout_Time").ToString()) %>' runat="server" Text="Sign out" ID="out"  CommandName="SignOut" CommandArgument='<%#Eval("SN") %>'/>