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
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
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.ICon tainer
'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.DataG rid
Friend WithEvents TextBox1 As System.Windows.Forms.TextB ox
Friend WithEvents TextBox2 As System.Windows.Forms.TextB ox
Friend WithEvents TextBox3 As System.Windows.Forms.TextB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataG rid
Me.TextBox1 = New System.Windows.Forms.TextB ox
Me.TextBox2 = New System.Windows.Forms.TextB ox
Me.TextBox3 = New System.Windows.Forms.TextB ox
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).Begi nInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol or = System.Drawing.SystemColor s.ControlT ext
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.TextBox 3)
Me.Controls.Add(Me.TextBox 2)
Me.Controls.Add(Me.TextBox 1)
Me.Controls.Add(Me.DataGri d1)
Me.Name = "frmPickData"
Me.Text = "frmPickData"
CType(Me.DataGrid1, System.ComponentModel.ISup portInitia lize).EndI nit()
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("Syste m.Int32")
myDataColumn.ColumnName = "ID"
myDataColumn.Unique = True
myDataTable.Columns.Add(my DataColumn )
' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste m.String")
myDataColumn.ColumnName = "Name"
myDataTable.Columns.Add(my DataColumn )
' Create third column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste m.String")
myDataColumn.ColumnName = "Type"
myDataTable.Columns.Add(my DataColumn )
' Create four column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste m.String")
myDataColumn.ColumnName = "Date"
myDataTable.Columns.Add(my DataColumn )
' 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(myDat aRow)
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 2
myDataRow("Name") = "Spot"
myDataRow("Type") = "Dog"
myDataRow("Date") = "3/24/2004"
myDataTable.Rows.Add(myDat aRow)
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 3
myDataRow("Name") = "Hammie"
myDataRow("Type") = "Hamster"
myDataRow("Date") = "7/20/2004"
myDataTable.Rows.Add(myDat aRow)
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 4
myDataRow("Name") = "Hammer"
myDataRow("Type") = "Goat"
myDataRow("Date") = "12/04/2005"
myDataTable.Rows.Add(myDat aRow)
' 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_CurrentCellChang ed(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChang ed
Dim i As Integer
Dim curRow As DataRow
If DataGrid1.CurrentRowIndex( ) >= 0 Then
curRow = myDataTable.Rows(DataGrid1 .CurrentRo wIndex())
If curRow.ItemArray.Length > 0 Then
For i = 0 To curRow.ItemArray.GetUpperB ound(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(DataGri d1.Current RowIndex)
curRow = myDataTable.Rows(CType(sen der.text, Integer) - 1)
If curRow.ItemArray.Length > 0 Then
For i = 0 To curRow.ItemArray.GetUpperB ound(0)
DataGrid1.UnSelect(i)
Next
DataGrid1.Select(curRow.It em(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.KeyEv entArgs)
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(ByV al sender As Object, ByVal e As System.Windows.Forms.Contr olEventArg s) Handles DataGrid1.ControlAdded
AddHandler e.Control.KeyDown, AddressOf Grid_KeyDown
End Sub
End Class
'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.ICon
'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.DataG
Friend WithEvents TextBox1 As System.Windows.Forms.TextB
Friend WithEvents TextBox2 As System.Windows.Forms.TextB
Friend WithEvents TextBox3 As System.Windows.Forms.TextB
<System.Diagnostics.Debugg
Me.DataGrid1 = New System.Windows.Forms.DataG
Me.TextBox1 = New System.Windows.Forms.TextB
Me.TextBox2 = New System.Windows.Forms.TextB
Me.TextBox3 = New System.Windows.Forms.TextB
CType(Me.DataGrid1, System.ComponentModel.ISup
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeCol
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.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.TextBox
Me.Controls.Add(Me.DataGri
Me.Name = "frmPickData"
Me.Text = "frmPickData"
CType(Me.DataGrid1, System.ComponentModel.ISup
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("Syste
myDataColumn.ColumnName = "ID"
myDataColumn.Unique = True
myDataTable.Columns.Add(my
' Create second column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste
myDataColumn.ColumnName = "Name"
myDataTable.Columns.Add(my
' Create third column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste
myDataColumn.ColumnName = "Type"
myDataTable.Columns.Add(my
' Create four column.
myDataColumn = New DataColumn
myDataColumn.DataType = System.Type.GetType("Syste
myDataColumn.ColumnName = "Date"
myDataTable.Columns.Add(my
' 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(myDat
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 2
myDataRow("Name") = "Spot"
myDataRow("Type") = "Dog"
myDataRow("Date") = "3/24/2004"
myDataTable.Rows.Add(myDat
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 3
myDataRow("Name") = "Hammie"
myDataRow("Type") = "Hamster"
myDataRow("Date") = "7/20/2004"
myDataTable.Rows.Add(myDat
myDataRow = myDataTable.NewRow()
myDataRow("ID") = 4
myDataRow("Name") = "Hammer"
myDataRow("Type") = "Goat"
myDataRow("Date") = "12/04/2005"
myDataTable.Rows.Add(myDat
' 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_CurrentCellChang
Dim i As Integer
Dim curRow As DataRow
If DataGrid1.CurrentRowIndex(
curRow = myDataTable.Rows(DataGrid1
If curRow.ItemArray.Length > 0 Then
For i = 0 To curRow.ItemArray.GetUpperB
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
Dim i As Integer
Dim curRow As DataRow
If DataGrid1.CurrentRowIndex(
DataGrid1.UnSelect(DataGri
curRow = myDataTable.Rows(CType(sen
If curRow.ItemArray.Length > 0 Then
For i = 0 To curRow.ItemArray.GetUpperB
DataGrid1.UnSelect(i)
Next
DataGrid1.Select(curRow.It
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.KeyEv
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(ByV
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(ByV al sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles dgWOItems.MouseDown, dgWOItemsBlanks.MouseDown
Dim dg As DataGrid = CType(sender, DataGrid)
If e.Button = MouseButtons.Right Then
Dim hti As System.Windows.Forms.DataG rid.HitTes tInfo
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.T ext ' txt now holds the value that was in the TextBox...
Case Else
bShowRow = False
End Select
End If
End Sub
Hope that helps,
Jake
Private Sub dgWorkOrders_MouseDown(ByV
Dim dg As DataGrid = CType(sender, DataGrid)
If e.Button = MouseButtons.Right Then
Dim hti As System.Windows.Forms.DataG
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.T
Case Else
bShowRow = False
End Select
End If
End Sub
Hope that helps,
Jake
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi, i got it already! what i did was close to what planocz suggested.. :) here's my code:
DSCount = m.DataSourcesGrid.get_Root Grid.get_D ataRows.Co unt - 1
For i = 0 to DSCount
row = m.DataSourcesGrid.get_Root Grid.get_D ataRows.ge t_Item(i). get_Cells. get_Item(1 ).CreateAc cessibilit yInstance. Value
If (row = src) Then
x = m.DataSourcesGrid.get_Root Grid.get_D ataRows.ge t_Item(i). get_Client Position.x
y = m.DataSourcesGrid.get_Root Grid.get_D ataRows.ge t_Item(i). get_Client Position.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! ;)
DSCount = m.DataSourcesGrid.get_Root
For i = 0 to DSCount
row = m.DataSourcesGrid.get_Root
If (row = src) Then
x = m.DataSourcesGrid.get_Root
y = m.DataSourcesGrid.get_Root
Call m.DataSourcesGrid.Click(x,
End If
Next
I am using testcomplete for this script so it's a bit different from what planocz posted :)
thanks everyone! ;)