4

DataGridView と、表示したいオブジェクトのリストがあります。

オブジェクトは次のとおりです。

public class Entity
{
    public int ID { get; set; }
}    

public class Travel: Entity
{
    public Service Service { get; set; }
    public City Source { get; set; }
    public City Destiny { get; set; }
    public decimal Price { get; set; }
}

public class Service: Entity
{
    public string Name { get; set; }
}

public class City: Entity
{
    public string Name { get; set; } // Max 50 chars
}

私のフォームでは、次のように Travel オブジェクトのリストをバインドします。

List<Travel> travels = logic.GetAllTravels();
DgvRecorridos.DataSource = travels;

そして、私は次のようになります:

ここに画像の説明を入力

代わりに、サービスの名前、ソース シティ、デスティニー シティを取得したいと思います。

前もって感謝します。

4

4 に答える 4

5

以下のコードを実行する代わりに:

List<Travel> travels = logic.GetAllTravels();  
DgvRecorridos.DataSource = travels;  

これを行う:

List<Travel> travels = logic.GetAllTravels();  
BindingSource bs = new BindingSource();  
bs.DataSource = travels;  
DgvRecorridos.AutoGenerateColumn = false;  
DgvRecorridos.DataSource = bs;  

次に、列を手動で追加します。

DataGridViewColumn col1 = new DataGridViewTextBoxColumn();  
col1.DataPropertyName = "Service.Name";  
col1.HeaderText = "Service Name";  
dataGridView1.Columns.Add(col1);  

DataGridViewColumn col2 = new DataGridViewTextBoxColumn();  
col2.DataPropertyName = "City.Name";  
col2.HeaderText = "City Name";  
dataGridView1.Columns.Add(col2);  

DataGridViewColumn col3 = new DataGridViewTextBoxColumn();  
col3.DataPropertyName = "City.Name";  
col3.HeaderText = "Destiny Name";  
dataGridView1.Columns.Add(col3);  

DataGridViewColumn col4 = new DataGridViewTextBoxColumn();  
col4.DataPropertyName = "Price";  
col4.HeaderText = "Price";  
dataGridView1.Columns.Add(col4);  

次に、DataGridView のセル書式設定イベント ハンドラーを追加します。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
{  
    if (dataGridView1.Rows[e.RowIndex].DataBoundItem != null &&   
        dataGridView1.Columns[e.ColumnIndex].DataPropertyName.Contains("."))  
    {  
        e.Value = BindProperty(dataGridView1.Rows[e.RowIndex].DataBoundItem,
            dataGridView1.Columns[e.ColumnIndex].DataPropertyName);  
    }  
}  

private string BindProperty(object property, string propertyName)  
{  
    string retValue = "";  

    if (propertyName.Contains("."))  
    {  
        PropertyInfo[] arrayProperties;  
        string leftPropertyName;  

        leftPropertyName = propertyName.Substring(0, propertyName.IndexOf("."));  
        arrayProperties = property.GetType().GetProperties();  

        foreach (PropertyInfo propertyInfo in arrayProperties)  
        {  
            if (propertyInfo.Name == leftPropertyName)  
            {  
                retValue = BindProperty(propertyInfo.GetValue(property, null),   
                propertyName.Substring(propertyName.IndexOf(".") + 1));  
                break;  
            }  
        }  
    }  
    else  
    {  
        Type propertyType;  
        PropertyInfo propertyInfo;  

        propertyType = property.GetType();  
        propertyInfo = propertyType.GetProperty(propertyName);  
        retValue = propertyInfo.GetValue(property, null).ToString();  
    }  

    return retValue;  
}  

セルの書式設定の完全なガイドについては、Antonio Bello のブログを参照してください。^_^ 私もここで 2 日前に同じ質問をしましたが、あなたと同じ答えが返ってきました。お役に立てば幸いです。

于 2014-10-23T08:30:29.067 に答える