Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGrid Row/Col Data Transfer to TextBoxes

Posted on 2004-08-26
3
Medium Priority
?
1,133 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Rougy
3 Comments
 
LVL 27

Accepted Solution

by:
planocz earned 1000 total points
ID: 11904323
Example:
   Private Sub datagrid1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagrid1.MouseUp
            MyTextBox1.Text = datagrid1.Item(datagrid1.CurrentRowIndex, 1)  '<----First column
            MyTextBox2.Text = datagrid1.Item(datagrid1.CurrentRowIndex, 2)  '<----Second column
End Sub
0
 

Author Comment

by:Rougy
ID: 11904373
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.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11904430
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question