简体   繁体   English

从 DataGridView 获取当前单元格值

[英]Getting Current Cell value from DataGridView

I am trying to get current cell value of a DataGridView.我正在尝试获取 DataGridView 的current cell value This current cell value is the product name .此当前单元格值为product name As the user types the product name I want to populate the Unit on its own.当用户键入the product name我想自己填充Unit So I need to get the current cell value so that I can pass it to database and get the expected unit for the product.所以我需要获取当前的单元格值,以便我可以将它传递给数据库并获得产品的预期单位。 I used a variable ( Product ) to store the current cell value and whenever I debug the code this variable contains nothing .我使用了一个变量 ( Product ) 来存储当前的单元格值,每当我调试代码时,这个变量nothing包含nothing

I am Putting my code below for you to Get an idea我把我的代码放在下面给你一个想法

Private Sub dataGridView2_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView2.CellValidating
    If e.ColumnIndex = 1 Then

        Dim product As String = DataGridView2.CurrentCell.Value
        Dim rowind As Integer = DataGridView2.CurrentCell.RowIndex
        Dim columnind As Integer = DataGridView2.CurrentCell.ColumnIndex
        Using ConnectionString As New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;")
            Dim strSQL As String = ("SELECT Unit from b_productMst where productname =" + product)
            Dim SQLcommand As New SqlCommand(strSQL, ConnectionString)
            ConnectionString.Open()
            Dim reader As SqlDataReader
            reader = SQLcommand.ExecuteReader()
            DataGridView2.Rows(rowind).Cells(columnind) = reader.Item(0)
        End Using
        End If
    End Sub

I even tried using我什至尝试使用

 product = DataGridView2.Rows(rowind).Cells(columnind).Value

but of no use.但没有用。

I have mentioned my goal above.我已经在上面提到了我的目标。 Please let me know if there is any other way to achieve this.请让我知道是否还有其他方法可以实现这一目标。

Don't use the name of the class as the name of your variable.不要使用类的名称作为变量的名称。 As in, don't Dim SqlCommand As New SqlCommand .就像,不要将Dim SqlCommand As New SqlCommand

The command and the reader both need using blocks.命令和阅读器都需要使用块。 (They expose .Dispose methods). (它们公开 .Dispose 方法)。 The command is included in the same block as the connection.该命令与连接包含在同一块中。 Note the comma at the end of the first Using line.请注意第一个 Using 行末尾的逗号。

Always use parameters to avoid Sql Injection.始终使用参数来避免 Sql 注入。 I had to guess at the datatype and field size.我不得不猜测数据类型和字段大小。 Check you database for the correct values and adjust the code accordingly.检查您的数据库是否有正确的值并相应地调整代码。

The DataReader starts before the first record. DataReader 在第一条记录之前启动。 You need to call .Read to move to the first record.您需要调用 .Read 移动到第一条记录。

Private Sub dataGridView1_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If e.ColumnIndex = 1 Then
        Dim product As String = DataGridView1.CurrentCell.Value.ToString 'Value is an Object
        Dim rowind As Integer = DataGridView1.CurrentCell.RowIndex
        Dim columnind As Integer = 1 'The If block code wouldn't be executing unless the ColumnIndex = 1
        Using Con As New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;"),
            cmd As New SqlCommand("SELECT Unit from b_productMst where productname = @Product;", Con)
            cmd.Parameters.Add("@Product", SqlDbType.VarChar, 100).Value = product
            Con.Open()
            Using reader = cmd.ExecuteReader()
                reader.Read()
                DataGridView1.Rows(rowind).Cells(columnind + 1).Value = reader.Item(0).ToString
            End Using
        End Using
    End If
End Sub

Unless the the b_productMst table is huge, I would far prefer to see all the data downloaded in the Form.Load and stored in memory as a DataTable or Dictionary.除非 b_productMst 表很大,否则我更希望看到所有在 Form.Load 中下载并作为 DataTable 或 Dictionary 存储在内存中的数据。 This would avoid repeated hits on the database.这将避免对数据库的重复命中。

Private dt As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Using Con As New SqlConnection("data source=ADMIN-PC\SQLEXPRESS; database=billdev;Trusted_Connection=yes;"),
            cmd As New SqlCommand("SELECT productname, Unit from b_productMst; ", Con)
        Con.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
End Sub

Private Sub dataGridView1_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    If e.ColumnIndex = 1 Then
        Dim product As String = DataGridView1.CurrentCell.Value.ToString 'Value is an Object
        Dim rowind As Integer = DataGridView1.CurrentCell.RowIndex
        Dim columnind As Integer = 1 'The If block code wouldn't be executing unless the ColumnIndex = 1
        DataGridView1.Rows(rowind).Cells(columnind + 1).Value = dt.Select("productname = product")(0)(2)
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更新 Datagridview 单元格值? - How to Update the Datagridview Cell value? 根据条件更新datagridview单个单元格值 - Update datagridview single cell value based on a condition Datagridview单元格选定的值插入到sqlserver - Datagridview Cell selected value insert into sqlserver C#使用存储过程从datagridview删除多个选择每次都传递相同的单元格值 - C# delete multiple selection from datagridview using stored procedure pass the same cell value each time 在 DataGridView c# 中更改单元格的值类型/值 - Changing the value type/value of a cell in DataGridView c# 使用嵌套的 for 循环从 Wpf 框架中的 DataGrid 单元格获取值 - Getting value from a DataGrid cell in Wpf framework with a nested for loop 如何根据dataGridView中的另一个列值自定义DataGridViewButton单元格 - How to customize DataGridViewButton cell according to the another column value in a dataGridView 在C#中使用SqlCommand更新datagridview单元格值 - Update datagridview cell value using SqlCommand in C# SQL:通过计算当前日期时间获取分钟值-表中的日期时间值 - SQL: Getting the minute value from calculating current datetime - datetime value from table 如果以前的单元格值和当前的单元格值在SQL Server中相同,则选择空 - Select empty if previous cell value and current cell value is same in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM