Home Forums WinForms controls Xceed Grid for WinForms DetailGridTemplates and its View Reply To: DetailGridTemplates and its View

Xceed Support
Member
Post count: 5658

Hi Saumya,

When adding DetailGrids at run-time, it is important to set the DataRelation so that the Grid understands how to place it correctly. The following code demonstrates a “best practice” on how to add DetailGrids at runtime. You will see in the code that the two DataTables are linked by ID which acts as a Foreign Key.

————

#region Create Master DataTable / Fill Master DataTable

      System.Data.DataTable dSource = new System.Data.DataTable(“Shows”);
   
      dSource.Columns.Add(“ID”,typeof(int));
      dSource.Columns.Add(“TV Show”,typeof(string));
      dSource.Columns.Add(“TV Station”,typeof(string));

      System.Data.DataRow dRow = dSource.NewRow();
      dRow[0] = 0;
      dRow[1] = “Family Guy”;
      dRow[2] = “Fox”;
      dSource.Rows.Add(dRow);

      dRow = dSource.NewRow();
      dRow[0] = 1;
      dRow[1] = “The Simpsons”;
      dRow[2] = “Fox”;
      dSource.Rows.Add(dRow);

      dRow = dSource.NewRow();
      dRow[0] = 2;
      dRow[1] = “Futurama”;
      dRow[2] = “Fox”;
      dSource.Rows.Add(dRow);

      dRow = dSource.NewRow();
      dRow[0] = 3;
      dRow[1] = “Beavis & Butthead”;
      dRow[2] = “MTV”;
      dSource.Rows.Add(dRow);

      #endregion Create Master DataTable / Fill Master DataTable

      #region Create Detail DataTable / Fill Detail DataTable

      System.Data.DataTable dDetail = new System.Data.DataTable(“Detail”);
   
      dDetail.Columns.Add(“ID”,typeof(int));
      dDetail.Columns.Add(“Name”,typeof(string));
      dDetail.Columns.Add(“Occupation”,typeof(string));

      dRow = dDetail.NewRow();
      dRow[0] = 0;
      dRow[1] = “Peter”;
      dRow[2] = “Toy Factory worker”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 0;
      dRow[1] = “Lois”;
      dRow[2] = “Housekeeper”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 0;
      dRow[1] = “Chris”;
      dRow[2] = “Student”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 0;
      dRow[1] = “Meg”;
      dRow[2] = “Student”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 0;
      dRow[1] = “Stewie”;
      dRow[2] = “Baby”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 0;
      dRow[1] = “Brian”;
      dRow[2] = “Dog”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 1;
      dRow[1] = “Homer”;
      dRow[2] = “Nuclear Plant Worker”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 1;
      dRow[1] = “Marge”;
      dRow[2] = “Housekeeper”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 1;
      dRow[1] = “Lisa”;
      dRow[2] = “Student”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 1;
      dRow[1] = “Bart”;
      dRow[2] = “Student”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 1;
      dRow[1] = “Maggie”;
      dRow[2] = “Baby”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 2;
      dRow[1] = “Fry”;
      dRow[2] = “Pizza delivery boy”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 2;
      dRow[1] = “Lila”;
      dRow[2] = “Spaceship captain”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 2;
      dRow[1] = “Bender”;
      dRow[2] = “Alcoholic Robot”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 3;
      dRow[1] = “Beavis”;
      dRow[2] = “Retard”;
      dDetail.Rows.Add(dRow);

      dRow = dDetail.NewRow();
      dRow[0] = 3;
      dRow[1] = “Butthead”;
      dRow[2] = “Retard”;
      dDetail.Rows.Add(dRow);
     
      #endregion Create Detail DataTable / Fill Detail DataTable

      #region Create DataSet / Add the Tables to it / Create Relation

      DataSet myData = new DataSet();

      myData.Tables.Add(dSource);
      myData.Tables.Add(dDetail);

      DataRelation myRelation = new DataRelation(“Show-VS-Characters”, dSource.Columns[“ID”] , dDetail.Columns[“ID”], false);
     
      myData.Relations.Add(myRelation);

      #endregion Create DataSet / Add the Tables to it / Create Relation
           
     
      #region Bind main grid Control to the Master table
     

      gridControl1.SetDataBinding(myData, myData.Tables[“Shows”].TableName);
     
      #endregion Bind main grid Control to the Master table

      #region Create detail Grid / Bind to relation
      DetailGrid detailGrid1 = new DetailGrid();
      detailGrid1.SetDataBinding(myData, “Shows.” + myData.Relations[0].RelationName);
      detailGrid1.HeaderRows.Add(new Xceed.Grid.ColumnManagerRow());

      #endregion Create detail Grid / Bind to relation

      gridControl1.Columns[“ID”].Visible = false;

          gridControl1.DetailGridTemplates.Add(detailGrid1);
          gridControl1.DetailGridTemplates[0].Columns[“ID”].Visible = false;
          gridControl1.DetailGridTemplates[0].Columns[“Occupation”].Width = 300;

      gridControl1.UpdateDetailGrids();

————

Imported from legacy forums. Posted by Marc [Xceed] (had 1184 views)