Results 1 to 6 of 6

Thread: Hiding/sizing columns in data grid

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Hiding/sizing columns in data grid

    I have a data grid that is bound to a relationship of two tables in a dataset. The common field in the relationship is classId. When I view the data in the data grid I do not want to see the classId field but if I do not include the field in my select statement I get an error when I try to form the relationship. Is there anyway to hide a field/column in a data grid? ALong this line of questions, can the width of the data grid columns be dynamically set to fit the data in each column.

    I can include code samples if needed.

  2. #2
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Hiding/sizing columns in data grid

    yeah you can set the size of the column width of the datagrid. try to use datagridtablestyle.
    search in this forum and you can find a lot of it.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Hiding/sizing columns in data grid

    try to use datagridtablestyle.
    That got me close but I'm still having problems. Here is what I'm trying:
    VB Code:
    1. Dim tableStyle As New DataGridTableStyle
    2. Dim colStyle As DataGridColumnStyle
    3.  
    4. daClass.Fill(ds, "Classes")
    5. daHull.Fill(ds, "Hulls")
    6. ds.Relations.Add("ClassRel", _
    7.                         ds.Tables("Classes").Columns("classId"), _
    8.                         ds.Tables("Hulls").Columns("classId"))
    9. Me.dgHulls.SetBindings(ds,Classes.ClassRel")
    10. tableStyle.MappingName = "Classes.ClassRel"
    11. Me.dgHulls.TableStyles.Add(tableStyle)
    12. colStyle = tableStyle.GridColumnStyles.Item("classId")
    13. colStyle.Width = 0
    When I try to run this I get an error on colStyle.Width = 0:
    Object reference not set to an instance of an object.
    The debugger shows that colStyle has a value of "Nothing". Can anyone explain what I'm doing wrong?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Hiding/sizing columns in data grid

    After looking at few more examples I've changed my code but now I get a different error (blue squiggle line): Option Strict On disallows implicit conversions from 'System.Windows.Forms.DataGridColumnStyle' to 'System.Windows.Forms.DataGridTextBoxColumn'
    VB Code:
    1. [B]Dim tableStyle As DataGRidTableStyle = New DataGridTableStyle[/B]
    2. [B]Dim colStyle As DataGridTextBoxColumn[/B]
    3.  
    4. daClass.Fill(ds, "Classes")
    5. daHull.Fill(ds, "Hulls")
    6. ds.Relations.Add("ClassRel", _
    7.                         ds.Tables("Classes").Columns("classId"), _
    8.                         ds.Tables("Hulls").Columns("classId"))
    9. Me.dgHulls.SetBindings(ds,Classes.ClassRel")
    10. tableStyle.MappingName = "Classes.ClassRel"
    11. Me.dgHulls.TableStyles.Add(tableStyle)
    12. colStyle = tableStyle.GridColumnStyles.Item("classId")
    13. colStyle.Width = 0
    I can turn off Option Strict and the blue line goes away but then I get the same error in my previous post. What am I missing?

  5. #5
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Hiding/sizing columns in data grid

    try this way.
    VB Code:
    1. dim dgt as new DataGridTableStyle()
    2. dim dgtb as new DataGridTextBoxColumn()
    3.             dgtb.MappingName="classid"
    4.             dgtb.Width=0
    5.             dgt.GridColumnStyles.Add(dgtb)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: Hiding/sizing columns in data grid

    mar_zim

    Thanks for your help. Your method got rid of the errors but the colulmn widths are not changing. Everything works as expected when I use this code in on a form where the table relationship is accomplished using the SQL join command and the data source is assigned using the dataMember property. Maybe one or both of the following statements are complicating things.
    VB Code:
    1. ds.Relations.Add("ClassRel", _
    2.                         ds.Tables("Classes").Columns("classId"), _
    3.                         ds.Tables("Hulls").Columns("classId"))
    4. Me.dgHulls.SetBindings(ds,Classes.ClassRel")
    Any thoughts?

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