如何使用绑定列将单元格数据添加到未绑定列而不使用For Each? - VB NET

时间:2016-08-04 07:08:57

标签: mysql vb.net datagridview

Peace Everyone,我目前在使用绑定列在datagridview上添加未绑定列时遇到此类方法的问题。因为我不能使用这种方法而不必使用“for each statement”,我觉得这很烦人。因为我试图通过不在这个网站上询问来解决这个问题。但是,我已经放弃了。所以,这是我的代码。请帮我。

'------------REFRESH DISPLAY------------'
Public Sub RefreshDaysDetails()
    '------------CONNECTION DATABASE------------'
    Dim connectionDaysDetails As New SqlConnection(_1LoginForm.connectionstring)
    '------------QUERY------------'
    sqlDaysDetails = "SELECT Date,CONVERT(varchar(15), TimeIn, 100) AS 'Time In',CONVERT(varchar(15), TimeOut, 100) AS 'Time Out',DATEPART(hour, WorkingHours) AS 'Working Hours'  FROM [3EmployeeAttendance] WHERE TimeOut IS NOT NULL AND TimeIn IS NOT NULL"
    '------------START CONNECTION------------'
    connectionDaysDetails.Open()
    '------------SQL------------'
    sCommandDaysDetails = New SqlCommand(sqlDaysDetails, connectionDaysDetails)
    sAdapterDaysDetails = New SqlDataAdapter(sCommandDaysDetails)
    sBuilderDaysDetails = New SqlCommandBuilder(sAdapterDaysDetails)
    sDsDaysDetails = New DataSet()



    sAdapterDaysDetails.Fill(sDsDaysDetails, "DaysDetails")
    sTableDaysDetails = sDsDaysDetails.Tables("DaysDetails")

    '------------SET OF BINDING SOURCE------------'
    DaysDetailsbindingsource.DataSource = sDsDaysDetails.Tables("DaysDetails")


    '------------CONNECTION DATABASE------------'
    Dim connectionDaysDetailsTwo As New SqlConnection(_1LoginForm.connectionstring)
    '------------QUERY------------'
    sqlDaysDetailsTwo = "SELECT DATEPART(hour,TimeIn) as 'TotalWorkingHoursTimeIn',DATEPART(hour,TimeOut) as 'TotalWorkingHoursTimeOut'  FROM [3EmployeeAttendance] WHERE TimeOut IS NOT NULL AND TimeIn IS NOT NULL "

    '------------START CONNECTION------------'
    connectionDaysDetailsTwo.Open()

    '------------SQL------------'
    sCommandDaysDetailsTwo = New SqlCommand(sqlDaysDetailsTwo, connectionDaysDetailsTwo)
    sAdapterDaysDetailsTwo = New SqlDataAdapter(sCommandDaysDetailsTwo)
    sBuilderDaysDetailsTwo = New SqlCommandBuilder(sAdapterDaysDetailsTwo)


    '------------CODES TO DISPLAY DATA TO TEXTBOXES------------'
    sCommandDaysDetailsTwo.Prepare()
    sDataReaderDaysDetailsTwo = sCommandDaysDetailsTwo.ExecuteReader()

    For Each row In sDataReaderDaysDetailsTwo
        TotalWorkingHoursTimeIn = sDataReaderDaysDetailsTwo.Item("TotalWorkingHoursTimeIn")
        TotalWorkingHoursTimeOut = sDataReaderDaysDetailsTwo.Item("TotalWorkingHoursTimeOut")
        While TotalWorkingHoursTimeIn < TotalWorkingHoursTimeOut
            TotalWorkingHours = TotalWorkingHours + 1
            TotalWorkingHoursTimeIn = TotalWorkingHoursTimeIn + 1
        End While
    Next


    '------------BINDS DGV TO THE EXISTING DATA------------'
    DGVViewDaysDetailsForm.DataSource = DaysDetailsbindingsource

    '------------CREATES DATA GRID VIEW TEXBOX COLUMN AND ADDS IT TO DATA GRID VIEW------------'
    Dim TotalWorkHoursextraColumn As New DataGridViewTextBoxColumn
    With TotalWorkHoursextraColumn
        .Name = "TotalWorkHoursPerDay"
        .HeaderText = "Total Work Hours"
        .ReadOnly = True
    End With
    DGVViewDaysDetailsForm.Columns.Add(TotalWorkHoursextraColumn)


    '------------LOOP DGV TO CALCULATE TOTAL HOURS PER ROW------------'
    For Each row As DataGridViewRow In DGVViewDaysDetailsForm.Rows
        row.Cells("TotalWorkHoursPerDay").Value = CInt(TotalWorkingHours)
    Next

    '------------CLOSE CONNECTION------------'
    connectionDaysDetailsTwo.Close()



    '------------CLOSE CONNECTION------------'
    connectionDaysDetails.Close()
    '------------DATA BINDINGS------------'
    DGVViewDaysDetailsForm.DataSource = DaysDetailsbindingsource
    BNViewDaysDetailsForm.BindingSource = DaysDetailsbindingsource


    '------------DATA GRID VIEW SELECTION------------'
    DGVViewDaysDetailsForm.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    DGVViewDaysDetailsForm.MultiSelect = False



End Sub

这是上面代码的图像输出:

ImageOutput

如您所见,工作时间列可以,因为它来自绑定列。但请查看名为“总工作时间”的未绑定列(我刚将其命名为“总工作时间”。因为我真正需要的是每行的小时数分解。),结果是相同的。全部15,因为我无法正确执行我的代码,同时在每个语句中包含此代码。

 '------------LOOP DGV TO CALCULATE TOTAL HOURS PER ROW------------'
For Each row As DataGridViewRow In DGVViewDaysDetailsForm.Rows
    row.Cells("TotalWorkHoursPerDay").Value = CInt(TotalWorkingHours)
Next

有什么方法可以关联row.Cells(“TotalWorkHoursPerDay”)。我的DGVViewDaysDetailsForm中的值= CInt(TotalWorkingHours)?

因为每次我删除for each并且只保留row.Cells(“TotalWorkHoursPerDay”)。Value = CInt(TotalWorkingHours)

我收到的错误代码如下: 找不到参数列名称 无法添加行,因为datagridview已绑定 列不存在

1 个答案:

答案 0 :(得分:0)

Private Sub dgv_CellValidating(sender As Object, e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgv.CellValidating    

if e.RowIndex > -1 then
Dim dteValue1 as Date = DateTime.Parse(dgv.item("TotalWorkingHoursTimeIn", e.RowIndex).Value)
Dim dteValue2 as Date = DateTime.Parse(dgv.item("TotalWorkingHoursTimeOut", e.RowIndex).Value)
 dgv.Rows("TotalWorkHoursPerDay", e.RowIndex).Value = DateDiff(DateInterval.Hour, dteValue1, dteValue2).ToString()
End if

在单元格验证事件中尝试上述代码。希望这有效