あなたはこのような多くの子要素を使用したい場合:
class MyClass
{
public int Id;
public MyOtherClass OtherClass;
}
class MyOtherClass
{
public string Name;
public int Number;
}
方法について:いくつかのイベントでは各セルの
第一ソリューション 設定値を(mabye別の1が優れています)手動でデータソースを設定した後、たとえば:
private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
MyClass data = dgv.Rows[ e.RowIndex ].DataBoundItem as MyClass;
dgv.Rows[ e.RowIndex ].Cells[ "colName" ].Value = data.OtherClass.Name;
dgv.Rows[ e.RowIndex ].Cells[ "colNumber" ].Value = data.OtherClass.Number;
}
2番目のソリューション データから適切なDataTableを作成してバインドするのはどうですか?
:あなたは(DataPropertyName = "MyProp1.MyProp2.MyProp3"
を使用してIE)より汎用的にする必要がある場合
私はあなたがこの
private void Grid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewColumn column = Grid.Columns[e.ColumnIndex];
if (column.DataPropertyName.Contains("."))
{
object data = Grid.Rows[e.RowIndex].DataBoundItem;
string[] properties = column.DataPropertyName.Split('.');
for (int i = 0; i < properties.Length && data != null; i++)
data = data.GetType().GetProperty(properties[i]).GetValue(data);
Grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = data;
}
}
あなたはDataPropertyNameが何をしたいですか?あなたはValueのラインに沿って何かを探しているようです。私はあなたがASP.Netで作業していないと思っています。これは私がよく知っていることですが、DataGridViewTextBoxColumnの他のプロパティを調べて、必要なものがあるかどうか確認します。 – Melanie
@Melanie私はWindowsフォームを使用しています。私は他のプロパティを見てみる – Andrey
また、別のオブジェクトのプロパティを使用すると、そのような:DataPropertyName = "MasterName" – Andrey