过滤或检查datagridview

时间:2016-09-06 07:00:41

标签: vb.net

在保存数据之前过滤或检查一个或多个datagridview单元格是否为null时,我需要帮助。我尝试了几个代码,但始终存在错误。以下是图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定你要做什么。也许你想要某种自动完成过程来运行。是吗?请参阅下面的代码示例。

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Data.SqlClient

Namespace WindowsFormsApplication4

    Public Class Form1
        Inherits Form

        Public Sub New()
            MyBase.New
            InitializeComponent
            Me.initializeFields
            Me.loadCustomerFromNorthwindDbToDgv1
        End Sub

        Private CustomerId As DataGridViewTextBoxColumn

        Private CompanyName As DataGridViewTextBoxColumn

        Private Address As DataGridViewTextBoxColumn

        Private Sub initializeFields()
            Me.CustomerId = New DataGridViewTextBoxColumn
            Me.CustomerId.Name = "CustomerID"
            Me.CustomerId.DataPropertyName = "CustomerID"
            Me.dataGridView1.Columns.Add(Me.CustomerId)
            Me.CompanyName = New DataGridViewTextBoxColumn
            Me.CompanyName.Name = "CompanyName"
            Me.CompanyName.DataPropertyName = "CompanyName"
            Me.dataGridView1.Columns.Add(Me.CompanyName)
            Me.Address = New DataGridViewTextBoxColumn
            Me.Address.Name = "Address"
            Me.Address.DataPropertyName = "Address"
            Me.dataGridView1.Columns.Add(Me.Address)
        End Sub

        Private dv As DataView

        Private Sub loadCustomerFromNorthwindDbToDgv1()
            Dim conConnect As SqlConnection = New SqlConnection("Data Source = EXCEL-PC; Database = 'Northwind.MDF'; Integrated Security = true")
            Try 
                Dim dAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName, Address FROM Customers ", conConnect)
                Dim DS As DataSet = New DataSet
                Dim bs As BindingSource = New BindingSource
                dAdapter.Fill(DS, "dsCustomer")
                Me.dataGridView1.AutoGenerateColumns = false
                Dim dt As DataTable = DS.Tables("dsCustomer")
                bs.DataSource = DS.Tables("dsCustomer")
                Me.dv = New DataView(DS.Tables("dsCustomer"))
                Me.dataGridView1.DataSource = bs
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

        End Sub

        'will do the search for CompanyName name everytime you type in @ the textbox search
        Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
            Me.dv.RowFilter = ("CompanyName like '%' + '"  _
                        + (textBox1.Text + "' + '%' "))
            Me.dataGridView1.DataSource = Me.dv
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
            Me.customersTableAdapter.Fill(Me.northwindDataSet.Customers)
        End Sub
    End Class
End Namespace