I want to hide the id column how do I do that.
And centered text and so on.
I try this, but the display is blank
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.
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
Retrieve the currently selected row from the table,
Can it run like the following code
Maybe my idea is strange
2.Populating Master-Details view through events
Examples
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
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 SelectedItem, SelectedItems, and SelectedIndex properties directly.
Refer the below link ,
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.
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); }
|
I found that using DetailsViewExpanding affected the ability to adjust the automatic line height of the content
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