Solved

DataGrid Row/Col Data Transfer to TextBoxes

Posted on 2004-08-26
3
1,097 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 250 total points
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now