locked
Hide First column of Data Grid RRS feed

  • Question

  • Hi All:



    I need to hide the first column of Data grid. I tried this code from msdn but dont see the Datamember property.

    DataGridTableStyle ts = new DataGridTableStyle();

    private void hideColumn()
    {
       // Set the DataGridTableStyle.MappingName property
       // to the table in the data source to map to.
       ts.MappingName = dataGrid1.DataMember;

       // Add it to the datagrid's TableStyles collection
       dataGrid1.TableStyles.Add(ts);

       // Hide the first column (index 0)
       dataGrid1.TableStyles[0].GridColumnStyles[0].


    }



    Compact version 3.5, cannot hide the first column of the datagrid or set the width to zero to hide it
    there is no property as such datagrid. cloumn or datagrid.rw.. any help wil be appreciated ?

    Thursday, August 21, 2008 7:56 PM

Answers

  • DataGridTableStyle.MappingName should be set to one of the following based on your DataSource:

     

    1. DataTable.TableName if source is DataTable (or DataView).

    2. String.Empty if source is SqlCeResultSet.

    3. list.GetType().Name for any IList collection, e.g. an array.

     

    You also must set column stiles with proper mapping names (which are column names for DataTable and SqlCeResultSet, property name on a class which represent row in the collection) for all the columns you want to see in the grid. If you don’t want to see a column then simply don’t set a column style for it.

     

    Also remember what mapping names are case sensitive.

     

    Common issues and reasons:

     

    1.       Table style is set but has no effect. Reason: wrong mapping name. Make sure to set mapping name properly.

    2.       No column are shown as soon as table style is set. Reason: table style mapping name is set correctly but no column styles are set or column mapping styles are set incorrectly. Make sure to set column styles with correct mapping names for each column you want to show.

    Thursday, August 21, 2008 8:55 PM

All replies

  • DataGridTableStyle.MappingName should be set to one of the following based on your DataSource:

     

    1. DataTable.TableName if source is DataTable (or DataView).

    2. String.Empty if source is SqlCeResultSet.

    3. list.GetType().Name for any IList collection, e.g. an array.

     

    You also must set column stiles with proper mapping names (which are column names for DataTable and SqlCeResultSet, property name on a class which represent row in the collection) for all the columns you want to see in the grid. If you don’t want to see a column then simply don’t set a column style for it.

     

    Also remember what mapping names are case sensitive.

     

    Common issues and reasons:

     

    1.       Table style is set but has no effect. Reason: wrong mapping name. Make sure to set mapping name properly.

    2.       No column are shown as soon as table style is set. Reason: table style mapping name is set correctly but no column styles are set or column mapping styles are set incorrectly. Make sure to set column styles with correct mapping names for each column you want to show.

    Thursday, August 21, 2008 8:55 PM
  • Hi

    see here may be help to idea for hide the column.

    and this did for .net framework.

    http://www.codeproject.com/KB/webforms/Datagrid_Col_Example.aspx

    thank you
    Friday, August 22, 2008 2:33 AM
  • Hello,

    I have the same problem & haven't been able to find a solution.

    Sound silly, but the columns do not show up so how will I know if I have the correct mapping name?

    Can you please explain the procedure for mapping columns.

    My code is as below:-

    1  
    2         Dim ts As New DataGridTableStyle  
    3  
    4         ' I have tried all of the below.....  
    5           
    6         ts.MappingName = dgPatientsOnWard.DataSource.GetType().Name  
    7         ts.MappingName = DirectCast(dgPatientsOnWard.DataSource, BindingSource).DataMember  
    8         ts.MappingName = bind.GetListName(Nothing)  
    9         ts.MappingName = bind.DataMember  
    10  
    11         dgPatientsOnWard.TableStyles.Add(ts)  
    12         MsgBox("Mapping Name: " & ts.MappingName)  
    13  
    14         (bind is the name of my BindingSource) 

    Any help would be much appreciated.
    Many Thanks
    Wednesday, February 11, 2009 3:21 PM
  • If columns don't show up then yes, you do have correct mapping name for table style but no column styles. Now add column style  for each column you want to see. There’s nothing to it really – just create new styles, set mapping name and add it to table style. Adjust other properties as needed.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Thursday, February 12, 2009 12:14 AM
  • This helped!  You can also just type the name of the class in the TableStyles designer that you put in your BindingSource.  This way you dont have to go into the form designer everytime.
    Wednesday, May 13, 2009 7:47 PM