Hide a button in a data grid

Discussion in 'ASP .Net' started by kevinpublic@excite.com, Dec 7, 2006.

  1. Guest

    I have an item list for ordered products on a data grid in VS 2003.
    It's an ASP page running VB behind it. All detail lines display as
    well as all shipping charges. On the edit screen, we allow them to
    cancel individual line items. We have the 'Cancel' button enabled for
    the line items of the datagrid. However, I would like to hide that
    button for the shipping charges. We don't want them cancelling their
    shipping fees, after all.

    Has anyone ever tried to hide a cancel/add/delete button in a data grid
    for a specific row?

    Thanks for your help.
    , Dec 7, 2006
    #1
    1. Advertising

  2. OHM Guest

    There are a few approaches you could use. But one way is to handle the
    ItemDataBound event for the datagrid. You can then use the item which is
    passed to the function to interrogate a value in the row and hide the button
    control in the appropriate row.

    HTH

    --
    OHM
    http://TrainingOn.net


    <> wrote in message
    news:...
    >I have an item list for ordered products on a data grid in VS 2003.
    > It's an ASP page running VB behind it. All detail lines display as
    > well as all shipping charges. On the edit screen, we allow them to
    > cancel individual line items. We have the 'Cancel' button enabled for
    > the line items of the datagrid. However, I would like to hide that
    > button for the shipping charges. We don't want them cancelling their
    > shipping fees, after all.
    >
    > Has anyone ever tried to hide a cancel/add/delete button in a data grid
    > for a specific row?
    >
    > Thanks for your help.
    >
    OHM, Dec 7, 2006
    #2
    1. Advertising

  3. mark carew Guest

    Hi
    Don't know about datagrid; but if you use a gridview you just set the text
    property of the cell to null and the button vanishes and is thus disabled
    (also the row is not contracted to the left)
    Viz.
    spreadsCell.Text = null;
    (see the very end of this listing for usage within context)
    horizButton is being turned off when it has no details to display
    HTH
    Mark Carew


    -------------------------------------------------
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class issues : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Session.Remove("articleno");
    }
    protected void umeArticlesRowCommand(Object src, GridViewCommandEventArgs
    e)
    {
    string commandIs = e.CommandName;
    if (commandIs == "HorizSpreads")
    {
    // get the row index stored in the CommandArgument property
    int index = Convert.ToInt32(e.CommandArgument);
    // get the GridViewRow where the command is raised
    GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
    // get the umeNo
    string spreadUmeNo = selectedRow.Cells[0].Text;
    // get the ArticleNo
    string spreadArticleNo = selectedRow.Cells[1].Text;
    // Show the spreads
    Session["UmeNo"] = spreadUmeNo;
    Session["ArticleNo"] = spreadArticleNo;
    Server.Transfer("scrollSpreads.aspx");
    }
    }
    protected void umeIssuesRowDataBound(object sender, GridViewRowEventArgs
    e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    GridView gva = (GridView)e.Row.FindControl("umeArticlesGridView");
    string connectString =
    ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

    // get the description or the articles for this issue
    string stringUmeNo =
    ((DataRowView)e.Row.DataItem)["umeno"].ToString();
    string umeArticleSelectCommand =
    "SELECT * FROM umeArticle WHERE umeno = " +
    stringUmeNo;
    SqlDataSource arts =
    new SqlDataSource(connectString, umeArticleSelectCommand);
    gva.DataSource = arts;
    gva.AutoGenerateColumns = false;

    //
    BoundField bfUme = new BoundField();
    bfUme.DataField = "umeNo";
    gva.Columns.Add(bfUme);
    gva.Columns[0].ItemStyle.Width = 25;

    //
    BoundField bfArticle = new BoundField();
    bfArticle.DataField = "articleNo";
    gva.Columns.Add(bfArticle);
    gva.Columns[1].ItemStyle.Width = 25;

    //
    ButtonField horizButton = new ButtonField();
    horizButton.ButtonType = ButtonType.Image;
    horizButton.ImageUrl = "./Images/smallbluebutton.gif";
    horizButton.CommandName = "HorizSpreads";
    gva.Columns.Add(horizButton);
    gva.Columns[2].ItemStyle.Width = 25;

    //
    BoundField bfDescription = new BoundField();
    bfDescription.DataField = "description";
    bfDescription.ItemStyle.Font.Name = "arial";
    bfDescription.ItemStyle.Font.Size = 9;
    bfDescription.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
    gva.Columns.Add(bfDescription);
    gva.Columns[3].ItemStyle.Width = 550;

    //
    gva.RowDataBound += new
    GridViewRowEventHandler(adjustSpreadRowVisibility);
    //
    gva.DataBind();
    }
    }
    protected void adjustSpreadRowVisibility(object sender,
    GridViewRowEventArgs e)
    {
    // for row that are data rows
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    TableCell umeNo = e.Row.Cells[0];
    umeNo.Visible = false;
    TableCell articleNo = e.Row.Cells[1];
    articleNo.Visible = false;
    // get a reference to the spreads button cell
    TableCell spreadsCell = e.Row.Cells[2];
    // source the connection string from web.config
    string connectString =
    ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    // get the description or the articles for this issue
    string stringUmeNo =
    ((DataRowView)e.Row.DataItem)["umeno"].ToString();
    string stringArticleNo =
    ((DataRowView)e.Row.DataItem)["articleno"].ToString();
    // create a factory to use for the count retrieval
    DbProviderFactory factory;
    string provider = "System.Data.SqlClient";
    factory = DbProviderFactories.GetFactory(provider);
    // open the connection
    DbConnection conn = factory.CreateConnection();
    conn.ConnectionString = connectString;
    conn.Open();
    // create the database command required to issue the count
    DbCommand cmd = factory.CreateCommand();
    cmd.CommandText =
    "SELECT count(*) FROM umeSpread WHERE umeno = " +
    stringUmeNo +
    " AND articleNo = " +
    stringArticleNo +
    " AND spreadImage <> ''";
    cmd.Connection = conn;
    // execute the count query returning a single value
    string spreadCountdr = cmd.ExecuteScalar().ToString();
    conn.Close();
    if (spreadCountdr == "0")
    {
    spreadsCell.Text = null;
    // spreadsCell.Visible = false;
    }
    }
    }
    }
    mark carew, Dec 7, 2006
    #3
  4. Guest

    That worked like a charm. Thanks!

    OHM wrote:
    > There are a few approaches you could use. But one way is to handle the
    > ItemDataBound event for the datagrid. You can then use the item which is
    > passed to the function to interrogate a value in the row and hide the button
    > control in the appropriate row.
    >
    > HTH
    >
    > --
    > OHM
    > http://TrainingOn.net
    , Dec 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kenneth

    Hide boundcolumn in grid?

    Kenneth, Jan 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    398
    Kenneth
    Jan 12, 2004
  2. Jennyfer J Barco

    Menu options hide behind a grid

    Jennyfer J Barco, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    519
    Curt_C [MVP]
    Sep 22, 2004
  3. steven robinson

    How can I hide button border on AWT Button?

    steven robinson, Nov 6, 2003, in forum: Java
    Replies:
    0
    Views:
    944
    steven robinson
    Nov 6, 2003
  4. BK Kim

    hide column in data grid

    BK Kim, Sep 16, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    133
    Alvin Bruney
    Sep 19, 2003
  5. Eddie B.

    Hide data grid cols?

    Eddie B., Oct 17, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    133
    S. Spurlock
    Oct 17, 2003
Loading...

Share This Page