Re: Mit Textbox auf Spalte von DataGrid zugreifen



Hallo Tamara,

Ich habe auf einer Form eine DataGridView und darin 3 Spalten.
Den Wert der 3. Spalte hätte ich gerne in einer Textbox.
Von Access her kenne ich die Möglichkeit von Column(3).text

Vergiss Access und überlege, wo Deine Daten ursprünglich herkommen.
Das nachfolgende Beispiel zeigt Dir, wie Du direkt auf die eigentliche
Datenquelle zugreifst:

' /// Code in einer leeren Form1
Public Class Form1
Private WithEvents DGV As DataGridView
Private WithEvents mCM As CurrencyManager
Private mDT As DataTable
Private mDV As DataView

Private Sub InitDGV()
DGV = New DataGridView
With DGV
.Bounds = New Rectangle _
( _
10, 10, _
Me.ClientSize.Width - 20, _
Me.ClientSize.Height - 20 _
)

.Anchor = AnchorStyles.Left Or _
AnchorStyles.Top Or _
AnchorStyles.Right Or _
AnchorStyles.Bottom

.Font = New Font("Arial", 10)
End With
Me.Controls.Add(DGV)
End Sub

Private Sub MakeData()
Dim DR As DataRow
Dim i As Integer

mDT = New DataTable
With mDT
.Columns.Add("ID", GetType(Integer))
.Columns(0).Unique = True

.Columns.Add("Text", GetType(String))


For i = 1 To 12
DR = .NewRow
DR.Item(0) = i
DR.Item(1) = MonthName(i)
.Rows.Add(DR)
Next
.AcceptChanges()
End With

mDV = New DataView(mDT)

mCM = CType(Me.BindingContext(mDV), _
CurrencyManager)
End Sub


Private Sub Form1_Load _
( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles Me.Load

MakeData()
InitDGV()

DGV.DataSource = mDV
mCM.Position = 5
End Sub

Private Sub mCM_PositionChanged _
( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
) Handles mCM.PositionChanged

Dim RecNum As Integer = mCM.Position
If RecNum > -1 Then
Dim DRV As DataRowView

DRV = CType(mCM.Current, DataRowView)

' alternativ
'DRV = mDV.Item(RecNum)

Me.Text = DRV.Item(0).ToString & _
" : " & _
DRV.Item(1).ToString
Else
Me.Text = ""
End If
End Sub
End Class
' \\\ E N T E

Du kannst natürlich auch die Daten aus den jeweiligen DataGridView-Cells
holen, was aber implizit auch nur wieder ein Zugriff auf die ursprünglichen
Daten der DataSource (DataView/DataTable) ist. Mit nachfolgendem Code in
DGV_CurrentCellChanged wäre ein solcher Zugriff möglich.

Dim DGVR As DataGridViewRow = DGV.CurrentRow
Me.Text = DGVR.Cells(0).Value.ToString & " : " & _
DGVR.Cells(1).Value.ToString()

Zum Sortieren, Filtern usw. brauchst Du in fast allen Fällen ohnehin eine
DataView und zum Navigieren von Datensatz zu Datensatz ist ein
CurrencyManager sinnvoll. Also ist es einfacher und direkter, gleich diese
"ursprünglichen" Objekte zu verwenden um auf die Daten zuzugreifen.

Gruß aus St.Georgen
Peter Götz
www.gssg.de (mit VB-Tipps u. Beispielprogrammen)



.



Relevant Pages

  • Re: An event raise twice when click the button.
    ... Private Sub AddinInstance_OnAddInsUpdateAs Variant) ... Private Sub AddinInstance_OnBeginShutdownAs Variant) ... Private WithEvents m_objInsp As Outlook.Inspector ... Dim tempMailItem As Outlook.MailItem ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: An event raise twice when click the button.
    ... Private Sub AddinInstance_OnAddInsUpdateAs Variant) ... Private Sub AddinInstance_OnBeginShutdownAs Variant) ... Private WithEvents m_objInsp As Outlook.Inspector ... Dim tempMailItem As Outlook.MailItem ...
    (microsoft.public.office.developer.outlook.vba)
  • Move images and lines on a picturebox....
    ... Private Type LINKDATA ... sLabel2 As String ... Dim MyData() As LINKDATA ... Private Sub Form_MouseDown(ByRef Button As Integer, ...
    (microsoft.public.vb.general.discussion)
  • DX8 SetNotificationPositions, Error 32811
    ... Private SEnum As DxVBLibA.DirectSoundEnum8 ... ' buffer, and buffer description ... Private Sub DirectXEvent8_DXCallback ... Dim WaveBuffer() As Byte ...
    (microsoft.public.vb.directx)
  • Re: Position eines Datensatzes in einer DataView
    ... Private WithEvents DGV As DataGridView ... Private Sub Form1_Load _ ... Dim DR As DataRow ... Me.ClientSize = New Size ...
    (microsoft.public.de.german.entwickler.dotnet.vb)