Mcbaloo Mcbaloo - 6 months ago 52
ASP.NET (C#) Question

Showing and hiding of gridview button based on the value of a cell in the same row

I have a gridview in my ASP.NET. in my gridview columns, i want the button on each row to show or hide if the value of a cell is the same cell is empty or null. for example, i want a button to show on each row that has Signout_Time as null or empty. i have written the code below. the issue i'm having is that the codes works in an opposite way. Buttons are showed on the rows with "Signout_Time" while the button on rows without a value for "Signout_Time" visibility becomes false. it shouldn't be so. I have also tried changing my if conditions, it still didnt work

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;


<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">
<asp:Button ID="out" runat="server" Text="Sign out" CommandName="SignOut" CommandArgument='<%#Eval("SN") %>'/>

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



You can set the button Visibility directly in the GridView Template:

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