Jeg har lavet en form, der har en DataGridView (data), to tekstbokse (txt1 og txt2) samt en knap (btn). Det er ikke nøjagtig hvad du efterspørger, men princippet er det samme, så forhåbentlig vil det hjælpe dig.
Når man klikker på knappen første gang, så henter den, om noget, det der står i vores DataGridView, og indsætter det i txt1.
Ved andet klik henter den, igen om noget, det der står i txt2, og indsætter det i vores DataGridView. Der er kun en række med en kolonne og altså kun et felt i vores DataGridView, og det har koordinaterne 0, 0, som det også fremgår af koden, men dette burde ikke være noget problem at lave om på. Det samme gælder med de 2 tekstbokse frem for 50 tekstbokse.
Form1.vb:
- Public Class Form1
- ' Til at starte med - ved første klik - skal den hente data
- Private getData As Boolean = True
-
- Private Function HasData() As Boolean
- Return Not IsNothing(data.Item(0, 0).Value)
- End Function
-
- Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
- If getData Then
- ' Tjek om der overhovedet er noget i vores DataGridView
- If Not HasData() Then
- MessageBox.Show("Ingen data at hente!")
- Return
- End If
-
- ' Læg data i textbox 1
- txt1.Text = data.Item(0, 0).Value.ToString()
- ElseIf Not getData
- ' Tjek om vi har noget at indsætte i vores DataGridView
- If String.IsNullOrEmpty(txt2.Text) Then
- MessageBox.Show("Ingen data at sætte!")
- Return
- End If
-
- ' Tag teksten fra textbox 2 og set den i vores DataGridView
- data.Item(0, 0).Value = txt2.Text
- End If
-
- ' Vend getData om, så True bliver til False og omvendt
- getData = Not getData
- End Sub
- End Class
Form1.Designer.vb:
- <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
- Partial Class Form1
- Inherits System.Windows.Forms.Form
-
- 'Form overrides dispose to clean up the component list.
- <System.Diagnostics.DebuggerNonUserCode()> _
- Protected Overrides Sub Dispose(ByVal disposing As Boolean)
- Try
- If disposing AndAlso components IsNot Nothing Then
- components.Dispose()
- End If
- Finally
- MyBase.Dispose(disposing)
- End Try
- End Sub
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.IContainer
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> _
- Private Sub InitializeComponent()
- Me.data = New System.Windows.Forms.DataGridView()
- Me.col1 = New System.Windows.Forms.DataGridViewTextBoxColumn()
- Me.txt1 = New System.Windows.Forms.TextBox()
- Me.txt2 = New System.Windows.Forms.TextBox()
- Me.Label1 = New System.Windows.Forms.Label()
- Me.Label2 = New System.Windows.Forms.Label()
- Me.btn = New System.Windows.Forms.Button()
- CType(Me.data, System.ComponentModel.ISupportInitialize).BeginInit()
- Me.SuspendLayout()
- '
- 'data
- '
- Me.data.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
- Me.data.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.col1})
- Me.data.Location = New System.Drawing.Point(13, 13)
- Me.data.Name = "data"
- Me.data.Size = New System.Drawing.Size(148, 74)
- Me.data.TabIndex = 0
- '
- 'col1
- '
- Me.col1.HeaderText = "Data"
- Me.col1.Name = "col1"
- '
- 'txt1
- '
- Me.txt1.Location = New System.Drawing.Point(12, 116)
- Me.txt1.Name = "txt1"
- Me.txt1.Size = New System.Drawing.Size(100, 20)
- Me.txt1.TabIndex = 1
- '
- 'txt2
- '
- Me.txt2.Location = New System.Drawing.Point(12, 173)
- Me.txt2.Name = "txt2"
- Me.txt2.Size = New System.Drawing.Size(100, 20)
- Me.txt2.TabIndex = 2
- '
- 'Label1
- '
- Me.Label1.AutoSize = True
- Me.Label1.Location = New System.Drawing.Point(12, 100)
- Me.Label1.Name = "Label1"
- Me.Label1.Size = New System.Drawing.Size(54, 13)
- Me.Label1.TabIndex = 3
- Me.Label1.Text = "Textbox 1"
- '
- 'Label2
- '
- Me.Label2.AutoSize = True
- Me.Label2.Location = New System.Drawing.Point(10, 157)
- Me.Label2.Name = "Label2"
- Me.Label2.Size = New System.Drawing.Size(54, 13)
- Me.Label2.TabIndex = 4
- Me.Label2.Text = "Textbox 2"
- '
- 'btn
- '
- Me.btn.Location = New System.Drawing.Point(12, 219)
- Me.btn.Name = "btn"
- Me.btn.Size = New System.Drawing.Size(100, 23)
- Me.btn.TabIndex = 5
- Me.btn.Text = "Hent/Indsæt"
- Me.btn.UseVisualStyleBackColor = True
- '
- 'Form1
- '
- Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
- Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
- Me.ClientSize = New System.Drawing.Size(669, 254)
- Me.Controls.Add(Me.btn)
- Me.Controls.Add(Me.Label2)
- Me.Controls.Add(Me.Label1)
- Me.Controls.Add(Me.txt2)
- Me.Controls.Add(Me.txt1)
- Me.Controls.Add(Me.data)
- Me.Name = "Form1"
- Me.Text = "Form1"
- CType(Me.data, System.ComponentModel.ISupportInitialize).EndInit()
- Me.ResumeLayout(False)
- Me.PerformLayout()
-
- End Sub
-
- Friend WithEvents data As DataGridView
- Friend WithEvents col1 As DataGridViewTextBoxColumn
- Friend WithEvents txt1 As TextBox
- Friend WithEvents txt2 As TextBox
- Friend WithEvents Label1 As Label
- Friend WithEvents Label2 As Label
- Friend WithEvents btn As Button
- End Class