Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hiya, I wonder if anyone can help me with this code.
What I am trying to do, is to read records from a database and display them on a gridview. However my last column of the grid is a templateField which I display an imageButton to do something once clicked.
My problem is :

How to display the ImageButton only when a particularly Cell of the grid is value true.
I was trying to set the ImageButton to false when the value was False, but it is not working

Any help please

*******************************************************

private void BindCustomDateBookings()
{
try
{
//Gets the date chosen by the user
var customDate = Convert.ToDateTime(txtDatePicker.Text);

//Creates a connection to the database
SqlConnection connectionViewBookings = new SqlConnection(connectionString);
//Query builder
SqlDataAdapter dataAdapterViewBookings = new SqlDataAdapter(queryCustomDateBookings, connectionViewBookings);
dataAdapterViewBookings.SelectCommand.Parameters.AddWithValue("@customDate", customDate);

//Create the data that will hold the result of DB
DataSet dataSetViewTodaysBookings = new DataSet();

//open, save results and close connection
connectionViewBookings.Open();
dataAdapterViewBookings.Fill(dataSetViewTodaysBookings);
connectionViewBookings.Close();

//fill the grid with results
GridViewCustomDate.DataSource = dataSetViewTodaysBookings.Tables[0];
GridViewCustomDate.DataBind();
}
catch (SqlException SqlExceptionTomorrow)
{
Response.Write(@"<SCRIPT LANGUAGE=""JavaScript"">alert('Could not connect to the database This date bookings')</SCRIPT>");
}
}//Ends Bind Custom Date bookings

***********************************************************
And the grid view has the following coded
XML
<asp:GridView ID="GridViewCustomDate" runat="server" AutoGenerateColumns="False" CssClass="viewBookingTable" GridLines="None"
             HeaderStyle-CssClass="gridViewHeader">
             <HeaderStyle CssClass="gridViewHeader" />
             <RowStyle CssClass="GridViewRowStyle" />
             <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle " />

              <Columns>
                  <asp:BoundField DataField="bookingID" ItemStyle-CssClass="displayGridViewID"
                      HeaderStyle-CssClass="displayGridViewID" >
                  <HeaderStyle CssClass="displayGridViewID" />
                  <ItemStyle CssClass="displayGridViewID" />
                  </asp:BoundField>
             <asp:BoundField DataField="visitorName" HeaderText="Visitor Name" />
             <asp:BoundField DataField="contactNo" HeaderText="contactNo" />
             <asp:BoundField DataField="hostName" HeaderText="Host Name" />
             <asp:BoundField DataField="roomName" HeaderText="Room Name" />
             <asp:BoundField DataField="timeIn" HeaderText="Time In" />
             <asp:BoundField DataField="smartCardNo" HeaderText="Smart Card No" />
                  <asp:TemplateField HeaderText="Required Access">
                      <ItemTemplate>
                          <asp:ImageButton ID="btnGetRequiredAccessCustomDate" runat="server"
                              ImageUrl="~/images/buttons/clickMe.png"
                              onclick="btnGetRequiredAccessCustomDate_Click" />
                      </ItemTemplate>
                  </asp:TemplateField>
              </Columns>

        </asp:GridView>
Posted

Hi,
you can just add the following cond in your imagebutton code

ASP.NET
<asp:imagebutton id="btnGetRequiredAccessCustomDate" runat="server" xmlns:asp="#unknown">
                              ImageUrl="~/images/buttons/clickMe.png"
                  Visible='<%#string.IsNullOrEmpty(Eval('ColumnName')==true? "false":"true" %>'
                              onclick="btnGetRequiredAccessCustomDate_Click" /></asp:imagebutton>
 
Share this answer
 
Have a look at this simple example, It might help you..
You have to assign a bound field ( boolean ) value to the visible property of the button.


C#
DataTable dt = new DataTable();
              dt.Columns.Add("smartCardNo", typeof(string));
              dt.Columns.Add("FlagVisible_Invisible", typeof(bool));
              dt.Rows.Add("one", false);
              dt.Rows.Add("two", true);
              dt.Rows.Add("three", true);

              GridView1.DataSource = dt;
              GridView1.DataBind();



XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%">
        <Columns>
         <asp:BoundField DataField="smartCardNo" HeaderText="Smart Card No" />
            <asp:TemplateField HeaderText="Required Access">
                <ItemTemplate>
                    <asp:ImageButton ID="btnGetRequiredAccessCustomDate" Visible='<%# Eval("FlagVisible_Invisible") %>' runat="server" ImageUrl="~/images/buttons/clickMe.png"
                          />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 
Share this answer
 
You can use RowDataBound Event.
Add OnRowDataBound="GridViewCustomDate_RowDataBound" to the GridView Markup.
C#
protected void GridViewCustomDate_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get that Column value.
        if (!thatValue)
        {
            ((Image)e.Row.FindControl("btnGetRequiredAccessCustomDate")).Visible = false;
        }
    }
}
 
Share this answer
 

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