Results 1 to 5 of 5

Thread: DataGrid Column Width

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    England
    Posts
    28

    DataGrid Column Width

    Hi,

    I am newbie with regards to VB so please be gentle....

    I am reading in a text file that I split into four fields using string slicing. I am creating a DataTable in code, and populating it with the data from the text file, and then creating a DataSet in code and adding the DataTable to the DataSet, and then binding these to a DataGrid that I have placed on my form at design time.

    It all works wonderfully well, and is as fast as I could ever have wished for. However, I would like to be able to control the individual column widths in the DataGrid, and I can't see a way to do it as the "Preferred Width" Property applies to every column. My fields are something like this:

    DAY.........DATE............TIME...........EVENT
    Wed........01.07.2004.....10.04.09......The Blackbird Flies South

    So I would like to be able to individually control the column widths.

    Thanks.
    On the Level..........

  2. #2
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    This is what I have used.

    ds is my dataset.

    colDescription is a previously declared column that is present in the dataset.

    VB Code:
    1. 'Set the column styles
    2.         Dim t As DataTable
    3.         For Each t In ds.Tables
    4.             Dim myGridTableStyle As DataGridTableStyle = New _
    5.                                                         DataGridTableStyle()
    6.  
    7.             Dim col As DataColumn
    8.             For Each col In ds.Tables("CalcTable").Columns
    9.  
    10.                 Dim c As New DataGridTextBoxColumn()
    11.                 c.NullText = ""
    12.                 c.HeaderText = col.Caption
    13.                 c.MappingName = col.ColumnName
    14.  
    15.                 If col Is colDescription Then
    16.                     c.Width = 150
    17.                 Else
    18.                     c.Width = 75
    19.                 End If
    20.  
    21.                 myGridTableStyle.GridColumnStyles.Add(c)
    22.             Next
    23.  
    24.             myGridTableStyle.MappingName = t.TableName
    25.             DataGrid1.TableStyles.Add(myGridTableStyle)
    26.         Next t
    This world is not my home. I'm just passing through.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    England
    Posts
    28
    You sir are, indeed, THE man!

    That did the trick. Many thanks.
    On the Level..........

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    England
    Posts
    28
    Further to this issue......

    I have my DataTable, and have added the DataTable to the DataSet, and then bound these to a DataGrid that I have placed on my form at design time. Following the excellent help, I have also been able to change the column widths as required.

    Now, I have an array (0 to maximum number of rows in the data grid) that contains a color entry that I want to be able to apply to each line (row) on the data grid. Ideally, I would like to be able to change the foreground and background colour of each row in the data grid based on the stored entry in the colour array.

    Is this possible? I will eventually want to be able to use searches to filter the data that is displayed, and the colours will need to be consistent.

    Many thanks.
    On the Level..........

  5. #5
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Try this link.

    http://www.devx.com/vb2themax/Articl...8/1954?pf=true

    Scroll down to the bottom and see if the example is what you are after. I haven't read through the whole thing myself, but it looks like what you are asking for.
    This world is not my home. I'm just passing through.

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