C# 開発

【C#】DataGridView : データの表示

2025.01.20 DataGrdiView : データを表示する

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.ColumnCount = 2;
    dataGridView1.Rows.Add(new string[] { "A001", "Apple" });
    dataGridView1.Rows.Add(new string[] { "B001", "Orange" });
    dataGridView1.Rows.Add(new string[] { "C001", "Grape" });
    dataGridView1.Rows.Add(new string[] { "D001", "Banana" });
    dataGridView1.Rows.Add(new string[] { "E001", "Lemon" });
}

2025.01.20 DataGrdiView : データを表示する

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.ColumnCount = 2;
    dataGridView1.Rows.Add(["A001", "Apple"]);
    dataGridView1.Rows.Add(["B001", "Orange"]);
    dataGridView1.Rows.Add(["C001", "Grape"]);
    dataGridView1.Rows.Add(["D001", "Banana"]);
    dataGridView1.Rows.Add(["E001", "Lemon"]);
}

DataGridView : データを表示する

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.ColumnCount = 3;
    var row = new DataGridViewRow();

    row.CreateCells(dataGridView1);
    row.Cells[0].Value = "0001";
    row.Cells[1].Value = "渋谷区";
    row.Cells[2].Value = "ハチ公";
   
    dataGridView1.Rows.Add(row);
}

DataGridView : データセットを設定とバインディングソースの設定

private void Form1_Load(object sender, EventArgs e)
{
    // データセットの作成
    var ds = new DataSet();
    var dt = ds.Tables.Add("Computer");

    dt.Columns.Add("メーカー");
    dt.Columns.Add("パソコン名");

    dt.Rows.Add(new string[] { "NEC", "ValueStar" });
    dt.Rows.Add(new string[] { "SONY", "VAIO" });
    dt.Rows.Add(new string[] { "DELL", "Precision" });

    // バインディングソースの設定
    bindingSource1.DataSource = ds;
    bindingSource1.DataMember = "Computer";
    dataGridView1.DataSource = bindingSource1;

    button1.Text = "前の行";
    button2.Text = "次の行";
}

private void button1_Click(object sender, EventArgs e)
{
    bindingSource1.MovePrevious();
}

private void button2_Click(object sender, EventArgs e)
{
    bindingSource1.MoveNext();
}

DataGridView : データセットを設定とバインディングソースの設定

private void Form1_Load(object sender, EventArgs e)
{
    // データグリッドにバインディングソースを設定しています。
    // バインディングソースには事前に作成したデータセットが割り当てられています。
    // 但し、データグリッドビューの AutoGenerateColumns を false を設定していることにより、
    // 列の自動追加は行っていません。通常はテキストボックスカラムが割り当てられます。
    // 列はテキストボックスカラムとコンボボックスカラムを別に作成してからデータグリッドビューに追加しています。
    // 特にコンボボックスカラムは、リストから選択ができるように別のバインディングソースを作成してリスト項目を割り当てています。
    // ボタンの操作によりカレント行を移動できます。

    var ds = new DataSet();
    var dt = ds.Tables.Add("Computer");

    dt.Columns.Add("メーカー");
    dt.Columns.Add("パソコン名");

    dt.Rows.Add(new string[] { "NEC", "ValueStar" });
    dt.Rows.Add(new string[] { "SONY", "VAIO" });
    dt.Rows.Add(new string[] { "DELL", "Precision" });

    bindingSource1.DataSource = ds;
    bindingSource1.DataMember = "Computer";

    dataGridView1.AutoGenerateColumns = false;
    dataGridView1.DataSource = bindingSource1;

    var col1 = new DataGridViewTextBoxColumn();
    col1.DataPropertyName = "メーカー";
    col1.HeaderText = "メーカー";
    dataGridView1.Columns.Add(col1);

    var bs = new BindingSource();
    bs.Add("ValueStar");
    bs.Add("VAIO");
    bs.Add("Precision");

    var col2 = new DataGridViewComboBoxColumn();
    col2.DataPropertyName = "パソコン名";
    col2.HeaderText = "パソコン名";
    col2.DataSource = bs;
    dataGridView1.Columns.Add(col2);
}

private void button1_Click(object sender, EventArgs e)
{
    bindingSource1.MovePrevious();
}

private void button2_Click(object sender, EventArgs e)
{
    bindingSource1.MoveNext();
}

-C# 開発