理論的には、これをどのように行うのでしょうか。
短期間:行が一貫している限り、カスタムコレクションを使用してデータテーブルのようにデータを格納し、フィールドと列の量を可変にします。
長い風:
2つまたは3つのクラス:フィールド、行、オプション:テーブル
通常、私は次のようなことを行います。List<Person> myList = new List<Person>;
次に、そのリストをdatagridviewにバインドし、列をPersonクラスのプロパティに基づいて作成します。
確認するコード:
List<row> table = new List<row>;
List<field> row0 = new List<field>;
row0.Add(new field(col1,"value1"));
row0.Add(new field(col2,"value2"));
row0.add(new field(col3,"value3"));
table.Add(row0);
dataGridView1.DataSource = table;
理論上の出力:
| |col 1 | col 2| col 3|
___________________________
|row0|value1|value2|value3|
public class cField
{
public string Name { get; set; }
public string Content { get; set; }
public cField()
{
}
public cField(string name, string content)
{
Name = name;
Content = content;
}
}
public class cRow:BindingList<cField>
{
public cRow()
{
}
}
public class tables:BindingList<cRow>
{
public tables()
{
fillTestData();
}
private void fillTestData()
{
for (Int32 i = 0; i < 10; i++)
{
cRow tRow = new cRow();
for (Int32 x=0; x < 3; x++)
{
cField f1 = new cField("ColumnName" + x.ToString(), "content" + x.ToString());
tRow.Add(f1);
}
base.Items.Add(tRow);
}
}
}
//example class which shows the output of what I'd like.
public class eField
{
public string ColumnName0 { get; set; }
public string ColumnName1 { get; set; }
public string ColumnName2 { get; set; }
public eField(string colName0, string colName1, string colName2)
{
ColumnName0 = colName0;
ColumnName1 = colName1;
ColumnName2 = colName2;
}
}
public class eTable : BindingList<eField>
{
public eTable()
{
base.Add (new eField ("content","content", "content"));
base.Add(new eField("content", "content", "content"));
base.Add(new eField("content", "content", "content"));
}
}
これがフォームのコードです。
public partial class Form1 : Form
{
tables t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
t = new tables ();
dataGridView1.DataSource = t;
dataGridView2.DataSource = t[0];
eTable table3 = new eTable ();
dataGridView3.DataSource = table3;
}
}
そのコードをプロジェクトにすると...最初のバインディングが表示されます....いくつかの組み込みのものをバインディングリストからgrid1にプルします。Grid2は、フィールドが水平方向に必要なときに、フィールドを垂直方向に一覧表示します。
グリッド3は、出力をどのようにしたいかを正確に示しています.....しかし、dataTableを模倣するコレクション構造では実現できません....(コードで提供)
免責事項:この問題を調査するために必要なキーワードが不足しています。あまり見つかりませんでした。私が見つけた最も近いものは、linqとピボットに関連していました。しかし、それらの出力のどれも私が説明したようではなかったようです。
私は至る所でカスタムコレクションを使用しているので、データテーブルを使用するのではなく、コードを非常によく似たものにしたいと思います。コレクションがこのように動作する必要があるのはこれが初めてです。