Results 1 to 5 of 5

Thread: [02/03] Problem jumping to a record in a datagrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    [02/03] Problem jumping to a record in a datagrid

    I'm working with a form for which I've added a pair of datagrids. When the user selects a record in the first datagrid, the records in the second one display the records related to the record in the first table.

    The user can also get the related record data for a particular record by selecting a record in the parent application (ArcMap) and hitting a button on the form. The problem is, this only shows the correct rows in the bottom DataGrid. I want to have the top datagrid snap to the record that the user selected. I tried the NavigateTo and Select methods for DataGrid but neither worked correctly. I'm not sure if I need to add something else to the creation of my DataGrid - the code to do so is below. It has some calls to the Arc interfaces - basically it is reading the details of a database table (most commonly an Access table) and creates a DataGrid to show most of the columns in it.

    VB Code:
    1. Private primaryTableGrid, relatedTableGrid As DataGrid
    2.     Dim keyList As String()
    3.     Private Function buildTable() As DataGrid
    4.         Dim myGrid As DataGrid
    5.         myGrid = New Windows.Forms.DataGrid
    6.         myGrid.Size = New Size(795, 200)
    7.         myGrid.TabStop = True
    8.         myGrid.TabIndex = 1
    9.  
    10.  
    11.         Dim pDoc As IMxDocument
    12.  
    13.         Dim pMap As IMap
    14.  
    15.         pDoc = m_app.Document
    16.         pMap = pDoc.FocusMap
    17.         If pDoc.SelectedLayer Is Nothing OrElse Not TypeOf pDoc.SelectedLayer Is IFeatureLayer Then
    18.             MsgBox("Please select a feature layer")
    19.             Exit Function
    20.         End If
    21.         pFeatureLayer = pDoc.SelectedLayer
    22.         Dim pFClass As IFeatureClass
    23.         Dim pFCursor As IFeatureCursor
    24.  
    25.  
    26.         pFClass = pFeatureLayer.FeatureClass
    27.         pFCursor = pFClass.Search(Nothing, True)
    28.  
    29.  
    30.  
    31.         Dim myGridStyle As DataGridTableStyle = _
    32.                          New DataGridTableStyle
    33.         myGridStyle.MappingName = "NamesTable"
    34.         Dim namesDataSet As DataSet = New DataSet
    35.         Dim namesDataTable As DataTable
    36.         namesDataTable = New DataTable("NamesTable")
    37.         namesDataSet.Tables.Add(namesDataTable)
    38.  
    39.         Dim vw As DataView
    40.         Dim fieldNames As String()
    41.         Dim counter, counter2, counter3 As Integer
    42.         counter2 = 0
    43.         For counter = 0 To pFeatureLayer.FeatureClass.Fields.FieldCount - 1
    44.             If pFeatureLayer.FeatureClass.Fields.Field(counter).Type <= esriFieldType.esriFieldTypeOID Then
    45.                 counter2 += 1
    46.             End If
    47.         Next
    48.         Dim fieldWidths(counter2 - 1) As Integer
    49.         Dim isNum(counter2 - 1) As Boolean
    50.         ReDim fieldNames(counter2 - 1)
    51.         counter2 = 0
    52.         For counter = 0 To pFeatureLayer.FeatureClass.Fields.FieldCount - 1
    53.             If pFeatureLayer.FeatureClass.Fields.Field(counter).Type <= esriFieldType.esriFieldTypeOID Then
    54.                 fieldNames(counter2) = pFeatureLayer.FeatureClass.Fields.Field(counter).Name
    55.                 If pFClass.Fields.Field(counter).Type = esriFieldType.esriFieldTypeString Then
    56.                     fieldWidths(counter2) = pFClass.Fields.Field(counter).Length * 6
    57.                     isNum(counter2) = False
    58.                 Else
    59.                     fieldWidths(counter2) = 80
    60.                     isNum(counter2) = True
    61.                 End If
    62.                 counter2 += 1
    63.             End If
    64.         Next
    65.  
    66.         Dim fieldColumn(fieldNames.Length - 1) As DataColumn
    67.         For counter = 0 To fieldNames.Length - 1
    68.             If isNum(counter) Then
    69.                 fieldColumn(counter) = New DataColumn(fieldNames(counter), GetType(Double))
    70.             Else
    71.                 fieldColumn(counter) = New DataColumn(fieldNames(counter), GetType(String))
    72.             End If
    73.  
    74.             namesDataTable.Columns.Add(fieldColumn(counter))
    75.         Next
    76.         For counter = 0 To fieldNames.Length - 1
    77.  
    78.             Dim cOrderDate As New DataGridTextBoxColumn
    79.             cOrderDate.MappingName = fieldNames(counter)
    80.             cOrderDate.HeaderText = fieldNames(counter)
    81.             cOrderDate.Width = fieldWidths(counter)
    82.             myGridStyle.GridColumnStyles.Add(cOrderDate)
    83.         Next
    84.         Dim pFeature As IFeature
    85.         pFeature = pFCursor.NextFeature
    86.         Dim keyIDX As Integer
    87.         If Not relatedTable Is Nothing Then
    88.             keyIDX = pFClass.FindField(localKey)
    89.             currentKeyValue = pFeature.Value(pFClass.FindField(localKey))
    90.         End If
    91.         ReDim keyList(pFClass.FeatureCount(Nothing) - 1)
    92.         counter3 = 0
    93.         Do While Not pFeature Is Nothing
    94.             keyList(counter3) = pFeature.Value(keyIDX)
    95.             counter3 += 1
    96.             Dim dRow As DataRow = namesDataTable.NewRow()
    97.             Try
    98.                 counter2 = 0
    99.                 For counter = 0 To pFeatureLayer.FeatureClass.Fields.FieldCount - 1 'fieldNames.Length - 1
    100.  
    101.                     If pFeatureLayer.FeatureClass.Fields.Field(counter).Type <= esriFieldType.esriFieldTypeOID Then
    102.  
    103.                         dRow(fieldNames(counter2)) = pFeature.Value(counter) 'fieldValues(counter) ' New String(fieldNames(counter)) 'DateTime(2001, 12, 1)
    104.                         counter2 += 1
    105.                     End If
    106.                 Next
    107.             Catch ex As Exception
    108.                 Console.WriteLine(ex)
    109.  
    110.             End Try
    111.             namesDataTable.Rows.Add(dRow)
    112.  
    113.             pFeature = pFCursor.NextFeature
    114.         Loop
    115.         pFCursor = Nothing
    116.         GC.Collect()
    117.  
    118.         namesDataTable.AcceptChanges()
    119.         vw = New DataView(namesDataTable)
    120.         vw.AllowNew = False
    121.         myGrid.DataSource = vw
    122.         Return myGrid
    123.     End Function
    Last edited by Holywhippet; May 2nd, 2006 at 01:18 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: [02/03] Problem jumping to a record in a datagrid

    [vbcode]Your code here[/Highlight] gives you:
    VB Code:
    1. Your code here

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Re: [02/03] Problem jumping to a record in a datagrid

    Well. I made some progress after a bit of experimentation. I found that the NavigateTo method kind of works if I leave the relationship name blank (since there are no relationships in the table). However, if I do this I get the names of the fields appearing at the top of the form for each time I invoke this, unless I press a small button that is marked "show/hide the parent rows for the current set of child rows". The other problem is that the scrollbar seems to move to the correct location, but the actual datagrid doesn't redraw to the show the correct record. If I click on the scrollbar once it redraws in the correct location though.

    I think my main problem might be the relationship name. I don't have any relations for this table. The documentation seems to indicate I can set up a DataRelation which doesn't link two tables but rather indicates uniqueness for a column - but I can't work out how to do this. Any ideas?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,439

    Re: [02/03] Problem jumping to a record in a datagrid

    Ah. That's better. I don't think that you should be using the NavigateTo method as that's for navigating a relationship between two tables in one grid. If you follow the WinForms FAQ link in my signature you should be able to find information on using two grids in a master-detail relationship. As for selecting a row in the parent grid, try setting the CurrentRowIndex property, although I would have though that calling Select would have worked. Maybe a call to Refresh after calling Select.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Canberra, Australia
    Posts
    175

    Re: [02/03] Problem jumping to a record in a datagrid

    Ah, thanks. Setting the CurrentRowIndex worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width