|
-
Oct 29th, 2013, 09:47 PM
#1
Thread Starter
Frenzied Member
-
Oct 29th, 2013, 10:00 PM
#2
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.
-
Oct 29th, 2013, 11:14 PM
#3
Thread Starter
Frenzied Member
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.

-
Oct 29th, 2013, 11:33 PM
#4
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:
UPDATE receipts SET description = TRIM(description)
That will remove all leading and trailing spaces as well as any multiple spaces between words.
-
Oct 30th, 2013, 12:48 AM
#5
Thread Starter
Frenzied Member
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()
-
Oct 30th, 2013, 01:35 AM
#6
Re: DataGridView Columns Width Issue
 Originally Posted by toecutter
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.
-
Oct 30th, 2013, 02:27 AM
#7
Thread Starter
Frenzied Member
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
-
Oct 30th, 2013, 03:10 AM
#8
Re: DataGridView Columns Width Issue
 Originally Posted by toecutter
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.
-
Oct 30th, 2013, 04:09 AM
#9
Thread Starter
Frenzied Member
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?
-
Oct 30th, 2013, 06:18 AM
#10
Re: DataGridView Columns Width Issue
 Originally Posted by toecutter
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|