Link to home
Start Free TrialLog in
Avatar of LJ Gaviola
LJ Gaviola

asked on

getting value of a cell in a grid

hello.. am not really sure where to post my question. nways, here it goes..

i want to get a value of a cell in a grid. i have a value and i want to find the match of that value in my grid. how do i do this? i want to do something like this (parametized):

sub myvalue(toFindValue)
   how to find the value routines goes here..
end sub

btw, i think the grid is xceed. thanks!

jean
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Usually it is better to do a search of the grid's datasource instead of the grid itself.  What is the datasource for your grid? Is your search limited to a specific column or open to anywhere in the grid?
Here is a sample of picking a cell data....


'FORM 1

Public Class frmPickData
    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"
        '
        'frmPickData
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(560, 169)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "frmPickData"
        Me.Text = "frmPickData"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
#End Region

    Private textboxArray As ArrayList
    Private myDataTable As DataTable

    Private Sub frmPickData_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 third column.
        myDataColumn = New DataColumn
        myDataColumn.DataType = System.Type.GetType("System.String")
        myDataColumn.ColumnName = "Type"
        myDataTable.Columns.Add(myDataColumn)

        ' Create four column.
        myDataColumn = New DataColumn
        myDataColumn.DataType = System.Type.GetType("System.String")
        myDataColumn.ColumnName = "Date"
        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"
        myDataRow("Date") = "1/14/2005"
        myDataTable.Rows.Add(myDataRow)

        myDataRow = myDataTable.NewRow()
        myDataRow("ID") = 2
        myDataRow("Name") = "Spot"
        myDataRow("Type") = "Dog"
        myDataRow("Date") = "3/24/2004"
        myDataTable.Rows.Add(myDataRow)

        myDataRow = myDataTable.NewRow()
        myDataRow("ID") = 3
        myDataRow("Name") = "Hammie"
        myDataRow("Type") = "Hamster"
        myDataRow("Date") = "7/20/2004"
        myDataTable.Rows.Add(myDataRow)

        myDataRow = myDataTable.NewRow()
        myDataRow("ID") = 4
        myDataRow("Name") = "Hammer"
        myDataRow("Type") = "Goat"
        myDataRow("Date") = "12/04/2005"
        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

    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim i As Integer
        Dim curRow As DataRow

        If DataGrid1.CurrentRowIndex() >= 0 Then
            DataGrid1.UnSelect(DataGrid1.CurrentRowIndex)
            curRow = myDataTable.Rows(CType(sender.text, Integer) - 1)
            If curRow.ItemArray.Length > 0 Then
                For i = 0 To curRow.ItemArray.GetUpperBound(0)
                    DataGrid1.UnSelect(i)
                Next
                DataGrid1.Select(curRow.Item(0) - 1)
            End If
        End If
    End Sub
    '    You could also add a handler to the cells like this

    ' The data grid controls are special controls (DataGridTextBox) so that you add event handlers for each of the cell
    'And handle the key events .. You can write the key_up, KeyPress handlers also...
    '--------------------------------------------------------------------------------------------------------------------------------

    Private Sub Grid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Dim DataGridCell As DataGridTextBox
        If TypeOf (sender) Is DataGridTextBox Then
            DataGridCell = DirectCast(sender, DataGridTextBox)
            MsgBox(DataGridCell.Text)
        End If

    End Sub

    Private Sub DataGrid1_ControlAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles DataGrid1.ControlAdded
        AddHandler e.Control.KeyDown, AddressOf Grid_KeyDown
    End Sub
End Class
If your looking for a way to trap the Mouse Event, determining a cell:

        Private Sub dgWorkOrders_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgWOItems.MouseDown, dgWOItemsBlanks.MouseDown
        Dim dg As DataGrid = CType(sender, DataGrid)
        If e.Button = MouseButtons.Right Then
            Dim hti As System.Windows.Forms.DataGrid.HitTestInfo
            hti = dg.HitTest(e.X, e.Y)
            Select Case hti.Type
                Case DataGrid.HitTestType.Cell
                    ' We have a cell...
                    ' Use Methods to determine what is in the cell...
                    Dim txt As String = CType(sender, DataGridTextBox).TextBox.Text ' txt now holds the value that was in the TextBox...
                Case Else
                    bShowRow = False
            End Select
        End If
    End Sub

Hope that helps,

Jake
Avatar of LJ Gaviola
LJ Gaviola

ASKER

hi again. i am writing a test script using testcomplete.. i was just passing a parameter and find that parameter in the grid. the grid has 3 columns which have names. about searching it from the datasource, that's not really what i want.

hope you still can help me :)

jean
jean,

I'm not sure exactly what you are doing, but in order to investigate a Grid, you need to examine the DataSource.  There is just no other way that's proper.  What exactly are you wanting, because I'm not sure I understand exactly what you want.

Jake
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
hi, i got it already! what i did was close to what planocz suggested.. :) here's my code:

            DSCount = m.DataSourcesGrid.get_RootGrid.get_DataRows.Count - 1
            For i = 0 to DSCount
                row = m.DataSourcesGrid.get_RootGrid.get_DataRows.get_Item(i).get_Cells.get_Item(1).CreateAccessibilityInstance.Value    
                If (row = src) Then
                    x = m.DataSourcesGrid.get_RootGrid.get_DataRows.get_Item(i).get_ClientPosition.x
                    y = m.DataSourcesGrid.get_RootGrid.get_DataRows.get_Item(i).get_ClientPosition.y
                    Call m.DataSourcesGrid.Click(x, y)    
                End If
            Next  

I am using testcomplete for this script so it's a bit different from what planocz posted :)

thanks everyone! ;)