HOME > .NET Framework > Forum > ทำ CheckBox ใน DataGridView เพื่อให้ Saveได้หลาย Record พอเลือกบาง Record ทำไมมันไม่ Save อันสุดท้ายที่เลือกค่ะ VB.NET (Win App)
ทำ CheckBox ใน DataGridView เพื่อให้ Saveได้หลาย Record พอเลือกบาง Record ทำไมมันไม่ Save อันสุดท้ายที่เลือกค่ะ VB.NET (Win App)
ตอนนี้ทำCheckBoxในDataGridเพื่อให้saveได้หลายRecord พอเลือกบางRecodeทำไมมันไม่saveอันสุดท้ายที่เลือกค่ะ
แต่ถ้าเลือก Select All กด Save มัน Save ให้หมดเลยค่ะั
แต่ถ้าเลือกแค่บาง Recode มันจะไม่ save ข้อมูลชุดสุดท้ายที่เลือกค่ะ
ไม่ทราบว่าเป็นเพราะอะไรค่ะ ขอบคุณค่ะ VBWinApp ค่ะ
อันนี้โค้ดแสดงผลค่ะ
Code (VB.NET)
Private Sub frmTestCheckBox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Showdatas()
End Sub
Private Sub Showdatas()
Conn = New SqlConnection
OpenConn()
Sb.Remove(0, Sb.Length)
Sb.Append("SELECT M_WorkDate,M_ShiftCode,M_ShiftDes,M_TimeIn,M_TimeOut,M_Rate,OT_TypeName,M_SumOT,M_SumSys,M_OTCorrect,M_Status,M_CorrectedUser,M_CorrectedDate,M_EmpCode from V_OTMaster2 ")
Sb.Append(" WHERE M_EmpCode ='4001085'")
Sb.Append(" and M_WorkDate between '08/01/2011' and '08/31/2011'")
'Sb.Append(" and M_Status <> 'A'")
Sb.Append(" order by M_EmpCode,M_WorkDate,M_Rate ")
Dim sqlShift As String
sqlShift = Sb.ToString()
Comm = New SqlCommand
With Comm
.CommandText = sqlShift
.CommandType = CommandType.Text
.Connection = Conn
dr = .ExecuteReader()
If dr.HasRows Then
Dim dtExtshift As DataTable
dtExtshift = New DataTable
dtExtshift.Load(dr)
dtgOvertime.DataSource = dtExtshift
FormatDgvOvertime()
Else
dtgOvertime.DataSource = Nothing
End If
End With
End Sub
Private Sub FormatDgvOvertime()
With dtgOvertime
If .RowCount > 0 Then
Me.dtgOvertime.Columns.Clear()
With dtgOvertime.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
'*** Column (Select) ***'
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.dtgOvertime.Columns.Add(ColumnCheckBox)
dtgOvertime.Columns(0).Frozen = False
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_WorkDate"
ColumnTextBox.HeaderText = "Work Date"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_ShiftCode"
ColumnTextBox.HeaderText = "Shift Code"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_ShiftDes"
ColumnTextBox.HeaderText = "Shift Description"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_TimeIn"
ColumnTextBox.HeaderText = "Time In"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_TimeOut"
ColumnTextBox.HeaderText = "Time Out"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_Rate"
ColumnTextBox.HeaderText = "Rate"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "OT_TypeName"
ColumnTextBox.HeaderText = "OT TypeName"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_SumOT"
ColumnTextBox.HeaderText = "SumOT"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_SumSys"
ColumnTextBox.HeaderText = "SumSys"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_OTCorrect"
ColumnTextBox.HeaderText = "OT Correct"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_Status"
ColumnTextBox.HeaderText = "Status"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_CorrectedUser"
ColumnTextBox.HeaderText = "CorrectedUser"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_CorrectedDate"
ColumnTextBox.HeaderText = "CorrectedDate"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "M_EmpCode"
ColumnTextBox.HeaderText = "EmpCode"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.dtgOvertime.Columns.Add(ColumnTextBox)
.Columns(14).Visible = False
End If
End With
End Sub
ส่วนอันนีั้โค้ดSaveค่ะ Code (VB.NET)
Conn = New SqlConnection
OpenConn()
For i As Integer = 0 To dtgOvertime.Rows.Count - 1
Dim EmpCode As [String] = dtgOvertime.Rows(i).Cells(14).Value.ToString()
Dim SumOT As [String] = dtgOvertime.Rows(i).Cells(9).Value.ToString()
Dim WorkDate As [DateTime] = dtgOvertime.Rows(i).Cells(1).Value.ToString()
Dim Rate As [String] = dtgOvertime.Rows(i).Cells(6).Value.ToString()
Dim EmpCorrect As String = ConnString.UserID
Dim chkCusID As Boolean
chkCusID = New Boolean
chkCusID = dtgOvertime.Rows(i).Cells(0).Value
If chkCusID Then
Tr = Conn.BeginTransaction()
Sb.Remove(0, Sb.Length)
Sb.Append("UPDATE Tb_OTMaster Set M_OTCorrect='" & SumOT & "', M_Status='C', M_CorrectedUser='" & EmpCorrect & "', M_CorrectedDate='" & Now.Date.ToString("M/d/yyyy") & "'")
Sb.Append(" where M_EmpCode='" & EmpCode & "' And M_WorkDate='" & WorkDate.ToString("MM/dd/yyyy") & "' And M_Rate = '" & Rate & "'")
Dim sqlAdd As String = ""
sqlAdd = Sb.ToString()
With Comm
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
.Transaction = Tr
.Parameters.Clear()
.ExecuteNonQuery()
Tr.Commit()
End With
End If
Next
Showdatas()