7 Replies - 2384 Views - Last Post: 23 May 2013 - 10:22 AM Rate Topic: -----

#1 skilly2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 01-March 13

Apply a function to datagridview column every time it's filled

Posted 22 May 2013 - 09:08 PM

I have a datagridview that is filled using a datatable as a binding source. It is filled from an SQL Query. It returns multiple rows, with two columns. The first column is a description. The second column is total number of seconds. What I'd like to do is format the total number of seconds to a format of hh:mm:ss to display in the datagridview. I've written a function to return a string in that format, shown below. However, I don't know how to apply this function to every row, every time the datatable is filled. Do I apply this function to the datatable, or to the datagridview? And how do I do it? Also, does it matter that the datagridview is bound to the datatable (I'm not editing the data, it's just for display).

Private Function SecondsToTotalTime(seconds As Int64) As String
        ts = New TimeSpan(0, 0, Convert.ToInt32(seconds))
        Dim s As String = String.Format("{0:00}:{1:00}:{2:00}", ts.Hours, ts.Minutes, ts.Seconds)
        ts = Nothing
        Return s
    End Function


Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Apply a function to datagridview column every time it's filled

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,099
  • Joined: 02-July 08

Re: Apply a function to datagridview column every time it's filled

Posted 22 May 2013 - 09:40 PM

Look into the CellFormatting event. Also ts.ToString does the same format that you were using String.Format to do.
Was This Post Helpful? 0
  • +
  • -

#3 skilly2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 01-March 13

Re: Apply a function to datagridview column every time it's filled

Posted 23 May 2013 - 07:57 AM

View Post_HAWK_, on 22 May 2013 - 09:40 PM, said:

Look into the CellFormatting event. Also ts.ToString does the same format that you were using String.Format to do.


Thanks for the link. I'm still having a little trouble. I'm almost there. Here's what I have for my CellFormatting Event:

Private Sub dgvTotalIndirectSummary_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvTotalIndirectSummary.CellFormatting
        
        If Me.dgvTotalIndirectSummary.Columns(e.ColumnIndex).Name = "colTotalTime" Then
            e.Value = SecondsToTotalTime(e.RowIndex) 'How do I get the value from the same row, but 
                                                     'column "colTotalSeconds"?
        End If

    End Sub


For e.Value, what I'm trying to do is take the value in a hidden column of the same row ("colTotalSeconds"), and format that using my SecondsToTotalTime function. By the way, I did change my SecondsToTotalTime function to use timespan.ToString. Thanks for that info!
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,099
  • Joined: 02-July 08

Re: Apply a function to datagridview column every time it's filled

Posted 23 May 2013 - 08:05 AM

Hidden columns still have an index - don't they?
Was This Post Helpful? 0
  • +
  • -

#5 Linesofcode  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 62
  • Joined: 23-May 13

Re: Apply a function to datagridview column every time it's filled

Posted 23 May 2013 - 08:19 AM

I didn't understand very well, but if you want to take a valor from each row in specific cell you just have to know the index of the cell/column.
 dgvTotalIndirectSummary.Rows(e.RowIndex).Cells(2).Value ' for example.


I would love to have a button to edit my posts on DIC..
where I say "valor" it's value.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Apply a function to datagridview column every time it's filled

Posted 23 May 2013 - 09:39 AM

View PostLinesofcode, on 23 May 2013 - 11:19 AM, said:

I would love to have a button to edit my posts on DIC..
where I say "valor" it's value.



You're almost there ;) I think you have to have a certain number of posts before they let you edit. For some reason, I think it's 15.

EDIT:
Well, 16 it appears see this

This post has been edited by CharlieMay: 23 May 2013 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 Linesofcode  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 62
  • Joined: 23-May 13

Re: Apply a function to datagridview column every time it's filled

Posted 23 May 2013 - 09:54 AM

Hi..yes, I was a little upset, so I google about "edit post on DIC", and I found that page too :D
Was This Post Helpful? 0
  • +
  • -

#8 skilly2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 01-March 13

Re: Apply a function to datagridview column every time it's filled

Posted 23 May 2013 - 10:22 AM

View PostLinesofcode, on 23 May 2013 - 08:19 AM, said:

I didn't understand very well, but if you want to take a valor from each row in specific cell you just have to know the index of the cell/column.
 dgvTotalIndirectSummary.Rows(e.RowIndex).Cells(2).Value ' for example.


I would love to have a button to edit my posts on DIC..
where I say "valor" it's value.


Perfect, that's what I was looking for. I almost had it, but I wasn't putting "dgvTotalInderectSummary.Rows." before the (e.RowIndex) part of my code.

Thanks very much for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1