Home > Software engineering >  DataGridView1 only shows the contents of the class, not of the subclass
DataGridView1 only shows the contents of the class, not of the subclass

Time:02-04

Hello I'm learning C# and I'm having a small doubt, I have an array that contains the objects of the guitar class and the piano subclass and a form with dataGridView1, but when I do dataGridView1.DataSource = my_array only the data of the guitar class appears.

Here my classes

    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; }
    }

}

Subclass piano

    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; }
    }

}

My form

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)
    {
        
    }   
}

How can I make dataGridView1 show the values of class guitar and subclass piano using my array?

Note: I am using vs2015

CodePudding user response:

Try to change data source class to the most detail columns subclass. For example, change piano class to Instrument, add a field InstrumentType, and set the array type to 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;
}

CodePudding user response:

Try a DataTable :

            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;
  •  Tags:  
  • Related