Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have search function in my project. The text in the search box modifies a dataview rowfilter.

When the Search Text textbox loses focus, I reset the rowfilter.

What I am trying to do is if a user clicks a cell in the datagrid showing the search results, is to use a value from that row to populate other fields.

The problem I am having is that the rowIndex always returns Zero.

I need to catch the row number that was clicked and get the stock code BEFORE the rowfilter gets reset by the Lost Focus Sub when the Search text Box loses focus.

VB
Private Sub txtStkEnqSearchText_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtStkEnqSearchText.LostFocus
       If Me.dgvStkEnqSearchResults.Focused Then
           Dim RowNo As Integer = Me.dgvStkEnqSearchResults.CurrentRow.Index
           Dim StockCode As String = Me.dgvStkEnqSearchResults.Rows(RowNo).Cells("StockCode").Value
           Me.txtStockEnquiry_StockCode.Text = StockCode
       End If
       txtStkEnqSearchText.Text = "Enter Search Text here..."
       txtStkEnqSearchText.ForeColor = Color.DimGray
   End Sub

   Private Sub txtStkEnqSearchText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtStkEnqSearchText.GotFocus
       If txtStkEnqSearchText.Text = "Enter Search Text here..." Then
           txtStkEnqSearchText.Text = ""
           txtStkEnqSearchText.ForeColor = Color.Black
           dvSearch.RowFilter = "CodeDesc LIKE '%" & Me.txtStkEnqSearchText.Text & "%'"
       End If
   End Sub

   Private Sub txtStkEnqSearchText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStkEnqSearchText.TextChanged
       If txtStkEnqSearchText.Text = "" Then
           dvSearch.RowFilter = "CodeDesc LIKE '%" & Me.txtStkEnqSearchText.Text & "%'"
           txtStkEnqSearchText.ForeColor = Color.DimGray
       ElseIf txtStkEnqSearchText.Text = "Enter Search Text here..." Then
           dvSearch.RowFilter = "CodeDesc LIKE ''"
       Else
           txtStkEnqSearchText.ForeColor = Color.Black
           dvSearch.RowFilter = "CodeDesc LIKE '%" & Me.txtStkEnqSearchText.Text & "%'"
       End If

   End Sub

   Private Sub txtStockEnquiry_StockCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStockEnquiry_StockCode.TextChanged
       If Me.txtStockEnquiry_StockCode.Text = "" Then
           ResetStockEnquiry()
           Exit Sub
       End If

       Dim dr As DataRow

       dr = GetStockItem(Me.txtStockEnquiry_StockCode.Text)
       Me.txtStockEnquiry_Desc.Text = dr.Item("Description")
       Me.txtStockEnquiry_MaxStkLev.Text = dr.Item("MaxStkLev")
       Me.txtStockEnquiry_MinStkLev.Text = dr.Item("MinStkLev")
       Me.txtStockEnquiry_UM.Text = dr.Item("Unit")
       Me.txtStockEnquiry_StockType.Text = dr.Item("StockType")
       Me.txtStockEnquiry_ABC_Cat.Text = dr.Item("ABCCat")
       Me.txtStockEnquiry_ProductGroup.Text = dr.Item("ProductGroup")
       dtStkQty.Rows.Clear()
       GetStockItemQty(dtStkQty, Me.txtStockEnquiry_StockCode.Text)
       Me.txtStockEnquiry_AllocatedQty.Text = GetAllocatedQty(Me.txtStockEnquiry_StockCode.Text)
       dr = Nothing
   End Sub

   Private Sub ResetStockEnquiry()
       Me.cboStockEnquiry_StockCode.SelectedIndex = 0
       Me.txtStockEnquiry_AllocatedQty.Text = ""
       Me.txtStockEnquiry_Desc.Text = ""
       Me.txtStockEnquiry_MaxStkLev.Text = ""
       Me.txtStockEnquiry_MinStkLev.Text = ""
       Me.txtStockEnquiry_ProductGroup.Text = ""
       Me.txtStockEnquiry_StockCode.Text = ""
   End Sub
Posted

If I'm not much mistaken, you need a current row index, the index of a selected row. Yes, the API is a bit misleading. Use:

C#
myGridView.CurrentCell.RowIndex


—SA
 
Share this answer
 
Comments
Manoj K Bhoir 2-Dec-11 0:47am    
SAKryukov your code is right but the problem is that it show index when cell gets selected but op want that index should be display when row gets selected.am i wrong?
Thanks for your replies SAKryukov and Manoj K Bhoir.

My problem was in the LostFocus sub on the "search text box"

I had is set to clear the rows in the datagrid when lost focus, and display "Enter Search Text Here..." so as soon as I click datagrid, the rows were cleared. (This was nice because when you have finished searching, and you click somwhere else, all the search rows are 'nice and clean'

I removed the code from LostFocus, and replaced it in the Click and DoubleClick events of the datagrid and now it works ok.

VB
Private Sub dgvStkEnqSearchResults_DoubleClick(sender As Object, e As System.EventArgs) Handles dgvStkEnqSearchResults.DoubleClick, dgvStkEnqSearchResults.Click
        If Not IsNothing(Me.dgvStkEnqSearchResults.CurrentRow) Then
            'Get The stock code that was clicked
            Dim currentrow As Integer = dgvStkEnqSearchResults.CurrentRow.Index
            Me.txtStockEnquiry_StockCode.Text = dgvStkEnqSearchResults.Rows(currentrow).Cells("StockCode").Value

            'Reset the search box (which also clears the datagrid)
            txtStkEnqSearchText.Text = "Enter Search Text here..."
            txtStkEnqSearchText.ForeColor = Color.DimGray
        End If
    End Sub


 
    Private Sub txtStkEnqSearchText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtStkEnqSearchText.GotFocus
        If txtStkEnqSearchText.Text = "Enter Search Text here..." Then
            txtStkEnqSearchText.Text = ""
            txtStkEnqSearchText.ForeColor = Color.Black
            dvSearch.RowFilter = "CodeDesc LIKE '%" & Me.txtStkEnqSearchText.Text & "%'"
        End If
    End Sub
 
Share this answer
 
Try this Code :
VB
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        MessageBox.Show(DataGridView1.CurrentRow.Index)
        'Show Selected Row Index
End Sub

I hope it will slove your problem. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900