2016-09-06 120 views
0

在保存數據之前,我需要幫助來篩選或檢查一個或多個datagridview單元是否爲空。我嘗試了幾個代碼,但總是有錯誤。下面是圖片:篩選或檢查datagridview

enter image description here

回答

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