tagCANDY CGI VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
VBレスキュー(花ちゃん) の Visual Basic 2010 用 掲示板(VB.NET 掲示板)
[ツリー表示へ]  [ワード検索]  [Home]

タイトル Re^12: DataGridViewにカーソルを位置づけたい
投稿日: 2008/08/25(Mon) 10:56
投稿者不二子
何度もありがとうございます。
私も関連リンクを探していると下記のページを見つけました。
http://support.microsoft.com/default.aspx?scid=kb;ja;310128

そこで、FillSchemaを早速使用してみたんですが、なぜか"テーブルに主キーがありません"
とエラーになりました。
MissingSchemaAction プロパティに AddWithKey 列挙を使用下場合は、このエラーにはなりませんでしたが、findRow できませんでした。
記述方法が悪いのでしょうか?

もう一度コーディングを記述させていただきます。
<フォームのDataGridViewにデータを表示する時>
    Private Sub BTN_表示ボタン_Click(ByVal sender As System.Object, _
                                     ByVal e As System.EventArgs) Handles BTN_検索ボタン.Click
        Dim strsql As String
        Try
            '---抽出クエリ作成
            strsql = "SELECT"
            strsql = strsql & " 社員コード,"
            strsql = strsql & " 社員氏名,"
            strsql = strsql & " 社員カナ"
            strsql = strsql & " FROM T社員テーブル
            '---SQL文を作成して実行する
            Dim comm As SqlCommand = New SqlCommand(strsql, Con)
            '---SQL文を引数にしてデータアダプタを生成
            Dim dataadapter As SqlDataAdapter = New SqlDataAdapter(comm)
            
            '''---■DataSet オブジェクトの更新や検証に必要な追加のスキーマを取得する
            '''dataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

            '---DataAdapter で取得するデータの受け皿を準備
            Dim ds As DataSet = New DataSet()

            '---■DataSet にスキーマを格納
            dataadapter.FillSchema(ds, SchemaType.Source, "社員コード")

            '---SQL 文を発行し、取得したデータを DataSet に格納
            dataadapter.Fill(ds, "社員テーブル")
            '---DataSet を DataGridView control と連結
            dgrSyain.DataSource = ds
            '---データソースの名前を設定
            dgrSyain.DataMember = "社員テーブル"
        Catch oExcept As Exception
            '例外が発生した時の処理
            MessageBox.Show(oExcept.ToString, "例外発生")
            Exit Sub
        End Try
    End Sub

<氏名カナで検索する時>
    Private Sub TXT_検索_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TXT_検索.KeyDown
        Dim strsql As String
        Dim strCode As String
        If e.KeyCode = Keys.Enter Then
            Try
                Dim command As New SqlClient.SqlCommand
                '---コネクションの指定
                command.Connection = Con
                '---コマンドの種類をテキストにする(省略可)
                command.CommandType = CommandType.Text
                '---実行するSQLを指定
                strsql = "SELECT"
                strsql = strsql & " 社員コード"
                strsql = strsql & " FROM T社員テーブル"
                strsql = strsql & " WHERE 社員カナ Like '" & TXT_検索.Text & "%" & "'"
                command.CommandText = strsql
                '---SQLの結果を取得する
                Dim sr As SqlClient.SqlDataReader
                sr = command.ExecuteReader()
                command.Dispose()
                '---取得データ0件の場合は
                If Not sr.HasRows Then
                    MessageBox.Show("該当データがありません")
                    Exit Try
                Else
                    While sr.Read
                        strCode = sr("社員コード").ToString()
                    End While
                End If
                sr.Close()
                '---DataTable 内のデータ行取得
                Dim findRow As DataRow = _
                    DirectCast(dgrSyain.DataSource, DataSet).Tables("社員テーブル").Rows.Find("& strCode &")
                '---カーソルの位置付け
                For Each dgvRow As DataGridViewRow In dgrSyain.Rows
                    Dim row As DataRow = DirectCast(dgvRow.DataBoundItem, DataRowView).Row
                    If row Is findRow Then
                        dgvRow.Selected = True
                        Return
                    End If
                Next
            Catch oExcept As Exception
                '例外が発生した時の処理
                MessageBox.Show(oExcept.ToString, "例外発生")
            End Try
        End If
    End Sub

- 関連一覧ツリー をクリックするとツリー全体を一括表示します)

古いスレッドにレスはつけられません。