CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2002
    Posts
    47

    How to adjust the column width in a datagrid

    Hi !

    I'm able to set the differetns column's name of a datadrid but not the width. How can i do that?

    Here's my code how i adjust the column name:

    Dim adoDRCurrentRow As DataRow
    Dim adoDCEffetsIdentifies As New DataColumn

    adoDCEffetsIdentifies = CType(_adodtCheque, DataTable).Columns(0)
    adoDCEffetsIdentifies.ColumnName = "DATE COMPTABLE"

    adoDCEffetsIdentifies = CType(_adodtCheque, DataTable).Columns(1)
    adoDCEffetsIdentifies.ColumnName = "NO REF"

    Thanks
    Alexandre

  2. #2
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    You got a column out the datatable and not of the datagrid itself.
    To change the width of a column in a datagrid, you must add a tablestyle to the grid.

    Try the following example:
    Add a DataGrid and a Button on a Form and copy the following code to it.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dt As New DataTable("Test")

    Dim co As DataColumn
    co = New DataColumn("col1", GetType(String))
    dt.Columns.Add(co)
    co = New DataColumn("col2", GetType(Integer))
    dt.Columns.Add(co)
    co = New DataColumn("col3", GetType(Date))
    dt.Columns.Add(co)

    Me.DataGrid1.DataSource = dt

    Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
    myGridTableStyle.MappingName = dt.TableName

    DataGrid1.TableStyles.Add(myGridTableStyle)

    myGridTableStyle = DataGrid1.TableStyles.Item("Test")

    Dim myColumnStyle As DataGridColumnStyle = myGridTableStyle.GridColumnStyles.Item("col2")
    myColumnStyle.Width = 200
    End Sub

    I hope this helps,
    Danny

  3. #3
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347
    Danny,

    I copied you example and it ran fine. I'm new at this. I already have a datagrid with columns. How might I apply this to my datagrid and columns.

    Some parameters I have are
    datagrid1
    table name=surname1 with columns:Name,Event,Date,Name2,Name3. They are all text.

    I don't know what else you might need to know. I would like to be able to give the 5 columns different widths.

    I appreciate your help.
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  4. #4
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347
    This is my actual code:

    ' Creating connection and command sting
    Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=" & DBFileName
    Dim sqlStr As String = "SELECT NAME1_INDEX,EVENT,SEX,DATE,COUNTY,NAME2,NAME3_COMMENTS
    ' Create connection object
    Dim conn As OleDbConnection = New OleDbConnection(conStr)
    ' Create data adapter object
    Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
    ' Create a dataset object and fill with data using data adapter's Fill method
    Dim ds As DataSet = New DataSet
    da.Fill(ds, DBTableName)
    ' Attach dataset's DefaultView to the datagrid control
    DataGrid1.DataSource = ds.DefaultViewManager
    DataGrid1.DataMember = DBTableName

    I can't believe I'm actually so far into this as I am! I can actually do searches and everything. WOW.

    If I can get how to set the width's on the columns I'm almost there.

    Appreciate your help.
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  5. #5
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    Hi Cathy,

    I add some extra code to yours and hope it will help you.

    Danny

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Creating connection and command sting
            Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=" & DBFileName
            Dim sqlStr As String = "SELECT NAME1_INDEX,EVENT,SEX,DATE,COUNTY,NAME2,NAME3_COMMENTS"
    
            ' Create connection object
            Dim conn As OleDbConnection = New OleDbConnection(conStr)
            ' Create data adapter object
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
            ' Create a dataset object and fill with data using data adapter's Fill method
            Dim ds As DataSet = New DataSet
            da.Fill(ds, DBTableName)
            ' Attach dataset's DefaultView to the datagrid control
            DataGrid1.DataSource = ds.DefaultViewManager
            DataGrid1.DataMember = DBTableName
    
    
            ' Set Column width
            Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
            myGridTableStyle.MappingName = DBTableName
    
            DataGrid1.TableStyles.Add(myGridTableStyle)
    
            myGridTableStyle = DataGrid1.TableStyles.Item(DBTableName)
    
            Dim myColumnStyle As DataGridColumnStyle
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME1_INDEX")
            myColumnStyle.Width = 50
    
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("EVENT")
            myColumnStyle.Width = 150
    
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("SEX")
            myColumnStyle.Width = 200
    
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("COUNTY")
            myColumnStyle.Width = 100
    
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME2")
            myColumnStyle.Width = 100
    
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME3_COMMENTS")
            myColumnStyle.Width = 300
    
        End Sub

  6. #6
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347
    It worked fine. But now I have my rowheadervisible (which I don't want). It's also reset my rowheight to something. How do I adjust the rowheight also.

    Thanks for responding
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  7. #7
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347
    I got the RowHeader fixed

    myGridTableStyle.RowHeadersVisible = False


    Used this for rowheight but it didn't work

    myGridTableStyle.PreferredRowHeight = 80

    Cathy
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  8. #8
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    Setting the PreferredRowHeight to 80 does work for me, when I put it in my previous example.

    Danny

    Code:
            ' Set Column width
            Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
            myGridTableStyle.MappingName = DBTableName
            myGridTableStyle.RowHeadersVisible = False
            myGridTableStyle.PreferredRowHeight = 80
    
            DataGrid1.TableStyles.Add(myGridTableStyle)
    
            myGridTableStyle = DataGrid1.TableStyles.Item(DBTableName)
    
            Dim myColumnStyle As DataGridColumnStyle
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME1_INDEX")
            myColumnStyle.Width = 50
    
            ...ect

  9. #9
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347
    It now works. I had inserted that line in the wrong place.

    My form has about 32 rows of information. The information in the cells seem to be at the left botton. Question is how do I set the font and the position of the data within the cells.

    Also, (null) values are displayed for empty cells. How do I rid the cell of the (null) and just have the cell blank.

    I really appreciate your help. I'm not real good at programming but if you ever need to look for oil on your property, I might be able to handle that.
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

  10. #10
    Join Date
    Jan 2003
    Location
    Amsterdam, Netherlands
    Posts
    97
    Getting rid of the null is easy, just add the following line to all your columns.
    - myColumnStyle.NullText = ""

    You only can adjust the horizontal alignment, by adding the following.
    - myColumnStyle.Alignment = HorizontalAlignment.Center

    To set the Font, you must set it for the entire grid.
    - DataGrid1.Font = New Font("Courier", 15)
    or for the headers.
    - myGridTableStyle.HeaderFont = New Font("Ariel", 10)

    Hope this helps also
    Danny
    (I'll appreciate your offer, maybe I'll keep you to it some day )

    Code:
            ' Set Column width
            Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
            myGridTableStyle.MappingName = DBTableName
            myGridTableStyle.RowHeadersVisible = False
            myGridTableStyle.PreferredRowHeight = 80
            myGridTableStyle.HeaderFont = New Font("Ariel", 10)
    
            DataGrid1.Font = New Font("Courier", 15)
            DataGrid1.TableStyles.Add(myGridTableStyle)
    
            myGridTableStyle = DataGrid1.TableStyles.Item(DBTableName)
    
            Dim myColumnStyle As DataGridColumnStyle
            myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME1_INDEX")
            myColumnStyle.Width = 50
            myColumnStyle.Alignment = HorizontalAlignment.Center
            myColumnStyle.NullText = ""

  11. #11
    Join Date
    Jan 2004
    Location
    TX, USA
    Posts
    347
    I meant something else rather than
    myColumnStyle.Alignment = HorizontalAlignment.Center

    Actually I guess I'm looking for VerticalAlignment.Center within the cell. I didn't see it.

    My grid is pretty tight. Ariel 7 with height of 7 or as tight as I can get it. The data in the cell itself seems to be assigned bottom.left.

    Is there some way to vertical center data in the cell?

    The big question. Does one have to have a memory of all the available options. I have a Sam's do it all in 21 days. Is there a better reference book for VB.net?

    I also find it hard to navigate in the MSDN library.

    Thanks
    Cathy
    Jan 2004 - NEWBIE to VB6
    Any and all help appreciated

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

Featured