Home > Back-end >  Single Datagridview Master-Details, no error but only shows "(Collection)"
Single Datagridview Master-Details, no error but only shows "(Collection)"

Time:02-15

I am using SQLite and I would like to use Master-Detail in a single DataGridView. No error return, I check the Master Table ("ASM") and the Details Table ("PART") their data are good. However, my gridAsmPart only shows one row with the word (Collection) on column PART. Any help will be appreciated.

  public void LoadData()
    {
       var asm = new SQLiteDataAdapter("SELECT * FROM ASM", dB.connectionString);
       var part = new SQLiteDataAdapter("SELECT * FROM PART", dB.connectionString);

       var ds = new DataSet();

        asm.Fill(ds, "ASM");
        part.Fill(ds, "PART");

        DataRelation data_relation = new DataRelation(
            "ASM_PART",
            ds.Tables["ASM"].Columns["PART"],
            ds.Tables["PART"].Columns["PART"]);
        ds.Relations.Add(data_relation);

        gridAsmPart.DataSource = ds;
    }

Edit 1: It seems that if I use the old DataGrid, it works.

CodePudding user response:

Well… on the line of code… gridAsmPart.DataSource = ds;ds is a DataSet and when you assign a DataSet to the grid as a DataSource, then you also need to specify “which” table in the DataSet the grid should display. That is what the grids DataMember is for.

Have you tried to set the grids DataMember property to something like…?

gridAsmPart.DataSource = ds;
gridAsmPart.DataMember = “ASM”;

Or if you want the “Part” table to display….

gridAsmPart.DataSource = ds;
gridAsmPart.DataMember = “PART”;
  • Related