我三流

いろいろ自分なりにやってみたことを書いています

M$ VC# 2010 + Npgsql コンボボックスでDataGridView選択表示

かなり久々に書く。
Accessでやった時のように、フォームにコンボボックスを設置して
その値で、同じフォームに設置した帳票サブフォームのデータを
選択表示するイメージだったんだけど...

[Access]                     [VC#]
  Main_Form                     Main_Form
    Combobox                      Combobox
      Sub_Form                       DataGridView

[Access]なら
 Comboboxの更新後処理イベントで
    Dim cnn As ADODB.Connection
    Dim Rst As ADODB.Recordset
    Dim strSQL As String
    Dim FormVal As Long

    FormVal = Nz([Forms]![Main_Form]![Combobox], 0)

    strSQL = "SELECT t1.A, t1.分類ID " & _
            "FROM テーブル As t1 WHERE t1.分類ID = " & FormVal & " ORDER BY t1.A;"

    Set cnn = CurrentProject.Connection

    With Rst
        .Open strSQL, cnn, adOpenStatic, adLockOptimistic
            Do Until .EOF
                !A = .A
                     .MoveNext
            Loop    
    End With

  cnn.Close: Set cnn = Nothing
    
  Set Rst = Nothing

てな具合で(しばらくぶりで書いてるから間違ってるかも)
簡単に表示させることができるんだが

[VC#]の場合

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;
using System.Data.SqlClient;

namespace PostgreSQLTEst
{
    public partial class FormRecords : Form
    {
        // メンバ変数
        // データベース接続子
        private NpgsqlConnection m_conn = new NpgsqlConnection
        (
              "Server=192.168.0.1;"
            + "Port=5432;"
            + "User Id=oresama;"
            + "Password=oresikawakaran;"
            + "Database=oresamadb;"
            + "Encoding=UNICODE"
        );

        // recordsのデータアダプタ
        private NpgsqlDataAdapter da = new NpgsqlDataAdapter();
        // sectorのデータアダプタ
        private NpgsqlDataAdapter s_id_da = new NpgsqlDataAdapter();
        // numbersのデータアダプタ
        private NpgsqlDataAdapter n_id_da = new NpgsqlDataAdapter();

        // データセット
        private DataSet ds = new DataSet();
        private DataSet Newds = new DataSet();
 
        // コンストラクタ
        public FormRecords()
        {
            InitializeComponent();
        }

        // フォームロードイベント
        private void FormRecords_Load(object sender, EventArgs e)
        {

            //recordsのデータアダプタ構築
            //selectコマンド
            da.SelectCommand = new NpgsqlCommand
            (
                   "select"
                + " r_id"
                + ", id"
                + ", s_id"
                + ", results"
                + ", flag"
                + ", time_stamp"
                + ", n_id"
                + " from"
                + " records"
                + " where n_id=:n_id"
                + " order by r_id",
                m_conn
            );
            if (cmb_n_id.SelectedItem == null)
            {
                // SelectedItemがない場合
                da.SelectCommand.Parameters.Add(new NpgsqlParameter("n_id",
                NpgsqlTypes.NpgsqlDbType.Integer, 0, "n_id",
                ParameterDirection.Input, false, 0, 0, DataRowVersion.Current,
                DBNull.Value));
            }
            else
            {
                // SelecteItemがある場合
                DataRowView row = (DataRowView)cmb_n_id.SelectedItem;
                da.SelectCommand.Parameters.AddWithValue("n_id", row["n_id"]);
            }

            // データセット生成
            da.Fill(ds, "records");
            n_id_da.Fill(ds, "numbers");

           // bindingSourceRecordsを設定
            bindingSourceRecords.DataSource = ds;
            bindingSourceRecords.DataMember = "records";

            bindingNavigatorRecords.BindingSource = bindingSourceRecords;
            dataGridViewRecords.AutoGenerateColumns = false;
            dataGridViewRecords.DataSource = bindingSourceRecords;

            // カラムを関連付け
            r_id.DataPropertyName = "r_id";
            id.DataPropertyName = "id";
            s_id.DataPropertyName = "s_id";
            results.DataPropertyName = "results";
            flag.DataPropertyName = "flag";
            time_stamp.DataPropertyName = "time_stamp";
            // コンボボックスcmb_n_id用
            cmb_n_id.DataSource = ds.Tables["numbers"];
            cmb_n_id.DisplayMember = "number";
            cmb_n_id.ValueMember = "n_id";
            Cn_id.DataPropertyName = "n_id";

        }
        private void cmb_n_id_SelectedIndexChanged(object sender, EventArgs e)
        {

            da.SelectCommand = new NpgsqlCommand
            (
                    "select"
                + " r_id"
                + ", id"
                + ", s_id"
                + ", results"
                + ", flag"
                + ", time_stamp"
                + ", n_id"
                + " from"
                + " records"
                + " where n_id=:n_id"
                + " order by r_id",
                m_conn
            );
            if (cmb_n_id.SelectedItem == null)
            {
                // SelectedItemがない場合
                da.SelectCommand.Parameters.Add(new NpgsqlParameter("n_id",
                NpgsqlTypes.NpgsqlDbType.Integer, 0, "n_id",
                ParameterDirection.Input, false, 0, 0, DataRowVersion.Current,
                DBNull.Value));
            }
            else
            {
                // SelecteItemがある場合
                DataRowView row = (DataRowView)cmb_n_id.SelectedItem;
                da.SelectCommand.Parameters.AddWithValue("n_id", row["n_id"]);
            }

            // データセット生成
            if (Newds.Tables["records"] != null)
                Newds.Tables["records"].Clear();
            da.Fill(Newds,"records");

            // bindingSourceRecordsを設定
            bindingSourceRecords.DataSource = Newds;
            bindingNavigatorRecords.BindingSource = bindingSourceRecords;

            dataGridViewRecords.AutoGenerateColumns = false;
            dataGridViewRecords.DataSource = bindingSourceRecords;

        }


 〜〜〜あと、省略〜〜〜

初心者には敷居が高すぎて
うわぁ〜、メンドくせーってなるわ。

でも、表示できてよかったわ。
これの更新処理(UPADTE)とかでもっと躓いたけど...