none
データバインドされた Windows フォーム DataGridView コントロールに非バインド列(テキスト列)を追加する RRS feed

  • 質問

  • オンラインヘルプを参考に非バインド列(テキスト)を追加してみようとしましたが
    実行エラーが発生しました

      Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            mdb_file_台帳 = "\\--台帳.mdb"
            provider_台帳 = "Provider=--"
            provider_台帳 += mdb_file_台帳

            Dim CommandText As String

            'SQLのSELECT文を作成
            CommandText = "SELECT * FROM T001台帳"

            '以上の設定からデータアダプターを生成
            Dim Adapter As New System.Data.OleDb.OleDbDataAdapter
            Adapter = New System.Data.OleDb.OleDbDataAdapter(CommandText, provider_台帳)

            '▼データ読込

            Dim Table As New DataTable()
            Adapter.Fill(Table)

            '▼DataGridViewにデータを表示

            Dim BindingSource1 As New BindingSource
            BindingSource1.DataSource = Table
            DataGridView1.DataSource = BindingSource1

            '▼ボタン列を追加
            Dim ButtonColumn As New DataGridViewButtonColumn
            With ButtonColumn
                .Name = "詳細"            '列の名前。プログラム中で使用する。
                .HeaderText = "詳細"    '列の見出しに表示するテキスト
                .Text = "詳細"               'ボタンに表示するテキスト
                .UseColumnTextForButtonValue = True
            End With
            DataGridView1.Columns.Insert(0, ButtonColumn)

    '--------------追加 start----------------

        ’テキスト列
            Dim add_column As New DataGridViewColumn
            With add_column
                .Name = "add"
                .HeaderText = "add"
            End With
            '   DataGridView1.Columns.Insert(2, add_column) <---error

    '--------------追加 end----------------

        End Sub

    End Class


    エラー内容は
     InvalidOpretionException はハンドルされませんでした
     列の CellType プロパティが null であるため、列を追加することはできません。

    サンプルのコマンドボタンは正常に表示されます

     いろいろ調べましたが、解決できません
     ご指導お願い致します。

    また、もし御存知でしたら、チェックボックスの追加もご教授ください。


     

    2007年2月26日 4:36

すべての返信

  • DataGridViewColumnは空のテンプレートの列だと思ってください。

    CellTemplateにDataGridViewTextBoxCellなどを指定するか、最初からDataGridViewTextBoxColumnをAddしてください。チェックボックスはDataGridViewCheckBoxColumnがあります。

    2007年2月26日 5:58
  •  

    おっちい様、早速のご教授ありがとうございます。
     
          Dim columnX As New DataGridViewTextBoxColumn
          columnX.HeaderText = "__列目の見出し"
          DataGridView1.Columns.Add(columnX)


      で試してみました。

      テキスト列が追加されましたが、DataGridViewの一番右欄に追加されました。

      調べてみましたが、列を指定するを見つけることができませんでした

      列を指定して、テキスト欄を追加することはできないでしょうか?

    2007年2月26日 9:33
  • あ、私がAddしてください、と書いたのが悪かったか。

    最初にizendさんが書かれてたように、Insertでもいけますよ。

    2007年2月26日 9:42
  •  

    おっちい様、速攻のご教授ありがとうございます。
     
            Dim columnX As New DataGridViewTextBoxColumn
            columnX.HeaderText = "2列目の見出し"
            DataGridView1.Columns.Insert(2, columnX)

        で、意図している結果が出ました。

        誠にありがとうございました。

    2007年2月26日 9:54