Master Details View How to hide a column

  DataSet data  = new DataSet();
  DataTable a=new DataTable();
  a.Columns.Add("id", typeof(string));
  a.Columns.Add("a", typeof(string));
  a.Columns.Add("b", typeof(string));
  a.Rows.Add(1000, "aaaa", "bbbb");
  a.Rows.Add(1001, "aaaa1", "bbbb1");
  a.Rows.Add(1002, "aaaa2", "bbbb2");
  data.Tables.Add(a);
  DataTable b= new DataTable();

  b.Columns.Add("id", typeof(string));
  b.Columns.Add("a", typeof(string));
  b.Columns.Add("b", typeof(string));
  b.Rows.Add(1000, "aaaa", "bbbb");
  b.Rows.Add(1001, "aaaa1", "bbbb1");
  b.Rows.Add(1002, "aaaa2", "bbbb2");


  data.Tables.Add(b);

  data.Relations.Add(new DataRelation("a_b", data.Tables[0].Columns["id"], data.Tables[1].Columns["id"]));
   
  sfDataGrid1.DataSource = data;

  sfDataGrid1.Columns["id"].Visible = false;
Image_3497_1714392857868

I want to hide the id column how do I do that.

And centered text and so on.



9 Replies

YM yu ma April 29, 2024 02:57 PM UTC



sfDataGrid1.DataSource = dt1;


SfDataGrid firstLevelNestedGrid = new SfDataGrid();
firstLevelNestedGrid.AutoGenerateColumns = true;
firstLevelNestedGrid.AllowEditing = false;
firstLevelNestedGrid.AllowSorting = true;
firstLevelNestedGrid.AllowFiltering = true;
//    firstLevelNestedGrid.AutoGenerateColumns = true;
firstLevelNestedGrid.DataSource = dt2;

GridViewDefinition firstLevelGridViewDefinition = new GridViewDefinition();
firstLevelGridViewDefinition.RelationalColumn = "id";

firstLevelGridViewDefinition.DataGrid = firstLevelNestedGrid;
sfDataGrid1.DetailsViewDefinitions.Add(firstLevelGridViewDefinition);


I try this, but the display is blank

Image_9649_1714402599818



SB Sweatha Bharathi Syncfusion Team April 30, 2024 01:40 PM UTC

yu ma ,


Query 1: To hide the “id” column and centered the text.

                                     We have reviewed your scenario. You expected to hide the column for the master-details view, but in your provided code snippet image, you only hide the column for the main grid. To hide the column for the master-details view, you can achieve this by using the DetailsViewLoading event.

In this event, you can access the detail view grid, and then hide the column for the detail view grid.

Additionally, you can center the text by using a cell style of horizontal alignment within this event.

Code Snippet:

  private void SfDataGrid1_DetailsViewLoading(object sender, DetailsViewLoadingAndUnloadingEventArgs e)

  {

      e.DetailsViewDataGrid.Columns["id"].Visible = false;

      e.DetailsViewDataGrid.Columns["a"].CellStyle.HorizontalAlignment = HorizontalAlignment.Center;

      e.DetailsViewDataGrid.Columns["b"].CellStyle.HorizontalAlignment = HorizontalAlignment.Center;

  }


Image Reference:



Query 2: I try this, but the display is blank.


    In the provided code snippet, setting the data source for both the main and nested grid separately is not the recommended approach. Instead, it's better to create a single data source with relations. You can then set this data source only for the main grid and create a relation between the parent and nested grids.


By creating and setting the relation name to GridViewDefinition.RelationalColumn, This allows the nested grid to display data related to the parent grid. Finally, the GridViewDefinition with the relation is added to the SfDataGrid.DetailsViewDefinitions collection of the parent DataGrid.


Can you clarify why you set the datasource for both the nested and parent grids?


Code Snippet:


  DataTable dt1 = CreateDataTable1();

  DataTable dt2 = CreateDataTable2();

 

  DataSet data = new DataSet();

  data.Tables.Add(dt1);

  data.Tables.Add(dt2);

 

 

 

  // Create the main grid

  sfDataGrid1.AutoGenerateColumns = true;

  sfDataGrid1.AllowEditing = false;

  sfDataGrid1.AllowSorting = true;

  sfDataGrid1.AllowFiltering = true;

  sfDataGrid1.DataSource = data;

 

  data.Relations.Add(new DataRelation("a_b", data.Tables[0].Columns["id"], data.Tables[1].Columns["id"]));

 

 

  GridViewDefinition firstLevelGridViewDefinition = new GridViewDefinition();

  firstLevelGridViewDefinition.RelationalColumn = "a_b";

 

  SfDataGrid firstLevelNestedGrid = new SfDataGrid();

  firstLevelNestedGrid.AllowSorting = true;

  firstLevelNestedGrid.AllowFiltering = true;

  firstLevelNestedGrid.AllowResizingColumns = true;

 

  firstLevelGridViewDefinition.DataGrid = firstLevelNestedGrid;

  sfDataGrid1.DetailsViewDefinitions.Add(firstLevelGridViewDefinition);

 



Image Reference:



UG Link : https://help.syncfusion.com/windowsforms/datagrid/masterdetailsview


We have provided sample for your reference , kindly review the sample and let us know if you have nay further concerns on this.


Attachment: MasterDetailsView_ec9d638f.zip


YM yu ma May 1, 2024 06:20 AM UTC

hi  Sweatha Bharathi


Thank you very much for your reply. The problem has been solved

I have doubts


Since whether the slave table can set the hidden id like the master table


sfDataGrid1.DetailsViewDefinitions[0].DataGrid.Columns["id"].Visible = false;


Retrieve the currently selected row from the table, 

Can it run like the following code

var dt2=  sfDataGrid1.DetailsViewDefinitions[0].DataGrid.View.Records
var row = records[i].Data as DataRowView;


Maybe my idea is strange


2.Populating Master-Details view through events

Examples

this.sfDataGrid.DetailsViewExpanding += SfDataGrid_DetailsViewExpanding;

private void SfDataGrid_DetailsViewExpanding(object sender, DetailsViewExpandingEventArgs e)
{
    OrderInfo orderInfo = e.Record as OrderInfo;
    if (orderInfo.OrderID == 10000)
        e.DetailsViewDataSource.Add("OrderDetails", GetDetailsViewDataSource());
}

Can I bind a DataTable directly?

------------------------------------------------------------------------------------------------------------------------------------------------------------------

Finally, happy holidays.

my English is not very good, from the translation software, there is not smooth place, forgive me




DD Dhivyabharathi Dakshinamurthy Syncfusion Team May 2, 2024 02:59 PM UTC

Hi yu ma,

Query 1: “whether the slave table can set the hidden id like the master table”

 

You can use your code to hide the master-details view column in the DetailsViewLoading event. Ensure that you implement this code when the master-details view has been loaded; otherwise, the column cannot be generated, leading to the exception being thrown.

 

You can access the selected record or records and selected record index of the DetailsViewDataGrid by using the SelectedItemSelectedItems, and SelectedIndex properties directly.

 

Refer the below link ,

 

UG Link : https://help.syncfusion.com/windowsforms/datagrid/masterdetailsview#getting-the-selecteditem-selecteditems-and-selectedindex-of-detailsviewdatagrid

 

Query 2: .Populating Master-Details view through events

 

Yes , You can set the DataSource on-demand when expanding the record through DetailsViewExpandingEventArgs.DetailsViewDataSource property in the SfDataGrid.DetailsViewExpanding` event handler.

 

UG Link : https://help.syncfusion.com/windowsforms/datagrid/masterdetailsview#populating-master-details-view-through-events




YM yu ma replied to Dhivyabharathi Dakshinamurthy May 21, 2024 01:21 PM UTC

Image_6189_1716297694214






SB Sweatha Bharathi Syncfusion Team May 22, 2024 02:22 PM UTC

Hi yu ma,

We have reviewed your provided image .The DataTable is converted improperly to IEnumerable in the DetailsViewExpanding event, causing the default properties of DataTable to be taken into account instead of the columns available in the DataTable.


To resolve this, pass the DataView of the DataTable through dataTable.DefaultView to DetailsViewDataSource.


Code Snippet :


  private void SfDataGrid1_DetailsViewExpanding(object sender, DetailsViewExpandingEventArgs e)

  {

      DataTable b = new DataTable();

      b.Columns.Add("id", typeof(string));

      b.Columns.Add("a", typeof(string));

      b.Columns.Add("b", typeof(string));

      b.Rows.Add(1000, "aaaa", "bbbb");

      b.Rows.Add(1001, "aaaa1", "bbbb1");

      b.Rows.Add(1002, "aaaa2", "bbbb2");

 

      e.DetailsViewDataSource.Add("id", b.DefaultView);

  }

 




YM yu ma replied to Sweatha Bharathi May 23, 2024 09:09 AM UTC

Thank you very much. I get it



YM yu ma May 23, 2024 10:01 AM UTC

I found that using DetailsViewExpanding affected the ability to adjust the automatic line height of the content



SB Sweatha Bharathi Syncfusion Team May 24, 2024 02:43 PM UTC

Hi yu ma,

We have reviewed your scenario. We suspect that you are expecting AutoRowHeight for DetailsViewDataGrid.  The Master-Details view does not support AutoRowHeight. We have already documented this limitation. Kindly refer to the link below:



UG Link : https://help.syncfusion.com/windowsforms/datagrid/masterdetailsview#master-details-view-limitations


Loader.
Up arrow icon