Click here to Skip to main content
14,883,273 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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 shouldnt be so. I have also tried changing my if conditions, it still didnt work

What I have tried:

ASP.NET
<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") %>'/>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        
                <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="5" />
                        
                        
            </asp:GridView>

HTML




C#
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;
}

}
Posted
Updated 11-Oct-16 3:54am

The result is opposite than the expected because the if condition is wrong.
Just add a not to the condition and it should work as expected.
C#
if (!String.IsNullOrEmpty(myDataRowView["Signout_Time"].ToString()))
{
  //rest of code


Please let me know, if it doesn't helps :)
   
Comments
Mcbaloo 11-Oct-16 9:51am
   
it worked perfectly. i had to change my status.visible = false. It worked that way with the solution you provided
Suvendu Shekhar Giri 11-Oct-16 12:30pm
   
Glad that your problem is resolved :)
I have discovered another easier way of doing it in my ItemTemplae. this is what i did

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



This also did the magic. Both solutions are perfect
   
v2
Comments
Suvendu Shekhar Giri 11-Oct-16 12:30pm
   
Great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900