Rougy
asked on
DataGrid Row/Col Data Transfer to TextBoxes
Hi,
I can't seem to find how to reference and send a row of data to a form's textboxes in winform mode.
With a flexgrid it used to be something like:
With form
.textbox(1) = .flexgrid.TextMatrix(.flex grid.Row, 1)
.textbox(2) = .flexgrid.TextMatrix(.flex grid.Row, 2)
.textbox(3) = .flexgrid.TextMatrix(.flex grid.Row, 3)
End With
I'd greatly appreciate the help.
And if anybody knows of a good book or link that goes into depth regarding the datagrid (especially formatting) I'd really appreciate it.
I'm very miffed that Microsoft has made this the centerpiece of both their webform and winform GUI, and yet they've provided virtually no substantial, informative documentation.
Thanks.
I can't seem to find how to reference and send a row of data to a form's textboxes in winform mode.
With a flexgrid it used to be something like:
With form
.textbox(1) = .flexgrid.TextMatrix(.flex
.textbox(2) = .flexgrid.TextMatrix(.flex
.textbox(3) = .flexgrid.TextMatrix(.flex
End With
I'd greatly appreciate the help.
And if anybody knows of a good book or link that goes into depth regarding the datagrid (especially formatting) I'd really appreciate it.
I'm very miffed that Microsoft has made this the centerpiece of both their webform and winform GUI, and yet they've provided virtually no substantial, informative documentation.
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'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.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataG rid
Friend WithEvents TextBox1 As System.Windows.Forms.TextB ox
Friend WithEvents TextBox2 As System.Windows.Forms.TextB ox
Friend WithEvents TextBox3 As System.Windows.Forms.TextB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataG rid
Me.TextBox1 = New System.Windows.Forms.TextB ox
Me.TextBox2 = New System.Windows.Forms.TextB ox
Me.TextBox3 = New System.Windows.Forms.TextB ox
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).Begi nInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol or = System.Drawing.SystemColor s.ControlT ext
Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(544, 128)
Me.DataGrid1.TabIndex = 0
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 144)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(144, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(160, 144)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(160, 20)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = "TextBox2"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(328, 144)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(168, 20)
Me.TextBox3.TabIndex = 3
Me.TextBox3.Text = "TextBox3"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(560, 174)
Me.Controls.Add(Me.TextBox 3)
Me.Controls.Add(Me.TextBox 2)
Me.Controls.Add(Me.TextBox 1)
Me.Controls.Add(Me.DataGri d1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).EndI nit()
Me.ResumeLayout(False)
End Sub
#End Region
Private textboxArray As ArrayList
Private myDataTable As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
populateDataGrid()
makeControlArray()
End Sub
Private Sub populateDataGrid()
' Create a new DataTable.
mydatatable = New DataTable("myTable")
' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow
' Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste m.Int32")
myDataColumn.ColumnName = "ID"
myDataColumn.Unique = True
myDataTable.Columns.Add(my DataColumn )
' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste m.String")
myDataColumn.ColumnName = "Name"
myDataTable.Columns.Add(my DataColumn )
' Create thid column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste m.String")
myDataColumn.ColumnName = "Type"
myDataTable.Columns.Add(my DataColumn )
' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0) = myDataTable.Columns("id")
myDataTable.PrimaryKey = PrimaryKeyColumns
' Create three new DataRow objects and add them to the DataTable
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 1
myDataRow("Name") = "Fluffy"
myDataRow("Type") = "Cat"
myDataTable.Rows.Add(myDat aRow)
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 2
myDataRow("Name") = "Spot"
myDataRow("Type") = "Dog"
myDataTable.Rows.Add(myDat aRow)
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 3
myDataRow("Name") = "Hammie"
myDataRow("Type") = "Hamster"
myDataTable.Rows.Add(myDat aRow)
' Set the DataSource of our DataGrid to our DataTable
DataGrid1.DataSource = myDataTable
End Sub
Private Sub makeControlArray()
' there are not control arrays in VB.Net
' this mimics that behaviour though
textboxArray = New ArrayList
textboxArray.Add(TextBox1)
textboxArray.Add(TextBox2)
textboxArray.Add(TextBox3)
End Sub
Private Sub DataGrid1_CurrentCellChang ed(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChang ed
Dim i As Integer
Dim curRow As DataRow
If DataGrid1.CurrentRowIndex( ) >= 0 Then
curRow = myDataTable.Rows(DataGrid1 .CurrentRo wIndex())
If curRow.ItemArray.Length > 0 Then
For i = 0 To curRow.ItemArray.GetUpperB ound(0)
If i <= textboxArray.Count - 1 Then
textboxArray(i).text = curRow.ItemArray(i)
End If
Next
End If
End If
End Sub
End Class
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'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.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataG
Friend WithEvents TextBox1 As System.Windows.Forms.TextB
Friend WithEvents TextBox2 As System.Windows.Forms.TextB
Friend WithEvents TextBox3 As System.Windows.Forms.TextB
<System.Diagnostics.Debugg
Me.DataGrid1 = New System.Windows.Forms.DataG
Me.TextBox1 = New System.Windows.Forms.TextB
Me.TextBox2 = New System.Windows.Forms.TextB
Me.TextBox3 = New System.Windows.Forms.TextB
CType(Me.DataGrid1, System.ComponentModel.ISup
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol
Me.DataGrid1.Location = New System.Drawing.Point(8, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(544, 128)
Me.DataGrid1.TabIndex = 0
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 144)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(144, 20)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "TextBox1"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(160, 144)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(160, 20)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = "TextBox2"
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(328, 144)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(168, 20)
Me.TextBox3.TabIndex = 3
Me.TextBox3.Text = "TextBox3"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(560, 174)
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.DataGri
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISup
Me.ResumeLayout(False)
End Sub
#End Region
Private textboxArray As ArrayList
Private myDataTable As DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
populateDataGrid()
makeControlArray()
End Sub
Private Sub populateDataGrid()
' Create a new DataTable.
mydatatable = New DataTable("myTable")
' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow
' Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste
myDataColumn.ColumnName = "ID"
myDataColumn.Unique = True
myDataTable.Columns.Add(my
' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste
myDataColumn.ColumnName = "Name"
myDataTable.Columns.Add(my
' Create thid column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste
myDataColumn.ColumnName = "Type"
myDataTable.Columns.Add(my
' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0) = myDataTable.Columns("id")
myDataTable.PrimaryKey = PrimaryKeyColumns
' Create three new DataRow objects and add them to the DataTable
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 1
myDataRow("Name") = "Fluffy"
myDataRow("Type") = "Cat"
myDataTable.Rows.Add(myDat
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 2
myDataRow("Name") = "Spot"
myDataRow("Type") = "Dog"
myDataTable.Rows.Add(myDat
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 3
myDataRow("Name") = "Hammie"
myDataRow("Type") = "Hamster"
myDataTable.Rows.Add(myDat
' Set the DataSource of our DataGrid to our DataTable
DataGrid1.DataSource = myDataTable
End Sub
Private Sub makeControlArray()
' there are not control arrays in VB.Net
' this mimics that behaviour though
textboxArray = New ArrayList
textboxArray.Add(TextBox1)
textboxArray.Add(TextBox2)
textboxArray.Add(TextBox3)
End Sub
Private Sub DataGrid1_CurrentCellChang
Dim i As Integer
Dim curRow As DataRow
If DataGrid1.CurrentRowIndex(
curRow = myDataTable.Rows(DataGrid1
If curRow.ItemArray.Length > 0 Then
For i = 0 To curRow.ItemArray.GetUpperB
If i <= textboxArray.Count - 1 Then
textboxArray(i).text = curRow.ItemArray(i)
End If
Next
End If
End If
End Sub
End Class
ASKER
Would you happen to know the event name of a click on the grid cell as opposed to the grid row or col?
You still get the points. Thanks very much.