Make one cell in gridview invisible?

Discussion in 'ASP .Net' started by Jo, Apr 6, 2006.

  1. Jo

    Jo Guest

    Hi all,

    I have a gridview displaying a dataset and some buttons. When a certain
    cell of this datasetrow is empty, I want a button in that row to be
    visible, otherwise no button should be shown in that row.

    I'm trying to make this work all day, but it's not going to work.

    I tried this in the rowCreated event. I am able to find the right row
    and cell in this row (as a datarow), but now the right gridview
    row-cell should be made visible.

    This is what I have right now (C#):

    protected void supplierFilesGrid_RowCreated(object sender,
    GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    DataRowView drView = (DataRowView)e.Row.DataItem;
    DataRow drRow = drView.Row;

    if (drRow[2].ToString() == "NEW")
    {
    int i = e.Row.RowIndex; //
    this works fine, after this I get an error:
    //grid.Rows.Cells[1].Visible = false; //
    argumentOutOfRangeException...
    (sender as GridView).Rows.Cells[1].Visible =
    false;
    }
    }
    }

    Has anyone a suggestion how to make this work?

    Thanks in advance!

    Regards,
    Jo
     
    Jo, Apr 6, 2006
    #1
    1. Advertising

  2. Jo

    sirfunusa Guest

    I believe you want the grid.ItemDataBound event
     
    sirfunusa, Apr 6, 2006
    #2
    1. Advertising

  3. Jo

    Jo Guest

    Hi,
    Maybe I do, but what should I do within this event?

    Regards,
    Jo
     
    Jo, Apr 6, 2006
    #3
  4. Jo

    sirfunusa Guest

    Here is a snippet of code I have in this event to get you started.

    e.Item.Cells.Item(4).ToolTip = e.Item.Cells.Item(5).Text
     
    sirfunusa, Apr 6, 2006
    #4
  5. Jo

    Jo Guest

    Hi,
    Thanks for your quick reply! You gave me the right hint to continue
    this 'problem'.

    A gridview doesn't have an ItemDataBound event, I used the RowDataBound
    event and it works! But not completely as hoped: the data/buttons in
    the columns behind the 'Invisible' column move 1 column to the left,
    filling the space of the 'invisible' column. That's not what I need,
    this column should be left empty, 'not replaced'...
    That's the only issue left now. Any solution to that?

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    DataRowView drView = (DataRowView)e.Row.DataItem;
    DataRow drRow = drView.Row;

    if (drRow[6].ToString() != "")
    {
    int i = e.Row.RowIndex;
    e.Row.Cells[7].Visible = false;
    }
    }
    }

    Regards,
    Jo
     
    Jo, Apr 7, 2006
    #5
  6. Jo

    Jan Hyde Guest

    "Jo" <>'s wild thoughts were
    released on 7 Apr 2006 00:49:27 -0700 bearing the following
    fruit:

    >Hi,
    >Thanks for your quick reply! You gave me the right hint to continue
    >this 'problem'.
    >
    >A gridview doesn't have an ItemDataBound event, I used the RowDataBound
    >event and it works! But not completely as hoped: the data/buttons in
    >the columns behind the 'Invisible' column move 1 column to the left,
    >filling the space of the 'invisible' column. That's not what I need,
    >this column should be left empty, 'not replaced'...
    >That's the only issue left now. Any solution to that?


    You don't want to remove the cell, only it's contents.

    J

    >protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    > {
    > if (e.Row.RowType == DataControlRowType.DataRow)
    > {
    > DataRowView drView = (DataRowView)e.Row.DataItem;
    > DataRow drRow = drView.Row;
    >
    > if (drRow[6].ToString() != "")
    > {
    > int i = e.Row.RowIndex;
    > e.Row.Cells[7].Visible = false;
    > }
    > }
    > }
    >
    >Regards,
    >Jo



    Jan Hyde (VB MVP)

    --
    Divorce: Going through a change of wife. (Henny Youngman)
     
    Jan Hyde, Apr 7, 2006
    #6
  7. Jo

    Jo Guest

    Hi,

    >
    >You don't want to remove the cell, only it's contents.
    >


    I thought I wasn't removing anything, just hiding
    ("e.Row.Cells[7].Visible = false"),
    still the content of remaining cells in that row is taking the place of
    the content of the 'invisible' cell.

    So, maybe you can be more clear about this?

    Thank you!
    Jo
     
    Jo, Apr 7, 2006
    #7
  8. Jo

    Jan Hyde Guest

    "Jo" <>'s wild thoughts were
    released on 7 Apr 2006 02:30:49 -0700 bearing the following
    fruit:

    >Hi,
    >
    >>
    >>You don't want to remove the cell, only it's contents.
    >>

    >
    >I thought I wasn't removing anything, just hiding
    >("e.Row.Cells[7].Visible = false"),
    >still the content of remaining cells in that row is taking the place of
    >the content of the 'invisible' cell.
    >
    >So, maybe you can be more clear about this?
    >


    'I want a button in that row to be visible, otherwise no
    button should be shown in that row'

    So change the visible propery of the button to false.



    Jan Hyde (VB MVP)

    --
    Could you say that a cannibalistic lion might swallow his pride?
    (Ken Shurget)
     
    Jan Hyde, Apr 7, 2006
    #8
  9. Jo

    Jo Guest

    Hi,

    >
    >So change the visible propery of the button to false.
    >


    First, I didn't know how to do that, but I've tried this code (see
    below), and I got an ArgumentOutOfRangeException!
    I guess that the row which gives the GridViewRowEventArgs haven't been
    created in the GridView at that time, the row before this one does exit
    in de Gridview, the current one not...
    Maybe I have to try to do this in the rowcreated_event???

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)

    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    DataRowView drView = (DataRowView)e.Row.DataItem;
    DataRow drRow = drView.Row;

    if (drRow[6].ToString() != "")
    {
    int i = e.Row.RowIndex;
    //e.Row.Cells[7].Visible = false; REPLACED BY:
    grid.Rows.Controls[7].Visible = false; ////////
    Gives an ArgumentOutOfRangeException!
    }
    }
    }
     
    Jo, Apr 7, 2006
    #9
  10. Jo

    Jo Guest

    I got the same problem in the rowcreated_event...

    And tried "e.Row.Cells.RemoveAt(7);" in stead of
    "e.Row.Cells[7].Visible = false", but that gives (visually) the same
    results
    That's something I can't explain: removing and making invisible isn't
    the same!

    I'm going of to enjoy my weekend. Hopefully someone can give me a
    straight answer and a solution to my 'problem'.

    Enjoy!
    Jo
     
    Jo, Apr 7, 2006
    #10
  11. Jo

    sirfunusa Guest

    It's not Controls[7]. Similar to Cell[7].Controls[0]
     
    sirfunusa, Apr 7, 2006
    #11
  12. Jo

    Jo Guest

    Thanks for your support!
    Now it's working:

    e.Row.Cells[7].Controls[1].Visible = false;

    Thank you!
    Jo
     
    Jo, Apr 10, 2006
    #12
    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. Roy
    Replies:
    0
    Views:
    396
  2. Roy
    Replies:
    8
    Views:
    1,877
    Danny
    Feb 14, 2005
  3. pvong
    Replies:
    4
    Views:
    12,080
    pvong
    Aug 5, 2007
  4. Andy B
    Replies:
    5
    Views:
    627
    Andy B
    May 29, 2008
  5. Julia B
    Replies:
    2
    Views:
    3,967
    Julia B
    Oct 2, 2008
Loading...

Share This Page