0

こんにちは、もう一度、ポインターが必要な途中で立ち往生しています.... forループでデータグリッドを埋める方法が必要です...

Dim rs As SqlCommand
Dim dt As New DataTable
For Each line As String In RichTextBox1.Lines
query = "myquery '" & RichTextBox1.Lines(i).ToString & "'"
rs = New SqlCommand(query, Module1.dtsrv_conn)
dt.load(rs.Executereader)
        If i < 1 Then
            DataGridView1.DataSource = dt                
        Else
            datagridview1.rows.Add(rs.ExecuteReader)
        End If
        i = i + 1
    Next

多くのテキスト行がRichTextBoxに入力され、各行をデータベースの値と比較する必要があり、結果のテーブルをデータグリッドに表示する必要があります...新しくクエリされた行をデータグリッドに直接追加しようとしましたが、例外がスローされたので試しました新しい行をデータテーブルに追加し、後でデータグリッドをロードする方法が見つかりません....

4

1 に答える 1

0

あまり明確ではない質問を理解した場合、ユーザーはデータベース内の行に対してクエリする値を入力しており、それらが存在する場合は、対応するデータまたはメッセージを表示します。

あなたがしたいことは、データグリッドをデータソース (私の例ではメモリ内データテーブル) にバインドし、DataGridView1.CellFormatting を介してデータバインディングをインターセプトして、それに応じてデータを比較/変更することです。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim sqlConnection As SqlConnection
    Dim sqlConnectionString As String = "YourConnectionString"
    Dim sqlDataAdapter As SqlDataAdapter
    Dim dataTable As New DataTable
    Dim sqlCommand As SqlCommand
    Dim sqlString As String
    Try
        sqlString = "SELECT * FROM YourTable" 'if user input values are filters, loop through and append them in WHERE clause
        sqlConnection = New SqlConnection(sqlConnectionString)
        sqlCommand = New SqlCommand(sqlString, sqlConnection)
        sqlConnection.Open()
        sqlDataAdapter = New SqlDataAdapter(sqlCommand)
        sqlDataAdapter.Fill(dataTable)
        sqlConnection.Close()

        DataGridView1.DataSource = dataTable

    Catch ex As Exception
        'Handle error
    End Try

End Sub

Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

    If Me.DataGridView1.Columns(e.ColumnIndex).Name = "YourColumnNameToCompare" Then
        If e.Value IsNot Nothing Then
            'do your comparison to user input text here and if necassary change the value like so..
            For Each line In RichTextBox1.Lines
                If line = e.Value Then
                    e.Value = "Value modified"
                    Exit For
                End If
            Next

            e.FormattingApplied = True
        End If
    End If
End Sub
于 2013-08-06T19:48:23.240 に答える