检查数据网格视图中是否有内容 [英] Check to see if something is in a datagrid view

查看:61
本文介绍了检查数据网格视图中是否有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

检查数据视图中是否有2个项目的最佳方法是什么。用户可以将产品密钥和用户ID输入两个单独的文本框中。如果文本框中的两个项目匹配并且它们位于同一行,则激活程序
并继续登录。 (登录将仅使用用户名和密码以相同的方式工作)。最好用什么代码来做这件事?另请注意,datagrid视图中的信息来自数据库。

What would be the best method to check to see if 2 items are in a dataview. The user would input a product key and user id into two seperate textboxes. If the two items that are in the text box, match and they are on the same row then activate the program and proceed to the login. (the login will work the same way only with a username and password). What code would be the best to use to do this? Please also note that the information in the datagrid view is taken from a database.

推荐答案

以下是执行此操作的众多方法之一。这不太可能是"最佳方式",但理解起来非常简单。询问10个编码器,这是"最佳方式",你可能会得到两个不同的答案。

Here is one of many ways to do this. It is unlikely that this is the 'best way', but it is very straightforward to understand. Ask 10 coders which is the 'best way' and you would likely get different answers from both of them.

这是一个测试示例,是一个独立的项目。

This is a test example and is a stand alone project.

' Form1 with blank DataGridView1
' and TextBox1 and TextBox2
Option Strict On
Option Explicit On
Class Form1
  Dim myTable As New DataTable("Freddy")
  Dim BS As New BindingSource
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' some test data
    With myTable
      .Columns.Add("Product Key")
      .Columns.Add("User ID")

      .Rows.Add("ABC-99", 1)
      .Rows.Add("DEF-199", 2)
      .Rows.Add("ZZY-999", 3)
      .Rows.Add("CAB-11", 4)
      .Rows.Add("FGH-111", 5)
    End With
    BS.DataSource = myTable
    DataGridView1.DataSource = BS
  End Sub
  Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged, TextBox1.TextChanged
    Dim r As Integer = -1
    For Each dr As DataRow In myTable.Rows
      r += 1
      If dr.Item(0).ToString = Trim(TextBox1.Text) AndAlso dr.Item(1).ToString = Trim(TextBox2.Text) Then
        ' CONTINUE APPLICATION
        ' CREDENTIALS MATCH
        MessageBox.Show("USER: " & dr.Item(1).ToString & " match found.")
        Exit For
      End If
    Next
  End Sub
End Class


这篇关于检查数据网格视图中是否有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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