Results 1 to 6 of 6

Thread: Hiding a column in Datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Location
    Plantation, FL (USA)
    Posts
    66

    Hiding a column in Datagrid

    Hi, I am using the following technique to hide a column in a datagrid. It works great, however, I am not able to get the item value anymore by calling

    DataGrid1.Item(DataGrid1.CurrentRowIndex(), 0)

    Is there a way to hide a column using this method and still get the value from the datagrid?
    I do not want to use a Datagrid Table Style

    VB Code:
    1. Dim cn As New SqlConnection("Data Source=MyServer;User Id=MyUser;Password=MyPassword;Initial Catalog=Northwind")
    2. cn.Open()
    3. Dim da As New SqlDataAdapter("Select * From Products", cn)
    4. Dim ds As New DataSet()
    5. da.FillSchema(ds, SchemaType.Source, "Products")
    6. da.Fill(ds, "Products")
    7. ' Next line hides column.
    8. ds.Tables("Products").Columns("QuantityPerUnit").ColumnMapping = MappingType.Hidden
    9. DataGrid1.SetDataBinding(ds, "Products")

    Thanks in advance
    Diego
    Last edited by dterza01; Jun 8th, 2004 at 01:30 AM.

  2. #2
    Member
    Join Date
    Jul 2003
    Posts
    41
    You can also use your DataSet's ColumnMapping property to hide a column.


    Imports System.Data.OleDb
    '
    ' Retrieve "Customer" data from database.
    '
    Dim strConnection = "Provider=..."
    Dim strSQL = "SELECT * FROM Customers"
    Dim conn As New OleDbConnection(strConnection)
    Dim DA As New OleDbDataAdapter(strSQL,conn)
    Dim DS As New DataSet()
    DA.Fill(DS, "Customers")
    '
    ' Hide the column.
    '
    DS.Tables("Customers").Columns("LastName").ColumnMapping = MappingType.Hidden
    DataGrid.DataSource = DS.Tables("Customers")

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Location
    Plantation, FL (USA)
    Posts
    66
    Am I missing something? That is exactly what I did and explained above.

    Please elaborate.

    The problem is not how to hide the column but how to access the information from the hidden column on the datagrid

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    You could set the width to 0.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Location
    Plantation, FL (USA)
    Posts
    66
    Thank you but that will involve defining a DataGridTableStyle and that is exactly what I want to aviod (see code below).

    Alternatively, I can get the value from the Dataset by using

    DS.Tables("tblSQL").Rows(grdObs.CurrentRowIndex())("bObsNumber")

    but if the user changes the sorting order on the datagrid this method does not work.


    VB Code:
    1. Dim aGridTableStyle As New DataGridTableStyle
    2.         aGridTableStyle.MappingName = "tblSQL"
    3.  
    4.         Dim aCol1 As New DataGridTextBoxColumn
    5.         Dim aCol2 As New DataGridTextBoxColumn
    6.         Dim aCol3 As New DataGridTextBoxColumn
    7.         Dim aCol4 As New DataGridTextBoxColumn
    8.         Dim aCol5 As New DataGridTextBoxColumn
    9.  
    10.         With aCol1
    11.             .MappingName = "bObsNumber"
    12.             .Width = 0
    13.         End With
    14.         With aCol2
    15.             .MappingName = "SupNumber"
    16.             .Width = 0
    17.         End With
    18.         With aCol3
    19.             .MappingName = "Invoice"
    20.         End With
    21.         With aCol4
    22.             .MappingName = "Date"
    23.         End With
    24.         With aCol5
    25.             .MappingName = "Time"
    26.         End With
    27.  
    28.  
    29.         With aGridTableStyle.GridColumnStyles
    30.             .Add(aCol3)
    31.             .Add(aCol4)
    32.             .Add(aCol5)
    33.             .Add(aCol1)
    34.             .Add(aCol2)
    35.         End With
    36.         '
    37.         ' Add the GridColumnStyles to the aGridTableStyle.
    38.         '
    39.         grdObs.TableStyles.Add(aGridTableStyle)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Location
    Plantation, FL (USA)
    Posts
    66
    Anybody??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width