Solved

DataGrid Row/Col Data Transfer to TextBoxes

Posted on 2004-08-26
3
1,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
planocz earned 250 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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