[英]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.