Hiding DataGrid Columns when AutoGenerateColumns="true"

Discussion in 'ASP .Net Datagrid Control' started by Jerad Rose, Mar 13, 2007.

  1. Jerad Rose

    Jerad Rose Guest

    I am working with a DataGrid (ASP.NET 2.0) that is set to
    AutoGenerateColumns="true". It is being populated from a DataSet. There is
    a column that I want to hide from the display, but I want to leave it in the
    DataSet so that it can be referenced. However, all attempts that I've tried
    are failing.

    The main method I see being used is this:

    myColumn.ColumnMapping = MappingType.Hidden

    However, this isn't working -- the column remains visible.

    If I do this:

    myDataTable.Columns.Remove("MyColumn")

    Then the column is hidden, but of course, this removes it from the dataset.

    Of course, I can't hide the column after the DataGrid is bound, because the
    Columns collection isn't populated when using AutoGenerateColumns="true"
    (the reason for this still baffles me, but I've given up on figuring that
    one out).

    I'm aware that I could hide each individual cell on ItemDataBound, but this
    requires referencing the cell through a numeric index, which is a poor
    practice, IMO (what if the order of the columns changes?).

    Here is the full code block:

    Dim myDataSet As DataSet = GetDataSet()

    myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
    MappingType.Hidden

    DataGrid1.DataSource = myDataTable
    DataGrid1.DataBind()

    Is there not a way to hide a column for a DataGrid that is using
    AutoGenerateColumns="true", by referencing the column by name?

    Thanks in advance for your help.

    Jerad
     
    Jerad Rose, Mar 13, 2007
    #1
    1. Advertisements

  2. Eliyahu Goldin, Mar 13, 2007
    #2
    1. Advertisements

  3. Jerad Rose

    Jerad Rose Guest

    Thanks for the response.

    Unfortunately, I don't see how I can use this event to hide a column. This
    event is fired when creating row items, not columns (it passes
    DataGridItemEventArgs, same as the ItemDataBound event). I still do not
    seem to have access to any sort of collection or object representing the
    column I want to hide.

    Can you elaborate on how I can achieve this through this event?

    Thanks again.

    Jerad

     
    Jerad Rose, Mar 14, 2007
    #3
  4. In this event you should be able to locate the cell belonging to the column
    you want to hide. You can hide the column by hiding individual cells.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


     
    Eliyahu Goldin, Mar 14, 2007
    #4
  5. Jerad Rose

    Jerad Rose Guest

    Thanks again for your response, Eliyahu.

    I mentioned this in my origninal post, but I prefer not to use this method,
    as it requires that I use an ordinal reference and I feel this is a poor
    practice, since it depends on the column order not changing. I would like
    to find a solution that allows me to reference the column by name, and then
    hide it.

    Is this not possible with my situation?

    Thanks.
    Jerad

     
    Jerad Rose, Mar 14, 2007
    #5
  6. There is an easily solution to this. In the same ItemCreated event detect
    the Header item and locate in it's Cells collection the cell with the text
    matching your column name. Remember the order of this cell and use it as an
    index for data items.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


     
    Eliyahu Goldin, Mar 14, 2007
    #6
  7. I don't know. There must be a reason for this. MS does care of the
    developers and if a feature is not there there should be a reason.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


     
    Eliyahu Goldin, Mar 14, 2007
    #7
  8. Jerad Rose

    Jerad Rose Guest

    Thansk again, Eliyahu.

    This sounds like it might work as a workaround, but it's definitely not a
    very elegant approach. So it sounds like there is no built-in method that
    ..NET supports? Is there a reason why this ability isn't there? Is it an
    oversight, or is it a bug?

    For example, to me, it would make much more sense if either:

    a) the DataGrid Columns collection *was* populated with autogenerated
    columns, instead of being completely empty
    b) setting the ColumnMapping to Hidden *did* work

    Is there any logical explanation as to why neither of these is the case?

    Thanks again.

    Jerad

     
    Jerad Rose, Mar 14, 2007
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.