Results 1 to 10 of 10

Thread: [RESOLVED] DataGridView Columns Width Issue

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] DataGridView Columns Width Issue

    2 Tables with almost exact same properties (column widths)

    2 TableAdaptors with almost exact same properties

    2 DataGridView with exact same properties yet one widens the width of the colomn to the Max length size of either the Tables or the TableAdaptors so it seems.






    I have tried all the MSDN examples.

    Code:
     ReceiptsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
            ReceiptsDataGridView.AutoResizeColumn(2, DataGridViewAutoSizeColumnMode.ColumnHeader)
            ReceiptsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
            ReceiptsDataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
            ReceiptsDataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)
            ReceiptsDataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
            ReceiptsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
            ReceiptsDataGridView.AutoResizeRow(2, DataGridViewAutoSizeRowMode.AllCellsExceptHeader)
            ReceiptsDataGridView.AutoResizeColumns()
            ReceiptsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DataGridView Columns Width Issue

    If this is a SQL Server database then it may be that one column is type char or nchar while the other column is type varchar or nvarchar. char and nchar are fixed-width, so they will pad their contents with spaces, which would account for what you're seeing. Many other databases would have similar data types.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: DataGridView Columns Width Issue

    You were correct in that the data types were different but i have corrected that and the grids are still behaving differently.




  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DataGridView Columns Width Issue

    Changing the data type of a char or nchar column to varchar or nvarchar is not going to change the data that's already in there. When you insert data into a fixed-width text column, the database pads the data to the appropriate width and that is the data that's stored. When you retrieve the data you retrieve the spaces because the spaces are part of the data. When you change the data type the spaces remain because the spaces are part of the data. If you want to remove the spaces from all the data in the column then you'll have to execute an appropriate SQL statement, e.g.
    sql Code:
    1. UPDATE receipts SET description = TRIM(description)
    That will remove all leading and trailing spaces as well as any multiple spaces between words.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: DataGridView Columns Width Issue

    TRIM is apparently not a SQL command so i found this which runs but doest Trim, also tried "LTRIM"

    Code:
          Dim connectionString As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\expressAccountsDB.mdf;Integrated Security=True;User Instance=True")
            Dim myCommand As New SqlCommand
            Try
                connectionString.Open()
                myCommand = New SqlCommand("UPDATE receipts SET description = RTRIM(description)")
                'myCommand = New SqlCommand("UPDATE receipts SET description = LTRIM(description)")
                myCommand.Connection = connectionString
                myCommand.ExecuteNonQuery()
                MsgBox("trimmed successfully")
            Catch ex As Exception
                MsgBox(ex.Message())
            End Try
            connectionString.Close()

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DataGridView Columns Width Issue

    Quote Originally Posted by toecutter View Post
    TRIM is apparently not a SQL command so i found this which runs but doest Trim, also tried "LTRIM"

    Code:
          Dim connectionString As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\expressAccountsDB.mdf;Integrated Security=True;User Instance=True")
            Dim myCommand As New SqlCommand
            Try
                connectionString.Open()
                myCommand = New SqlCommand("UPDATE receipts SET description = RTRIM(description)")
                'myCommand = New SqlCommand("UPDATE receipts SET description = LTRIM(description)")
                myCommand.Connection = connectionString
                myCommand.ExecuteNonQuery()
                MsgBox("trimmed successfully")
            Catch ex As Exception
                MsgBox(ex.Message())
            End Try
            connectionString.Close()
    Oops! I looked at the wrong documentation. RTRIM is "right trim" and LTRIM is "left trim". They are equivalent to String.TrimEnd and String.TrimStart in VB. SQL Server pads the end of the text with spaces so you need to trim the end, i.e. the righthand side of the text, so RTRIM is the appropriate function.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: DataGridView Columns Width Issue

    hmmm, deleted the table adaptor and made a new one and all good, so dont need the sql command.

    thanks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DataGridView Columns Width Issue

    Quote Originally Posted by toecutter View Post
    hmmm, deleted the table adaptor and made a new one and all good, so dont need the sql command.

    thanks
    I didn't mean that you need that in your application. I meant that you needed to run it once against the database to remove all the padding.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: DataGridView Columns Width Issue

    Oh, so you would do this in the Server Explorer window by right clicking the Table, selecting New Query and entering that cmd into the Query wizard?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DataGridView Columns Width Issue

    Quote Originally Posted by toecutter View Post
    Oh, so you would do this in the Server Explorer window by right clicking the Table, selecting New Query and entering that cmd into the Query wizard?
    Precisely. You only need to update the data once. Once the padding is removed it's not going to come back again because you changed the data type of the column.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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