Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.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") %>' CssClass="active"/>
                               </ItemTemplate>
                           </asp:TemplateField>
                       </Columns>

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






code behind

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

                  }
                  }
           }


       }
Posted
Updated 10-Oct-16 11:53am

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.

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["yourfieldnames"].ToString()))
	            {
		           Button status = (Button)e.Row.FindControl("out");
	               if (status != null)
	               {
			           status.visible = false;
		           }
	            }
				break;
     }
}
 
Share this answer
 
v2
Comments
Mcbaloo 11-Oct-16 3:56am    
There is a bit of problem tho. The control is working in opposite way. For every field that is empty or null, the Button's visibility becomes false while for those if the field contains a value(the value is a datetime), the button appears. i have tried modifying the codes but still not working.
Mcbaloo 11-Oct-16 10:06am    
Just needed to change it to if (!String.IsNullOrEmpty(myDataRowView["yourfieldnames"].ToString()))
Anil Kumar Palla 16-Nov-22 8:01am    
yourfieldnames means?
Have you tried debugging your code to verify if it hits your if-condition?

Also try to validate for empty values like this instead:

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


For details, see: String.IsNullOrWhiteSpace Method (String) (System)[^]
 
Share this answer
 
v2

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