无法从Datagridview Click事件获取行号 [英] Cant get row number from Datagridview Click event

查看:90
本文介绍了无法从Datagridview Click事件获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有搜索功能.搜索框中的文本会修改数据视图行过滤器.

当搜索文本"文本框失去焦点时,我将重置行过滤器.

我要尝试做的是,如果用户单击数据网格中显示搜索结果的单元格,则将使用该行中的值填充其他字段.

我遇到的问题是rowIndex始终返回零.

当搜索文本框失去焦点时,我需要捕捉被单击的行号并获取股票代码,然后行过滤器将由Lost Focus Sub重置.

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.

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

推荐答案

如果我没有记错的话,则需要当前行索引,即选定行的索引.是的,API有点误导.用途:

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:

myGridView.CurrentCell.RowIndex



—SA



—SA


感谢您的回复SAKryukov和Manoj K Bhoir.

我的问题是在搜索文本框"上的LostFocus子目录中

我已设置为在失去焦点时清除datagrid中的行,并显示在此处输入搜索文本...",因此,一旦我单击datagrid,就将其清除. (这很好,因为当您完成搜索后,单击其他位置,所有搜索行都干净整洁"

我从LostFocus中删除了代码,并将其替换为datagrid的Click和DoubleClick事件,现在可以正常使用了.

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.

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


尝试以下代码:
Try this Code :
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. :)


这篇关于无法从Datagridview Click事件获取行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆