史萊姆論壇

返回   史萊姆論壇 > 專業主討論區 > 程式語言討論區
忘記密碼?
論壇說明

歡迎您來到『史萊姆論壇』 ^___^

您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的!

請點擊這裡:『註冊成為我們的一份子!』

Google 提供的廣告


發文 回覆
 
主題工具 顯示模式
舊 2014-01-21, 09:50 PM   #1
魔術王子
版區管理員
 
魔術王子 的頭像
榮譽勳章
UID - 115097
在線等級: 級別:42 | 在線時長:1993小時 | 升級還需:28小時級別:42 | 在線時長:1993小時 | 升級還需:28小時
註冊日期: 2004-01-13
住址: 魔術學園
文章: 2989
精華: 0
現金: 14967 金幣
資產: 2678597 金幣
預設 程式 - [使用版本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
魔術王子 目前離線  
送花文章: 1530, 收花文章: 1555 篇, 收花: 3894 次
回覆時引用此帖
舊 2014-01-21, 09:50 PM   #2 (permalink)
版區管理員
 
魔術王子 的頭像
榮譽勳章
UID - 115097
在線等級: 級別:42 | 在線時長:1993小時 | 升級還需:28小時級別:42 | 在線時長:1993小時 | 升級還需:28小時
註冊日期: 2004-01-13
住址: 魔術學園
文章: 2989
精華: 0
現金: 14967 金幣
資產: 2678597 金幣
預設

引用:
作者: 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列的第一欄資料
魔術王子 目前離線  
送花文章: 1530, 收花文章: 1555 篇, 收花: 3894 次
回覆時引用此帖
發文 回覆



發表規則
不可以發文
不可以回覆主題
不可以上傳附加檔案
不可以編輯您的文章

論壇啟用 BB 語法
論壇啟用 表情符號
論壇啟用 [IMG] 語法
論壇禁用 HTML 語法
Trackbacks are 禁用
Pingbacks are 禁用
Refbacks are 禁用

相似的主題
主題 主題作者 討論區 回覆 最後發表
程式 - [使用版本VB2008]程式設定選單 魔術王子 程式語言討論區 0 2014-01-22 06:19 AM


所有時間均為台北時間。現在的時間是 06:27 AM


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


SEO by vBSEO 3.6.1