Link to home
Start Free TrialLog in
Avatar of Matt J
Matt J

asked on

VB.Net DataGridView Cell value trimming

I'm writing a program that displays a time stamp or large number in a datagrideview. I'm loading the data using a SQLite engine.

In order to convert the number (time stamp) I need to take only the first 11 characters or numbers from the value.

Is their a way to loop through all cells in a column and trim it down to say 11 characters?
Avatar of Kimputer
Kimputer

        For Each row As DataGridViewRow In DG.Rows
           row.Cells(0).Value = strings.left(row.Cells(0).Value.ToString, 11)
        Next

Open in new window


Just quick example, I think you're smart enough to figure how to adjust to your own code.

This is just the first column, if you need ALL columns, add another loop inside the current one (this example is for the first 4 cols).

For Each row As DataGridViewRow In SSG1.Rows
            For i = 0 To 3 Step 1
                row.Cells(i).Value = Strings.Left(row.Cells(i).Value.ToString, 11)
            Next
        Next

Open in new window



Please note, you're reading the SQL then filling the DG (first operation, what you're already doing), and now you alter the WHOLE DG (2nd operation, using my provided code). Why not input it correctly already in the first run (read value, trim, then put it in the DG)? The bigger the DG, the more inefficient it becomes, maybe even CPU/memory hogging the server/PC.
you should change your query and get first 11 chars

select substring(cast(timestamp as varchar), 1, 11) as timestamp,...

Open in new window

Avatar of Matt J

ASKER

Well right now, I am in the testing phase to see the best way to convert the data. What I want to do is keep the data raw as it is in the history files of web browsers. Therefor, what I am doing is allowing the user to view what is in the SQLite file but they are not able to alter it. I would like to be able to convert it as it is loaded, but I am not a SQL expert by any means.

I have tried the first two codes, and I am receiving an error that it is not set to an object reference.
Obviously I dont' know your DG names, adjust it where it's needed.
Avatar of Matt J

ASKER

User generated image
Avatar of Matt J

ASKER

The column I want to loop through is the Column 5, all cells.
what is your sql
you should change your sql and no additional code is required...
Avatar of Matt J

ASKER

Dim connection As New SQLite.SQLiteConnection("data Source= MY DATA SOURCE")
        Dim adapter As New SQLite.SQLiteDataAdapter("select * from urls", connection)

        Dim table As New DataTable
        Dim allhistoryItems
        Dim URL As String
        Dim Title As String
        connection.Open()[code]

Open in new window

       adapter.Fill(table)
        DataGridView1.DataSource = table
        adapter.Dispose()
        connection.Close()
        connection.Dispose()
        DataGridView1.GetCellCount(DataGridViewElementStates.Selected)[/code]
Avatar of Matt J

ASKER

Dim connection As New SQLite.SQLiteConnection("data Source=C:\Users\" & Environment.UserName & "\AppData\Local\Google\Chrome\User Data\Default\History")
        Dim adapter As New SQLite.SQLiteDataAdapter("select * from urls", connection)

        Dim table As New DataTable
        Dim allhistoryItems
        Dim URL As String
        Dim Title As String
        connection.Open()


        adapter.Fill(table)

        DataGridView1.DataSource = table


        adapter.Dispose()
        connection.Close()
        connection.Dispose()
        DataGridView1.GetCellCount(DataGridViewElementStates.Selected)

Open in new window

change that query as

select u.*, substring(cast(timestamp as varchar), 1, 11) as ts from urls u

Open in new window


and use this new column "ts" instead
* change "timestamp" to your actual column name...
Avatar of Matt J

ASKER

I am sorry but I don't know my way around SQL, are you able to show me in a little bit more detailed explanation?

I have tried your last comment several different ways, this is what I am getting now?User generated image
first you should use
this

select u.* from urls u

Open in new window


then check column names on that line, put a breakpoint on next line
then add watch table and see what columns do you get...
after getting the column name use this query

select u.*, substring(cast(myTSColumn as varchar), 1, 11) as ts from urls u

Open in new window


then in your code use row("ts") instead of row(5)

if you know your column name, just update the query and use it...
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.