In my WPF project, I have a datagrid which looks like:
<DataGrid x:Name="dgConfig" BorderThickness="5" AutoGenerateColumns="False" Margin="10,10,10,15" ItemsSource="{Binding ModulesView, Mode=TwoWay}" FontSize="14" Background="White">
<DataGrid.Columns>
<DataGridTextColumn Header="ParamName" Binding="{Binding ParamName}" />
<DataGridTextColumn Header="ParamValue" Binding="{Binding ParamValue}" />
<DataGridTextColumn Header="DefaultValue" Binding="{Binding DefaultValue}" />
<DataGridTextColumn Header="MaxValue" Binding="{Binding MaxValue}"/>
<DataGridTextColumn Header="MinValue" Binding="{Binding MinValue}"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}" SortDirection="Ascending"/>
</DataGrid.Columns>
</DataGrid>
And now I want to get the Row value of ParamValue. In this case, I need to put the ParamValue into registerArray in the order of Address, so how should I set the Datagrid displayed in the order of Address and then put the ParamValue into Array registerArray in the order of Address? Many thanks!
--------------------------------update------------------------------------------
Define ModulesView:
public ICollectionView ModulesView
{
get { return _ModulesView; }
set
{
_ModulesView = value;
NotifyPropertyChanged();
}
}
Use ModulesView:
private void RefreshModule()
{
ModulesView = new ListCollectionView(sdb.GetModules())
{
Filter = obj =>
{
var Module = (Module)obj;
return SelectedProduct != null && SelectedProduct.ModelNumber == Module.ModelNumber;
}
};
}
SortDirection="Ascending"on your Address column.ICollectionView ModulesViewin the ViewModel to populate datagrid, and second I don't know why but the data in the RowAddressdidn't default sorted untill I click the Row Header.OrderBy()? That way you won't have to rely on the Datagrid sorting. DataGrid SortDirection ignored. Also you have a small typoHeader="Addrsss"should beHeader="Address"ParamValuequestion, is there something like a button users can click, and that should triggerregisterArrayto be filled? If so, you can just useregisterArray = ModulesView.OrderBy(mod => mod.Address).Select(mod => mod.ParamValue).ToArray();at any point of your code to get the array. Note that you would needusing System.Linq;for that.