Link to home
Start Free TrialLog in
Avatar of Rougy
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(.flexgrid.Row, 1)
   .textbox(2) = .flexgrid.TextMatrix(.flexgrid.Row, 2)
   .textbox(3) = .flexgrid.TextMatrix(.flexgrid.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.
ASKER CERTIFIED SOLUTION
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rougy
Rougy

ASKER

That's the ticket!

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.
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.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.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        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.TextBox3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        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("System.Int32")
        myDataColumn.ColumnName = "ID"
        myDataColumn.Unique = True
        myDataTable.Columns.Add(myDataColumn)

        ' Create second column.
        myDataColumn = New DataColumn
        myDataColumn.DataType = System.Type.GetType("System.String")
        myDataColumn.ColumnName = "Name"
        myDataTable.Columns.Add(myDataColumn)

        ' Create thid column.
        myDataColumn = New DataColumn
        myDataColumn.DataType = System.Type.GetType("System.String")
        myDataColumn.ColumnName = "Type"
        myDataTable.Columns.Add(myDataColumn)

        ' 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(myDataRow)

        myDataRow = myDataTable.NewRow()
        myDataRow("ID") = 2
        myDataRow("Name") = "Spot"
        myDataRow("Type") = "Dog"
        myDataTable.Rows.Add(myDataRow)

        myDataRow = myDataTable.NewRow()
        myDataRow("ID") = 3
        myDataRow("Name") = "Hammie"
        myDataRow("Type") = "Hamster"
        myDataTable.Rows.Add(myDataRow)

        ' 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_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
        Dim i As Integer
        Dim curRow As DataRow

        If DataGrid1.CurrentRowIndex() >= 0 Then
            curRow = myDataTable.Rows(DataGrid1.CurrentRowIndex())
            If curRow.ItemArray.Length > 0 Then
                For i = 0 To curRow.ItemArray.GetUpperBound(0)
                    If i <= textboxArray.Count - 1 Then
                        textboxArray(i).text = curRow.ItemArray(i)
                    End If
                Next
            End If
        End If
    End Sub

End Class