Results 1 to 3 of 3

Thread: [02/03] Problem selecting first row in DataGrid

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    22

    Question [02/03] Problem selecting first row in DataGrid

    Good morning, all! Here's my problem: I have a normal datagrid that when a user selects a row, it will open up another form w/ details about the selected row. It all works fine except when I click on the very top of the datagrid (where the column names are displayed) - it gives me an error on my next form because current row index = 0. I can add an if to catch this, but then if the first data row is selected, it will not actually select the row. What am I doing wrong?

    I would like for the users to click at the top of the datagrid to sort the data and also be able to select the first row in the datagrid. I can't seem to get both functions working properly.

    Here's my code:

    VB Code:
    1. Private Sub productlist_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    2. Handles productlist.MouseUp
    3. If numclicks > 498 Then
    4.             MessageBox.Show("You have selected too many items.")
    5.         Else
    6.             Try
    7.  
    8.                 Dim pt = New Point(e.X, e.Y)
    9.  
    10.                 Dim hti As DataGrid.HitTestInfo = productlist.HitTest(pt)
    11.  
    12.                 If hti.Type = DataGrid.HitTestType.Cell Then
    13.  
    14.                     productlist.CurrentCell = New DataGridCell(hti.Row, hti.Column)
    15.  
    16.                     productlist.Select(hti.Row)
    17.  
    18.                     ProdID = Me.productlist.Item(Me.productlist.CurrentRowIndex, 0).ToString
    19.                    
    20.                 End If
    21.  
    22.             Catch ex As Exception
    23.                 MessageBox.Show(ex.ToString)
    24.  
    25.             End Try
    26.         End If
    27.  
    28.         'If productlist.CurrentRowIndex = 0 Then '= "" Then ' Then
    29.         'productlist.CurrentRowIndex = 1
    30.         'ProdID = ""
    31.         'MessageBox.Show("Please select an item inside the grid.")
    32.         'Else
    33.             Dim myform As New productdetailform
    34.             myform.Show()
    35.         'End If
    36.  
    37.  
    38.     End Sub

    I can uncomment different parts of the if statement to get different behaviors in the datagrid, but I can not get it to where I can sort by clicking at the top AND select the first row of data.

    Thanks for any and all help you folks can provide!
    Last edited by amerif; May 12th, 2006 at 07:37 AM.

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