史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   [使用版本VB2008]DataGridView元件不連接資料庫使用 (http://forum.slime.com.tw/thread275341.html)

魔術王子 2014-01-21 09:50 PM

[使用版本VB2008]DataGridView元件不連接資料庫使用
 
在Visual Basic語言中DataGridView是跟資料庫相關聯的元件
DataGridView可以將資料庫的資料列示出來,並且做修改
不過大家不曉得有沒有試過,不透過資料庫,來使用DataGridView做資料編輯
以下便是程式執行畫面,以及相關程式碼:
https://imagizer.imageshack.us/v2/570x443q90/842/80oa.jpg
引用:

作者: Form1.Designer.vb

[略]

Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
Friend WithEvents ToolStripButton1 As System.Windows.Forms.ToolStripButton
Friend WithEvents ToolStripSeparator1 As System.Windows.Forms.ToolStripSeparator
Friend WithEvents ToolStripButton2 As System.Windows.Forms.ToolStripButton
Friend WithEvents ToolStripButton3 As System.Windows.Forms.ToolStripButton
Friend WithEvents ToolStripButton4 As System.Windows.Forms.ToolStripButton
Friend WithEvents ToolStripButton5 As System.Windows.Forms.ToolStripButton
Friend WithEvents Column1 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column2 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents Column3 As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents ToolStripButton6 As System.Windows.Forms.ToolStripButton
Friend WithEvents StatusStrip1 As System.Windows.Forms.StatusStrip
Friend WithEvents ToolStripStatusLabel1 As System.Windows.Forms.ToolStripStatusLabel
Friend WithEvents ToolStripStatusLabel2 As System.Windows.Forms.ToolStripStatusLabel
Friend WithEvents ToolStripStatusLabel3 As System.Windows.Forms.ToolStripStatusLabel

End Class


魔術王子 2014-01-21 09:50 PM

引用:

作者: Form1.resx

[略]

<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="ToolStripButton1.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFNSURBVDhPY2AYtODuAvb/t+Zx/r86h+v/xRm8/0l26HWg
xj+PGv//edjw/+RkAdINOD+dF27AwR4R0g04PkkQaHvd/z8Pqv7vaBcnbMClmTz/z07l+38SqPFIv9D/
fd0iQM3l///eK/q/vkHm/6oauf9LKxX/zy9V/j+rSBXTwFNAf/55WA/UVAvElf//3CsB4rz/f+9m/v97
J/n/39tx///djADikP+TcjQxDTjUKwx27t/7pUCNBUCN2UCNqUCNCf//3YoGagz7/+9G0P9/1/3+d6Xp
Yhqws13i/5Zmqf/rwM6V/7+kQvH/31sxQI3h/6fmafyfkKX1vztD9397qv7/5kRDwmEyG+hPkHP/3Qj4
35OhQ1gDekIB+fPfDX8g9vvfmqxPugHd6bpg/4JwfZwR6Qa0JBn8b4g3/F8Ta/y/MtqEdANITvvkagAA
JCPYnqPoI1MAAAAASUVORK5CYII=
</value>
</data>
<data name="ToolStripButton6.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFvSURBVDhPrZJNKMNxGMd3Vd5KubigVg4j0/ISEjIkLDIh
MkPagYNIaBcHB1Iu5ICL5EBSUk60OM3b5q01tf5Lk9Iu+Mfl9zEWpf3xJ7/Lc/h9n8/zfL89Gs1/PDxu
sJnA6eBPPNYWEd1VCIsRdja/hXDtg5W5zxo2lqDPjOgwIjorYGFKEfK6obCUwVhv5D+nzjCk3oBoyof+
5k8iMdKFMOfAtP17myzPIirT3kCMdoH3DCYGw9BQVZURPg+0FIUtmbIQDbkw0Kqu+X0CB3uhlXMR1elQ
q/9lc0CCpgLITkDWx3Ovj4U6gzoIAT+Y86AwiaC1Gi6O38K91YUgq/M/BHh3C20lPBQn4y1NJ7i/+9HA
eD8ubQyy+1AZwvMTDFuhJIUTXSLBrfUI4U15BlJ7DfK1X+EO9raRcxJxpUYh9TSC/Kg4yaGNQ3Y5FQAz
Y0hp0XiNmSBdfelVttvg/EgBcOmCySE42leXtqqLUiF6AYojAxxFb0PyAAAAAElFTkSuQmCC
</value>
</data>
<data name="ToolStripButton2.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADySURBVDhPvZDPqkRgGMbnVhBmgSyw0WxIUiILSZQNio2s
rCwUK/fCJT6nV51pVjI6na++3tXv+fd4/OVblgXruoLuOI7o+x5N06AoCiRJAs/zcOo3TRPmeT7gYRjQ
dR3Ksjxg3/dhWda5wKf6vu+o6xp0v25JEDmnaYogCLBt23WRX+c8zxFFEVzXhWma15IQ3LYtsix7w6/X
C5qmQZKk8yQUs6oqxHGMMAzhOA4I1nX9gAVBAM/z1+vYtv12/npEAqi3oigQRfH6gJ9OhmHg+XyCZdl7
AqqqguO449+qQMMRzDDMPQFZlo/4VONWgn+DfgDiaJDZcgAVdQAAAABJRU5ErkJggg==
</value>
</data>
<data name="ToolStripButton3.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADCSURBVDhPzZExCoQwEEW9igZExIigsQnaiGBhoZUW2hkt
BQ+mR/ySgMtWS5xqAyHVez9/xnH+9pzniW3boN/Xn9TQvu9YlgXDMOC6LnvJA6/rimma0Pc9qqqyk+ik
4ziglPrAdV2jLEtkWfZb8iTP84xxHNF1HZqmQVEUBuacIwgC+5m0bWvgNE3t+39PW/cWQpjk11vQgJQS
cRzD932aIM9z05kxRhMkSQLXdekC3d3zPHNJM4iiyKSHYUgTkFIp0A0z2m/fYf6PfgAAAABJRU5ErkJg
gg==
</value>
</data>
<data name="ToolStripButton4.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADASURBVDhPzZK9CoMwFIV9l+DPokvipE4uAYMgCC6CbuKo
W15KH/HIDaRDobW9dGggU+73nZOQIPjbZa3Fvu9Y1xWskl4wzzNPsG0blmXBMAw8AcHjOKJtW56A4K7r
UNc1T9D3PbTWKMuSJzDGOFhK+ZngOA5M04SmaVztqqqQ5znSNEWSJBBCIAxDnOf5WkgSejSClVIOjuPY
wbTp/PZPUEJRFMiyDFEUPeC3yc9WGqZ0n/wV7GUEUQMWfHvPXw5cAtZwsTsuedUAAAAASUVORK5CYII=
</value>
</data>
<data name="ToolStripButton5.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8
YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADUSURBVDhPtZK7CoQwEEX9FRUfhQiCVmIasRMEESsLK8VO
rfwq/cQrd2Fgm5Vs2A2kSXLO3EliWf8ax3Fg33csywKjGhRs24ZxHM0E67pimib0fW8mIDwMA+q6NhMQ
btsWZVmaCRi9qirkea4nuK4L8zyj6zo0TfOClVJI0xRRFCEIAnieh/M8Pwu5KZWLokCWZYjjGL7vw3Gc
Z1jemknYt8BhGMJ1XT34XZIkCQjbtv0dLBK2w+hMpP0TWVUujLFZnZP9c3LtUUZYehaAkLZAO+ovD96W
b4aqnNMymgAAAABJRU5ErkJggg==
</value>
</data>
<metadata name="StatusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>129, 17</value>
</metadata>
</root>

引用:

作者: Form1.vb
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Rows.Clear()
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
DataGridView1.MultiSelect = False
End Sub

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
DataGridView1.Rows.Add("資料 " & DataGridView1.Rows.Count & " - 1", "資料 " & DataGridView1.Rows.Count & " - 2", "資料 " & DataGridView1.Rows.Count & " - 3")
ToolStripButton6.Enabled = False
ToolStripButton2.Enabled = False
ToolStripButton3.Enabled = False
ToolStripButton4.Enabled = False
ToolStripButton5.Enabled = False
End Sub

Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
End Sub

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
DataGridView1.Rows.Insert(0, DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value, DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value, DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value)
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
DataGridView1.Rows(0).Cells(0).Selected = True
End Sub

Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
Dim i As Single = DataGridView1.CurrentRow.Index
If i = 0 Then
ToolStripButton5_Click(sender, e)
Else
DataGridView1.Rows.Insert(i - 1, DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value)
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
DataGridView1.Rows(i - 1).Cells(0).Selected = True
End If
End Sub

Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
Dim i As Single = DataGridView1.CurrentRow.Index
If i >= DataGridView1.Rows.Count - 2 Then
ToolStripButton2_Click(sender, e)
Else
DataGridView1.Rows.Insert(i + 2, DataGridView1.Rows(i).Cells(0).Value, DataGridView1.Rows(i).Cells(1).Value, DataGridView1.Rows(i).Cells(2).Value)
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
DataGridView1.Rows(i + 1).Cells(0).Selected = True
End If
End Sub

Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
DataGridView1.Rows.Insert(DataGridView1.Rows.Count - 1, DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value, DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value, DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value)
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Selected = True
End Sub

Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
ToolStripStatusLabel1.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value
ToolStripStatusLabel2.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value
ToolStripStatusLabel3.Text = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value
End Sub

Private Sub DataGridView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count - 1 Then
ToolStripButton6.Enabled = True
ToolStripButton2.Enabled = True
ToolStripButton3.Enabled = True
ToolStripButton4.Enabled = True
ToolStripButton5.Enabled = True
Else
ToolStripButton6.Enabled = False
ToolStripButton2.Enabled = False
ToolStripButton3.Enabled = False
ToolStripButton4.Enabled = False
ToolStripButton5.Enabled = False
End If
End Sub
End Class

Form1.Designer.vb為設計元件的表單
Form1.resx為程式的資源檔
Form1.vb為這程式的程式碼

整個程式就是表單上插入ToolStrip元件和DataGridView元件,ToolStrip元件再加入6個ToolStripButton元件DataGridView元件要加入三個Column元件,StatusStrip元件可有可無,只是為了講解如何取得資料

DataGridView1.Rows.Add(資料1,資料2,資料3,...)為新增資料列的方法(註:資料數量要跟欄位數量相等)
DataGridView1.Rows.RemoveAt(Index)移除Index列資料
DataGridView1.Rows.Insert(Index,資料1,資料2,資料3,...)在Index列後面插入資料(註:資料數量要跟欄位數量相等)
DataGridView1.Rows(Index).Cells(0).Value取得Index列的第一欄資料


所有時間均為台北時間。現在的時間是 11:15 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1