DataTable にバインドされた DataGridView があります。
後で新しいボタン列を DGV に直接追加します。次回テーブルを更新するときに、DGV から以前のデータをすべて消去したいと考えています。
テーブルの場合、私はvar table = new DataTable();
これを行いますが、DGV がメソッド内でローカルとして定義されている場合に DataGridView でこれを行うと、フォームに表示されなくなります。
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//dataGridView1 = new DataGridView(); //<-- uncommenting this line breaks the form's dgv from displaying anything
DataTable table = new DataTable();
table.Columns.Add("C_" + table.Columns.Count);
table.Rows.Add("R1");
table.Rows.Add("R2");
dataGridView1.DataSource = table;
DataGridViewButtonColumn oCol = new DataGridViewButtonColumn();
oCol.Name = "Buttons";
oCol.Text = "(...)";
oCol.UseColumnTextForButtonValue = true;
dataGridView1.Columns.Add(oCol);
}
}
}
これはバグですか、それとも dgv を適切に更新/リセット/クリアするにはどうすればよいですか?
編集:
上記のコード スニペットは、オリジナルから編集されています。コード内の行のコメントを外して、RunMode のときの button1 の異なる動作を確認します。