error msg : Index was out of range. Must be non-negative and less than the size of the collection.
ปัญหา : ขณะที่ผูก datasource เข้ากับ datagridview จะมีเรียกอีเวนต์ _RowEnter ทำงานอัตโนมัติ แต่เรายังไม่ได้คลิกเลือกใน datagridview หรือไม่เกิดการ SelectedRows() ดังนั้นค่า index ที่ใน SelectedRow(0) ที่เรากำหนดในอีเวนต์ _RowEnter จึงอ้างอิงผิดพลาด
สาเหตุ : เกิดภายใต้อีเวนต์ DataGridView_RowEnter ซึ่งเราได้กำหนดให้มีการทำงานดังนี้
***SelectedRow(0) <--- ขณะที่เรายังไม่ได้เลือกจริงๆ ใน datagridview
-----------------------------------------------------------------------------------------
Private Sub DataGridView_RowEnter(....)
txtPileID.Text = dgvPile.SelectedRows(0).Cells("colCode").Value.ToString
End Sub
-----------------------------------------------------------------------------------------
แก้ไข : ภายในอีเวนต์ DataGridView_RowEnter ให้ตรวจสอบก่อนว่าได้เกิดการเลือก Row ใน DataGridView หรือยัง??
-----------------------------------------------------------------------------------------
Private Sub DataGridView_RowEnter(....)
If Not DataGridView.SelectedRows.Count = 0 Then
txtPileID.Text = dgvPile.SelectedRows(0).Cells("colCode").Value.ToString
Else
Exit Sub
End If
End Sub
-----------------------------------------------------------------------------------------
วันพฤหัสบดีที่ 26 มีนาคม พ.ศ. 2552
Index was out of range. Must be non-negative and less than the size of the collection
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
0 ความคิดเห็น:
แสดงความคิดเห็น