JavaScript UI Libraries & Blazor Components Suite – Smart UI › Forums › Data Grid › Dynamic columns (Blazor) › Reply To: Dynamic columns (Blazor)
November 9, 2021 at 2:52 pm
#102526
Yavor Dashev
Participant
Hi TurricanDE,
When you set GridColumn newColumn = new GridColumn(){ Label = columnsList[i], DataField = columnsList[i], SortOrder = GridColumnSortOrder.Desc };
it only sorts the cells intially when they are loaded, however if you want to have the Sorting and Filtering functionalities I have modified the code example from my previous reply:
<Grid DataSourceSettings="DataSettings" @ref="grid" DataSource="dataRecords" Columns="@columns" Sorting="@sorting" Filtering="@filtering"></Grid>
<div class="options">
<div class="caption">Settings</div>
<div class="option">
<Button Disabled="@addColumnBtnDisabled" OnClick="AddColumn">Add Column</Button>
</div>
<div class="option">
<Button Disabled="@removeLastColumnBtnDisabled" OnClick="RemoveLastColumn">Remove Last Column</Button>
</div>
<div class="option">
<Button Disabled="@removeFirstColumnBtnDisabled" OnClick="RemoveFirstColumn">Remove First Column</Button>
</div>
<div class="option">
<Button Disabled="@updateFirstColumnBtnDisabled" OnClick="UpdateFirstColumn">Update First Column Header</Button>
</div>
</div>
}
</Example>
@code {
Grid grid;
GridSorting sorting = new GridSorting()
{
Enabled = true,
Mode = GridSortingMode.Many
};
GridFiltering filtering = new GridFiltering()
{
Enabled = true,
};
GridDataSourceSettings DataSettings = new GridDataSourceSettings()
{
DataFields = new List<GridDataSourceSettingsDataField>()
{
new GridDataSourceSettingsDataField()
{
Name = "Id",
DataType = GridDataSourceSettingsDataFieldDataType.Number
},
new GridDataSourceSettingsDataField()
{
Name = "Area",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "Population_Urban",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "Population_Rural",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "Population_Total",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "GDP_Agriculture",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "GDP_Industry",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "GDP_Services",
DataType = GridDataSourceSettingsDataFieldDataType.String
},
new GridDataSourceSettingsDataField()
{
Name = "GDP_Total",
DataType = GridDataSourceSettingsDataFieldDataType.String
}
}
};
bool addColumnBtnDisabled;
bool removeLastColumnBtnDisabled;
bool removeFirstColumnBtnDisabled;
bool updateFirstColumnBtnDisabled;
List<GridColumn> columns = new List<GridColumn>()
{
new GridColumn()
{
DataField = "Country",
Label = "Country",
},
new GridColumn()
{
DataField = "Area",
Label = "Area"
},
new GridColumn()
{
DataField = "Population_Rural",
Label = "Population_Rural"
},
new GridColumn()
{
DataField = "Population_Total",
Label = "Population_Total"
},
new GridColumn()
{
DataField = "GDP_Total",
Label = "GDP_Total"
}
};
private List<CountryRecord> dataRecords;
protected override void OnInitialized()
{
base.OnInitialized();
dataRecords = dataService.GenerateCountriesData();
}
private string[] columnsList = new string[]
{
"Id",
"Country",
"Area",
"Population_Urban",
"Population_Rural",
"Population_Total",
"GDP_Agriculture",
"GDP_Industry",
"GDP_Services",
"GDP_Total"
};
For your other question I will further investigate this behavior.
Please, do not hesitate to contact us if you have any additional questions.
Best regards,
Yavor Dashev
Smart UI Team
https://www.htmlelements.com/