 |
|
มีตารางโชว์จำนวนสินค้ากับมีตารางการขายสินค้าด้วย คือถ้าจำนวนสินค้าเท่ากับ0 อยากจะให้ไม่สามารถบันทึกสินค้าได้ ควรทำอย่างไรดี |
|
 |
|
|
 |
 |
|
คือตารางโชว์จำนวนสินค้าจะต้องเลือกradiobutton
โค้ดในradiobutton
Code (VB.NET)
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
Try
If Not con.State = ConnectionState.Open Then
con.Open()
End If
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
da = New OleDbDataAdapter("SELECT * FROM Stock1", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
DataGridView1.Columns(0).Visible = True
DataGridView1.Columns(1).Visible = True
DataGridView1.Columns(2).Visible = True
DataGridView1.Columns(3).Visible = False
DataGridView1.Columns(4).Visible = False
DataGridView1.Columns(5).Visible = True
DataGridView1.Columns(6).Visible = False
DataGridView1.Columns(7).Visible = False
DataGridView1.Columns.Item(0).Width = 50
DataGridView1.Columns.Item(1).Width = 100
DataGridView1.Columns.Item(2).Width = 80
DataGridView1.Columns.Item(5).Width = 100
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
End Try
End Sub
โค้ดปุ่มบันทึก
Code (VB.NET)
Try
Dim cmd As New OleDbCommand
If Not con.State = ConnectionState.Open Then
'open connection
con.Open()
End If
cmd.Connection = con
'check whether add new or update
If Me.semat.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = " INSERT INTO Tb_History(His_products, His_quantity, His_student, His_teacher, His_date, His_time)" & _
" VALUES ('" & Me.semat.Text & "'," & Me.txtnum.Text & ",'" & _
Me.txtna1.Text & "','" & Me.txtna2.Text & "','" & Me.txtdate.Text & "','" & _
Me.txttime.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("บันทึกลงตารางเรียบร้อยแล้ว")
'refresh data in list
RefreshData()
'close connection
con.Close()
End If
Catch ex As Exception
MessageBox.Show("เกิดข้อผิดพลาด เนื่องจาก " & ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
Tag : .NET, Ms Access, VS 2012 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2015-11-14 18:08:28 |
By :
peemes101 |
View :
1260 |
Reply :
22 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
XXX จะเขียนอย่างไรดีครับ
เพราะในdatagridviewคือ columns ที่5 ชื่อว่า จำนวนสินค้า
จากโค้ดในradio คืออันนี้ DataGridView1.Columns(5).Visible = True
และ
ืnot insert จะเขียนประมาณไหนดีครับ
|
 |
 |
 |
 |
Date :
2015-11-15 12:40:29 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
IF DataGridView1.Columns(5).Visible <> True Then
' Not Insert
Else
'Insert
End IF
|
 |
 |
 |
 |
Date :
2015-11-16 13:37:33 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ในปุ่มบันทึกดังนี้
Code (VB.NET)
If DataGridView1.Columns(5).Visible <= 0 > True Then
MsgBox("ไม่สามารถบันทึกได้")
Else
'check whether add new or update
If Me.semat.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = " INSERT INTO Tb_History(His_products, His_quantity, His_student, His_teacher, His_date, His_time)" & _
" VALUES ('" & Me.semat.Text & "'," & Me.txtnum.Text & ",'" & _
Me.txtna1.Text & "','" & Me.txtna2.Text & "','" & Me.txtdate.Text & "','" & _
Me.txttime.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("บันทึกลงตารางเรียบร้อยแล้ว")
'refresh data in list
RefreshData()
'close connection
con.Close()
End If
End If
แล้วทำไมมันเข้าที่ INSERT อย่างเดียวเลย?
|
 |
 |
 |
 |
Date :
2015-11-17 06:47:45 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
If DataGridView1.Columns(5).Visible <= 0 > True Then
ซึ่งใน<> ก็ใส่เงือนไขว่าถ้า =0 จะบันทึกไม่ได้หรือไม่ใช่ครับ
แต่ใส่อันนี้ If DataGridView1.Columns(5).Visible <> True Then
จะบันทึกได้หมด
แต่สิ่งที่ต้องการคือ Columsนี้มัน =0 จบันทึกไม่ได้ครับ
|
 |
 |
 |
 |
Date :
2015-11-17 21:55:14 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ้าวแล้วยังงั้น
ควรเขียนอย่างไรละครับ
ว่า ถ้าคอลัมนี้ น้อยกว่าหรือเท่ากับ 0 จะบันทึกไม่ได้
|
 |
 |
 |
 |
Date :
2015-11-18 11:04:28 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
If (int)DataGridView1.Rows(???).Columns(5).Value<= 0 Then
บ้านๆเลย แค่ดึงค่าใน Columns(5) ออกมาแค่นั้นครับ
|
 |
 |
 |
 |
Date :
2015-11-18 13:38:15 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใส่แล้วครับ
มันขึ้นerrorดังรูป

|
 |
 |
 |
 |
Date :
2015-11-18 14:06:29 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
If (int)DataGridView1.Rows(???).Columns(5).Value<= 0 Then
บ้านๆ คือ
??? คือ Rows ที่เราเปิดอ่านข้อมูลอ่าครับ
ถ้า (int) ไม่ได้ก็ใช้ Convert รึไรก็ได้ที่ให้ได้ค่าออกมาเป็น int ครับ
|
 |
 |
 |
 |
Date :
2015-11-18 14:34:35 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อืม ขอโทษนะจะว่าดีไหม ที่คุณ TOR บอกอ่ะมันก็ถูกแล้ว แต่เขาบอกมาเป็นแนวทาง เข้าใจว่าพึงเริ่มเขียนแต่ก็อย่าทิ้งหนังสือหรือพี่ goo
พี่ goo อีกล่ะ -_-"
จะอธิบายให้ก็แล้วกันนะครับ
if = ถ้า DataGridView1 = ตารางข้อมูล1 Rows = แถว ที่เขาใส่ ??? เพราะไม่รู้ว่าแถวที่เท่าไร Columns(5) = Index ของ Columns เป็นชื่อก็ได้ value = ค่าที่ได้
(int) = เหมือนให้ Convert เป็น ตัวเลข ไม่งั้นไม่เข้าเงื่อนไข <= 0
"เพราะไม่ได้ vb มานานแล้วอาจจะอธิบายไม่ถูกหมดนะครับ"
If (int)DataGridView1.Rows(???).Columns(5).Value<= 0 Then
ถ้าเราจะเอามาใช้จะต้องเป็นประมาณนี้
If (int)DataGridView1.Rows(1).Columns(5).Value<= 0 Then
แต่ก็ละมันจะตรวจเฉพาะแถวที่ 1 เท่านั้นแต่เรามีข้อมูลใน ตารางเยอะกว่า 1 แถวเราจะทำอย่างไง
ลองดูเรื่อง Loop For Each While...End While เพราะอย่างไงก็หนีไม่พ้น
ลองดูครับ
และอีกอย่างเวลาหาข้อมูลกับพี่เขาอะ(พี่ goo) ไม่ต้องพิมพ์ยาวให้พิมพ์สั่นก็ได้ เช่น vb loop datagridview พี่เขาเข้าใจครับผม
|
 |
 |
 |
 |
Date :
2015-11-18 14:36:50 |
By :
บัญดิษฐ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณสำหรับคำอธิบายครับ
แต่พอใส่ If (int)DataGridView1.Rows(1).Columns(5).Value<= 0 Then
มันerror เหลือ1ที่ คือ End of statement expected. โดยขึ้นยักๆสีน้ำเงินตั้งแต่ datagridviewไปครับ
และ
ข้อมูลแถวของผมมันแยกตามradio ที่เลือกซึ่งถ้าเลือก rad 1 มี 10 แถว และถ้าเลือกrad 2 มี 15 แถว
ดังควรเขียนอย่างไรดีครับ
|
 |
 |
 |
 |
Date :
2015-11-18 16:10:52 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
If (int)DataGridView1.Rows(???).Columns(5).Value<= 0 Then
บ้านๆ คือ
??? คือ Rows ที่เราเปิดอ่านข้อมูลอ่าครับ
ถ้า (int) ไม่ได้ก็ใช้ Convert รึไรก็ได้ที่ให้ได้ค่าออกมาเป็น int ครับ
เหมือนเอาคำตอบเก่ามาตอบเลย 5555
ลองศึกษาให้มากๆหน่อยครับ
|
 |
 |
 |
 |
Date :
2015-11-18 17:22:16 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องใส่แบบนี้
If Int(DataGridView1.Rows(1).Columns(5).Value) <= 0 Then
แต่ขึ้นerrorว่า 'Columns' is not a member of 'System.Windows.Forms.DataGridViewRow'.
|
 |
 |
 |
 |
Date :
2015-11-19 01:18:05 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือมีradioให้เลือกอยู่ 2 อัน ซึ่งเมื่อเลือกแบบไหน ก็จะโชว์ของสินค้านั้นในdatagridview1
ดังนั้นมีส่วนที่ทำให้error หรือป่าวครับ
และนับถูกครับ มันอยู่ตำแหน่งที่5ในdatabase แต่พอมาโชว์ใน vb มันอยู่คอลัมที่3
|
 |
 |
 |
 |
Date :
2015-11-19 10:14:37 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันยัง error ว่า Columns' is not a member of 'System.Windows.Forms.DataGridViewRow'. ครับ
โดยขึ้นยักสีน้ำเงิน ใต้ DataGridView1.Rows(1).Columns ครับ
|
 |
 |
 |
 |
Date :
2015-11-19 12:02:05 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาอย่างนี้แล้วกัน ถ้าผมอธิบายแล้วยังไม่เขาใจอีก คุณเดินไปร้านหนังสือไปซื้อมาสักเล่ม ไม่เอาเล่มนี้ที่คุณมีอยู่
แล้วคุณจะรู้ว่าโลกนี้มันไม่ได้เขียนโค้ดได้แบบเดียว วิธีเดียว คุณต้องศึกษาทำความเขาใจ แก้ปัญหาเอง
ค้นหา google ก็บอกแล้ว เขามีบอกทุกอย่าง ไม่จำเป็นต้องเก่งภาษา ผมจบ ปวส ภาษาผมก็ไม่ได้
แต่รู้อะไรไหม บริษัทยังจ้างผมอยู่
ถ้าดูตัวอย่างนี้แล้วยังไม่ได้ คงไม่มีคำตอบให้อีกแล้วละครับ
Code (VB.NET)
Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click
Dim rowindex As String
Dim found as Boolean = false
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells.Item("ITEM_ID").Value = TextBox5.Text Then
rowindex = row.Index.ToString()
found = true
Dim actie As String = row.Cells("PRICE").Value.ToString()
MsgBox(actie)
Exit for
End If
Next
If Not found Then
MsgBox("Item not found")
End if
End Sub
ไม่ได้ให้เอาโค้ดไปใช้ทั้งหมดเลย แต่เอาให้ศึกษาดู วิธีที่ชาวบ้านเขาเขียน ฮ่วย..หมอนี่ คือเว้ายากแท้
ผมว่า ผมเริ่มรู้สึกเหมือนท่านหน้าฮี แล้วละ(ว่าท่านมาเยอะเหมือนกัน เขาตัวเองเลย)
|
 |
 |
 |
 |
Date :
2015-11-19 13:24:26 |
By :
บัญดิษฐ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากๆเลย
สามารถทำได้แล้ว
|
 |
 |
 |
 |
Date :
2015-11-20 17:49:09 |
By :
peemes101 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|