Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > สอบถามเรื่องการคิดเลขจาก datatable ครับ ว่าวิธีแบบนี้จะทำการ loop ยังไง



 

สอบถามเรื่องการคิดเลขจาก datatable ครับ ว่าวิธีแบบนี้จะทำการ loop ยังไง

 



Topic : 131960



โพสกระทู้ ( 156 )
บทความ ( 0 )



สถานะออฟไลน์




สอบถามเรื่องการคิดเลขครับ คือผมได้ query เอาจำนวนใส่ใน datatable ไว้แล้ว แต่ผมจะ loop เพื่อเอามาลบกับข้อมูลใน Datatable ยังไงครับ ตัวอย่าง
Datatable
1.มีจำนวน 35000
2.มีจำนวน 10000
3.มีจำนวน 60000
จำนวนที่ต้องการเอามาลบ
1.มีจำนวน 8000
2.มีจำนวน 15000
3.มีจำนวน 13000
4.มีจำนวน 5000
5.มีจำนวน 5000

โดยที่ดึง datatable จำนวน แรกมา เอามาลบจนกว่าจะเหลือ 0 เช่น
Datatable 1.มีจำนวน 35000
35000 - 8000 = 27000
27000 - 15000 = 12000
12000 - 13000 = -1000
Datatable 2.มีจำนวน 15000
-1000 - 15000 = 14000

ประมาณนี้ครับ ผมจะ loop ยังไงพอจะมีตัวอย่างไหมครับ ตอนนี้ที่ทำลบกันมั่วมาก รบกวนด้วยครับ

Code (VB.NET)
                    Dim drFindqty As DataRow
                    Dim Getcount As Integer
                    Dim sumcount_oh As Integer
                    Dim Countdata As Integer
                    Dim Countoh As Integer
                    Dim Get_qty_oh As Integer
                    Dim Get_qty_oh_copy As Integer
                    If dtdata2.Rows.Count > 0 Then
                        For p As Integer = 0 To dtdata2.Rows.Count - 1
                            Dim Get_item As String = dtdata2.Rows.Item(p).Item("pld_material_2").ToString
                            Dim Get_qty_iss As Integer = Math.Floor(CDbl(dtdata2.Rows(p).Item("pld_Iss_qty"))) 'จำนวนของต้องการ
                            Get_qty_iss = Mid(Get_qty_iss, 2)

                            If dtGetlot.Rows.Count > 0 Then
                                For T As Integer = 0 To dtGetlot.Rows.Count - 1
                                    If dtFindqty.Rows.Count = 1 Then
                                        Get_qty_oh = dtFindqty.Rows(0).Item("Get_sum").ToString
                                    ElseIf dtFindqty.Rows.Count > 1 And dtFindqty.Rows.Count <> 0 Then
                                        Countdata = Countdata + 1
                                        Get_qty_oh = dtFindqty.Rows(Countdata).Item("Get_sum").ToString
                                    ElseIf dtGetlot.Rows.Count > 0 Then
                                        Get_qty_oh = Math.Floor(CDbl(dtGetlot.Rows(Countoh).Item("ld_qty_oh"))).ToString 'จำนวนที่มี
                                    End If
                                        If Get_qty_oh > Get_qty_iss Then
                                            sumcount_oh = Get_qty_oh - Math.Floor(Get_qty_iss)
                                            Getcount = sumcount_oh

                                            Dim Get_ref As String = dtGetlot.Rows.Item(T).Item("ld_ref").ToString()
                                            Dim Get_loc As String = dtGetlot.Rows.Item(T).Item("ld_loc").ToString
                                            Dim Get_lot As String = dtGetlot.Rows.Item(T).Item("ld_lot").ToString
                                            Dim Get_serial As String = dtGetlot.Rows.Item(T).Item("serial").ToString
                                            drFindqty = dtFindqty.NewRow
                                            drFindqty("Get_item") = Get_item
                                            drFindqty("Get_ref") = Get_ref
                                            drFindqty("Get_loc") = Get_loc
                                            drFindqty("Get_lot") = Get_lot
                                            drFindqty("Get_serial") = Get_serial
                                            drFindqty("Get_sum") = sumcount_oh
                                            drFindqty("Get_qty_oh") = Get_qty_oh
                                            drFindqty("Get_Minus") = DBNull.Value
                                            Dim newRow = dtFindqty.NewRow()
                                            newRow.ItemArray = drFindqty.ItemArray
                                            dtFindqty.Rows.Add(newRow)
                                            Exit For
                                        ElseIf Get_qty_oh < Get_qty_iss Then
                                            sumcount_oh = Get_qty_oh - Math.Floor(Get_qty_iss)
                                            Getcount = sumcount_oh
                                            If sumcount_oh <= 0 Then
                                            Countdata = Countdata + 1
                                            Countoh = Countoh + 1
                                            Get_qty_oh_copy = Get_qty_oh
                                                Get_qty_oh = Math.Floor(CDbl(dtGetlot.Rows(Countoh).Item("ld_qty_oh"))).ToString 'จำนวนที่มี
                                                sumcount_oh = Mid(sumcount_oh, 2)
                                                Getcount = Get_qty_oh - sumcount_oh
                                                sumcount_oh = "-" & sumcount_oh
                                            End If

                                            Dim Get_ref As String = dtGetlot.Rows.Item(Countoh).Item("ld_ref").ToString()
                                            Dim Get_loc As String = dtGetlot.Rows.Item(Countoh).Item("ld_loc").ToString
                                            Dim Get_lot As String = dtGetlot.Rows.Item(Countoh).Item("ld_lot").ToString
                                            Dim Get_serial As String = dtGetlot.Rows.Item(Countoh).Item("serial").ToString
                                        drFindqty = dtFindqty.NewRow
                                        If sumcount_oh <= 0 Then
                                            drFindqty("Get_item") = Get_item
                                            drFindqty("Get_ref") = Get_ref
                                            drFindqty("Get_loc") = Get_loc
                                            drFindqty("Get_lot") = Get_lot
                                            drFindqty("Get_serial") = Get_serial
                                            drFindqty("Get_sum") = 0
                                            drFindqty("Get_qty_oh") = Get_qty_oh_copy
                                            drFindqty("Get_Minus") = sumcount_oh
                                            Dim newRow1 = dtFindqty.NewRow()
                                            newRow1.ItemArray = drFindqty.ItemArray
                                            dtFindqty.Rows.Add(newRow1)
                                        End If
                                        drFindqty("Get_item") = Get_item
                                        drFindqty("Get_ref") = Get_ref
                                        drFindqty("Get_loc") = Get_loc
                                        drFindqty("Get_lot") = Get_lot
                                        drFindqty("Get_serial") = Get_serial
                                        If Getcount <= 0 Then
                                            drFindqty("Get_sum") = 0
                                            drFindqty("Get_Minus") = sumcount_oh
                                        Else
                                            drFindqty("Get_sum") = Getcount
                                            drFindqty("Get_Minus") = DBNull.Value
                                        End If
                                        drFindqty("Get_qty_oh") = Get_qty_oh
                                        Dim newRow2 = dtFindqty.NewRow()
                                        newRow2.ItemArray = drFindqty.ItemArray
                                        dtFindqty.Rows.Add(newRow2)


                                        If sumcount_oh <= 0 And dtFindqty.Rows.Item(Countdata).Item("Get_sum").ToString = 0 Then
                                            Dim cutsum As Integer = dtFindqty.Rows.Item(Countdata).Item("Get_sum").ToString
                                            sumcount_oh = Mid(sumcount_oh, 2)
                                            Countdata = Countdata + 1
                                            cutsum = dtFindqty.Rows.Item(Countdata).Item("Get_sum").ToString
                                            Countdata = Countdata - 1
                                            If sumcount_oh > cutsum Then
                                                Countdata = Countdata + 1
                                                sumcount_oh = Get_qty_oh - sumcount_oh
                                                drFindqty("Get_item") = Get_item
                                                drFindqty("Get_ref") = Get_ref
                                                drFindqty("Get_loc") = Get_loc
                                                drFindqty("Get_lot") = Get_lot
                                                drFindqty("Get_serial") = Get_serial
                                                If Getcount <= 0 Then
                                                    drFindqty("Get_sum") = 0
                                                    drFindqty("Get_Minus") = sumcount_oh
                                                    Get_qty_oh = 0
                                                    drFindqty("Get_qty_oh") = Get_qty_oh
                                                Else
                                                    drFindqty("Get_sum") = Getcount
                                                    drFindqty("Get_Minus") = DBNull.Value
                                                End If
                                                drFindqty("Get_qty_oh") = Get_qty_oh
                                                Dim newRow3 = dtFindqty.NewRow()
                                                newRow3.ItemArray = drFindqty.ItemArray
                                                dtFindqty.Rows.Add(newRow3)
                                                If Get_qty_oh = 0 And Getcount < 0 And sumcount_oh < 0 Then
                                                    Countoh = Countoh + 1
                                                    Get_qty_oh = Math.Floor(CDbl(dtGetlot.Rows(Countoh).Item("ld_qty_oh"))).ToString 'จำนวนที่มี
                                                    sumcount_oh = dtFindqty.Rows.Item(Countdata).Item("Get_Minus").ToString
                                                    sumcount_oh = Get_qty_oh - sumcount_oh
                                                    drFindqty("Get_item") = Get_item
                                                    drFindqty("Get_ref") = Get_ref
                                                    drFindqty("Get_loc") = Get_loc
                                                    drFindqty("Get_lot") = Get_lot
                                                    drFindqty("Get_serial") = Get_serial
                                                    If Getcount <= 0 Then
                                                        drFindqty("Get_sum") = 0
                                                        drFindqty("Get_Minus") = sumcount_oh
                                                        Get_qty_oh = 0
                                                        drFindqty("Get_qty_oh") = Get_qty_oh
                                                    Else
                                                        drFindqty("Get_sum") = Getcount
                                                        drFindqty("Get_Minus") = DBNull.Value
                                                    End If
                                                    drFindqty("Get_qty_oh") = Get_qty_oh
                                                    Dim newRow4 = dtFindqty.NewRow()
                                                    newRow4.ItemArray = drFindqty.ItemArray
                                                    dtFindqty.Rows.Add(newRow4)
                                                Else
                                                    Exit For
                                                End If
                                            End If
                                        End If
                                        Exit For
                                        End If
                                Next
                            End If

                        Next
                    End If




Tag : .NET, Oracle, VS 2008 (.NET 3.x)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2018-09-04 11:45:47 By : bankguszo View : 648 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ถ้า Loop เยอะขนาดนี้ทำที่ Stored Procedure ดีกว่าครับ ง่ายกกว่าการเขียน Code เยอะเลยครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-09-04 14:21:59 By : mr.win
 


 

No. 2



โพสกระทู้ ( 156 )
บทความ ( 0 )



สถานะออฟไลน์


ยังไงครับช่วยอธิบายหน่อยครับ ผมไปต่อไม่ได้ครับ มันติดตรงที่ว่า จำนวนที่เป็นเลขค่อมกัน ที่ต้องใช้เลขจาก datatable 2 ตัวอะครับ มันต้องดึง 2 อันพร้อมกัน พอทำได้ loop ไม่จบอีกครับ วนจน error
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-09-05 08:09:16 By : bankguszo
 

 

No. 3



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์


ผลลัพธ์ที่ได้จะมีหน้าตาแบบไหน ?? ครับ

อย่างนี้หรือเปล่า
Datatable 1.จำนวนแรกคือ 35,000
เอา 35,000 ตั้งแล้วเริ่มลบด้วยตัวเลขใน DataTable 2
35000 - 8000 = 27000
27000 - 15000 = 12000
12000 - 13000 = -1000
หากตรวจสอบแล้ว ผลการลบมีค่าน้อยกว่า 0
ผลลัพธ์ที่ได้
แบบที่ 1 : ได้เลข 3 ตัวคือ 8,000 , 15000 , 13000 สำหรับตัวเลข 35,000
แบบที่ 2 : ได้เลข 3 (สามารถลบได้ 3 ครั้ง แล้วได้ค่าน้อยกว่า 0)

ส่วนจำนวนที่ 2 ของ Datatable 1 คือ 10,000
Datatable 2.มีจำนวน 15,000
เอา 15,000 ตั้งแล้วเริ่มลบด้วยตัวเลขใน DataTable2
10,000 - 8,000 = 2,000
2,000 - 15000 = -13000

เป็นแบบนี้เรื่อยๆไป อย่างนี้หรือเปล่า
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-09-05 18:36:27 By : Rashun
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามเรื่องการคิดเลขจาก datatable ครับ ว่าวิธีแบบนี้จะทำการ loop ยังไง
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่