问题
please if someone can help :( ,I'm looking at first to combine date and times (ex: ( DatetaskStart ,TimetaskStart) , (DatetaskEnd ,TimetaskEnd)).
And throughout my dategridview I want make a subtraction between combinaision obtained ((date, time) of the beginning of the task ) and ((date,time) of end the task) and finally get the total times in hours of all the tasks obtained.
Dim sql As String = "select id_task, DatetaskStart ,TimetaskStart , DatetaskEnd ,TimetaskEnd from task where id_task = " & Textbox1.Text & " and Datetask Between '" & DateTimePicker1.Text & "' And '" & DateTimePicker2.Text & "';"
command.CommandText = sql
Dim Time_task as Double
Dim total as Double
Dim DateValue As Date
Dim DateValue2 As Date
connection.Open()
Dim ds As New DataSet
Dim SQLAdapter As New MySqlDataAdapter(sql, connStr)
SQLAdapter.Fill(ds, "connectString")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "connectString"
DataGridView1.AutoResizeColumns()
connection.Close()
For Each row As DataGridViewRow In DataGridView1.Rows
Time_task += (Date.TryParse(row.Cells(3).Value + " " + row.Cells(4).Value, DateValue)) - (Date.TryParse(row.Cells(1).Value + " " + row.Cells(2).Value, DateValue2))
Next
total = Time_task
TextBox7.Text = total
回答1:
You need something like this;
Dim TaskEnd As DateTime
Dim TaskStart As DateTime
Dim result As TimeSpan = TaskEnd - TaskStart
MsgBox(result.TotalHours)
VB has a TimeSpan, when every you need to keep track of a duration, use it!
Please next time, try not to drag in any unrelated items. This problem has nothing to do with a datagridview... It has to do with date and time...
来源:https://stackoverflow.com/questions/18078278/manipulate-datagridview