 |
|
จะสามารถเขียน Code VB.Net ดึงชื่อชีส Sheet ใน Excel มาแสดงใน Combobox มีวีธีไหนบ้างคับ |
|
 |
|
|
 |
 |
|
http://stackoverflow.com/questions/13618224/how-to-get-sheets-name-from-excel-2007-xlsx-using-vb-net-or-c-sharp
|
 |
 |
 |
 |
Date :
2015-06-24 13:51:59 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ในบทความมีวิธีอ่านด้วย ADO.Net ครับ
ASP.NET ADO.NET & Excel (Odbc)
|
 |
 |
 |
 |
Date :
2015-06-24 19:01:17 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("Data Source=PTK530080085-PC;Initial Catalog=test;Integrated Security=True;Pooling=False")
cn.Open()
Dim message As String = String.Empty
Dim cm As New SqlCommand
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
If isSelected Then
message &= row.Cells("id").Value.ToString()
With cm
.Connection = cn
For i As Integer = 0 To DataGridView1.RowCount - 2
.CommandText = "insert into test2 values('" & DataGridView1.Rows(i).Cells(1).Value & "','" & DataGridView1.Rows(i).Cells(2).Value & "','" & DataGridView1.Rows(i).Cells(3).Value & "' )"
.ExecuteNonQuery()
Next
End With
End If
Next
cm.Dispose()
cm = Nothing
With cn
.Close()
.Dispose()
End With
cn = Nothing
End Sub
คือการ insert จะ insert เฉพาะแถวที่ ติ๊ก Checkbox จาก DataGridView เอาไว้อะคับ
แต่ตอนนี้เข้าแล้วแต่เข้าหมดทุกแถวจาก DataGridView ไม่เข้าแถวที่เราเลือกอะคับ
|
 |
 |
 |
 |
Date :
2015-06-25 08:57:09 |
By :
aphisit007 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
For i As Integer = 0 To DataGridView1.RowCount - 2
if Checkbox = true then ' insert เฉพาะตัวที่ Checkbox ไว้
.CommandText = "insert into test2 values('" & DataGridView1.Rows(i).Cells(1).Value & "','" & DataGridView1.Rows(i).Cells(2).Value & "','" & DataGridView1.Rows(i).Cells(3).Value & "' )"
.ExecuteNonQuery()
end if
Next
|
 |
 |
 |
 |
Date :
2015-06-25 09:11:29 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
อันนี้ผมแอด checkbox ไว้ตอน Fromload
มันเชค ไม่เจออะคับ
|
 |
 |
 |
 |
Date :
2015-06-25 09:31:39 |
By :
aphisit007 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("Data Source=PTK530080085-PC;Initial Catalog=test;Integrated Security=True;Pooling=False")
cn.Open()
Dim message As String = String.Empty
Dim cm As New SqlCommand
For Each row As DataGridViewRow In DataGridView1.Rows
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
With cm
.Connection = cn
For i As Integer = 0 To DataGridView1.RowCount - 2
If isSelected = True Then ' insert เฉพาะตัวที่ Checkbox ไว้
.CommandText = "insert into test2 values('" & DataGridView1.Rows(i).Cells(1).Value & "','" & DataGridView1.Rows(i).Cells(2).Value & "','" & DataGridView1.Rows(i).Cells(3).Value & "' )"
.ExecuteNonQuery()
End If
Next
End With
Next
cm.Dispose()
cm = Nothing
With cn
.Close()
.Dispose()
End With
cn = Nothing
End Sub
ยังไม่ได้เลยอะคับ
|
 |
 |
 |
 |
Date :
2015-06-25 14:48:46 |
By :
aphisit007 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
For Each row As DataGridViewRow In DataGridView1.Rows
For i As Integer = 0 To DataGridView1.RowCount - 2
มันผิดตรงนี้อ่าครับ
บ้านๆ คือ
Code (VB.NET)
For i As Integer = 0 To DataGridView1.RowCount - 2
if check = true then
insert
end if
next
บ้านก็มีแค่นี้ครับ
ลองดูใหม่ๆๆๆ
|
 |
 |
 |
 |
Date :
2015-06-25 15:41:37 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Friend Class ConnectData 'ประกาศเป็น Friend Class จะได้ใช้ได้ทั่วถึงทุก class
Dim cn As New SqlConnection("Data Source=PTK530080085-PC;Initial Catalog=test;Integrated Security=True;Pooling=False")
Dim cm As New SqlCommand
Public Sub ConnectOpen()
cn.Open()
End Sub
Public Sub ConnectClose()
cm.Dispose()
cm = Nothing
With cn
.Close()
.Dispose()
End With
cn = Nothing
End Sub
Public Sub SQLExecute(sql As String)
With cm
.Connection = cn
.CommandText = sql
.ExecuteNonQuery()
End With
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ConnectData.ConnectOpen() 'เปิดใช้งาน SqlConnection
Dim SQL As String =""
For i As Integer = 0 To DataGridView1.RowCount - 2
If Convert.ToBoolean(DataGridView1(0,i).Value)= True Then ' insert เฉพาะตัวที่ Checkbox ไว้
SQL = "insert into test2 values('" & DataGridView1(1,i).Value & "','" & DataGridView1(2,i).Value & "','" & DataGridView1(3,i).Value & "' )"
ConnectData.SQLExecute(SQL) 'เรียกใช้ SQLExecute
End If
Next
End With
ConnectData.ConnectClose() 'ปิดใช้งาน SqlConnection
End Sub
ประมาณนี้ครับ
Delete Update ก็ใช้หลักการเดียวกัน
แค่นี้ก็ได้ class ออกมาใช้เล่นแบบสบายๆ แถมใช้ง่ายไม่ต้องประกาศตัวแปรให้วุ่นวายด้วยครับ 5555
|
 |
 |
 |
 |
Date :
2015-06-25 16:03:56 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
'For i As Integer = 0 To DataGridView1.RowCount - 2 *****
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("Data Source=PTK530080085-PC;Initial Catalog=test;Integrated Security=True;Pooling=False")
cn.Open()
Dim message As String = String.Empty
Dim cm As New SqlCommand
For Each row As DataGridViewRow In DataGridView1.Rows 'แถวแรก ถึง แถวสุดท้าย
Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
With cm
.Connection = cn
'For i As Integer = 0 To DataGridView1.RowCount - 2 *****
If (isSelected) AndAlso (Not row.IsNewRow) Then ' insert เฉพาะตัวที่ Checkbox ไว้
.CommandText = "insert into test2 values('" & row.Cells(1).Value & "','" & row.Cells(2).Value & "','" & row.Cells(3).Value & "' )"
.ExecuteNonQuery()
End If
'Next *****
End With
Next
cm.Dispose()
cm = Nothing
With cn
.Close()
.Dispose()
End With
cn = Nothing
End Sub
|
 |
 |
 |
 |
Date :
2015-06-25 16:40:40 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อสังเกตุพอเป็นพิธี
1.
จาก #NO 10 IsNewRow เป็นใครมาจากไหน
มันมาจากนี่ DataGridView Property AllowUserToAddRows
เพราะฉะนั้น DataGridView.Rows.Count - 2 ผลลัพธ์สุ่มเสี่ยง (ข้อมูลไม่ครบ)
2. นิพจน์ And แตกต่างกับ AndAslo อย่างไร?
If (isSelected) And (Not row.IsNewRow) มันจะประมวลผลทั้งสองนิพจน์เสมอ
If (isSelected) AndAlso (Not row.IsNewRow) ถ้านิพจน์แรกเป็นเท็จมันจะไม่ประมวลผลนิพจน์ที่ 2
3. cn.Open()
สมมุติว่า DataGridView2.Rows.Count = 0 มันเท่ากับว่า อ้าขาให้ดูฟรีฟรี
ถ้าจำไม่ผิดนะ
ตั้งแต่ VS2005 จะมีคำสั่ง Using xxx ... End Using ให้ใช้งานได้สะดวกยิ่งขึ้น
xxx จะถูกเปิดและปิดโดยอัตโนมัติ (ไม่ต้องเปิด/ปิด เอง)
|
 |
 |
 |
 |
Date :
2015-06-25 17:08:01 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แถมให้อีกนิดหนึ่งเกี่ยวกับ
Using xxx
...
End Using
อะไรก็ตามที่สั่ง Dispose() ได้ก็ย่อมใช้ Using...End Using ได้เสมอ
ปล. ลองศึกษาคำสั่ง Dispose() เพิ่มเติมว่ามันมาจากไหน (รู้ต้นและรู้ปลาย)
|
 |
 |
 |
 |
Date :
2015-06-25 17:19:55 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากคับ ได้แล้วคับผม
|
 |
 |
 |
 |
Date :
2015-06-26 09:31:29 |
By :
aphisit007 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|