簡體   English   中英

DataGridView1 只顯示 class 的內容,而不是子類的內容

[英]DataGridView1 only shows the contents of the class, not of the subclass

您好,我正在學習 C#,我有一個小疑問,我有一個數組,其中包含吉他 class 和鋼琴子類的對象以及帶有 dataGridView1 的表單,但是當我執行dataGridView1.DataSource = my_array時,只有吉他 class 出現。

這里是我的課

    class guitar
{
    private string make;
    private string model;
    private int year;

    public guitar(string Make, string Model, int Year)
    {
        make = Make;
        model = Model;
        year = Year;
    }

    public string Make
    {
        get { return make; }
        set { make = value; }
    }

    public string Model
    {
        get { return model; }
        set { model = value; }
    }

    public int Year
    {
        get { return year;  }
        set { year = value; }
    }

}

子類鋼琴

    class piano : guitar
{
    private int numKeys;

    public piano(int NumKeys, string Make, string Model, int Year) :base (Make,Model,Year)
    {
        numKeys = NumKeys;
    }

    public int NumKeys
    {
        get { return numKeys; }
        set { numKeys = value; }
    }

}

我的表格

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    guitar[] my_array = new guitar[6]; 

    private void Form1_Load(object sender, EventArgs e)
    {
        my_array[0] = new guitar("Gibson", "Les Paul", 1958);
        my_array[1] = new guitar("Fender", "Jazz Bass", 1964);
        my_array[2] = new guitar("Guild", "Bluesbird", 1971);
        my_array[3] = new piano(50, "Josh", "Yellowbird", 2011);
        my_array[4] = new piano(10, "Albert", "Redbird", 9992);
        my_array[5] = new piano(20, "John", "Pinkbird", 1234);
 
        dataGridView1.DataSource = my_array;         
    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        
    }   
}

如何使用我的數組讓 dataGridView1 顯示 class 吉他和鋼琴子類的值?

注意:我使用的是 vs2015

嘗試將數據源 class 更改為最詳細的列子類。 例如,將piano class 更改為Instrument ,添加字段InstrumentType ,並將數組類型設置為Instrument

class Instrument : guitar
{
    public InstrumentType InstrumentType;
    private int? numKeys;

    public Instrument(InstrumentType instrumentType, int? NumKeys, string Make, string Model, int Year) : base(Make, Model, Year)
    {
        InstrumentType = instrumentType;
        numKeys = NumKeys;
    }

    public int? NumKeys
    {
        get { return numKeys; }
        set { numKeys = value; }
    }
}
public enum InstrumentType
{
    Piano,
    Guitar
}

// form 
Instrument[] my_array = new Instrument[6];
private void Form1_Load(object sender, EventArgs e)
{
    my_array[0] = new Instrument(InstrumentType.Guitar, null, "Gibson", "Les Paul", 1958);
    my_array[1] = new Instrument(InstrumentType.Guitar, null, "Fender", "Jazz Bass", 1964);
    my_array[2] = new Instrument(InstrumentType.Guitar, null, "Guild", "Bluesbird", 1971);
    my_array[3] = new Instrument(InstrumentType.Piano, 50, "Josh", "Yellowbird", 2011);
    my_array[4] = new Instrument(InstrumentType.Piano, 10, "Albert", "Redbird", 9992);
    my_array[5] = new Instrument(InstrumentType.Piano, 20, "John", "Pinkbird", 1234);
    dataGridView1.DataSource = my_array;
}

嘗試數據表:

            DataTable dt = new DataTable("Guitars");
            dt.Columns.Add("Make", typeof(string));
            dt.Columns.Add("Model", typeof(string));
            dt.Columns.Add("Year", typeof(int));

            dt.Rows.Add(new object[] {"Gibson", "Les Paul", 1958});
            dt.Rows.Add(new object[] {"Fender", "Jazz Bass", 1964});
            dt.Rows.Add(new object[] {"Guild", "Bluesbird", 1971});

            dataGridView1.DataSource = dt;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM