1

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;
            }
        };
    }
8
  • 2 questions, first how did you populate your datagrid, from code-behind or using the ItemsSource "ModulesView"? Second, your sorting should already be set correctly since you used SortDirection="Ascending" on your Address column. Commented Jun 15, 2017 at 9:20
  • First I use ICollectionView ModulesView in the ViewModel to populate datagrid, and second I don't know why but the data in the Row Address didn't default sorted untill I click the Row Header. Commented Jun 15, 2017 at 9:32
  • For the sorting problem, would it be an acceptable alternative to sort the items before setting ItemsSource, by OrderBy()? That way you won't have to rely on the Datagrid sorting. DataGrid SortDirection ignored. Also you have a small typo Header="Addrsss" should be Header="Address" Commented Jun 15, 2017 at 9:35
  • And as for your ParamValue question, is there something like a button users can click, and that should trigger registerArray to be filled? If so, you can just use registerArray = ModulesView.OrderBy(mod => mod.Address).Select(mod => mod.ParamValue).ToArray(); at any point of your code to get the array. Note that you would need using System.Linq; for that. Commented Jun 15, 2017 at 9:39
  • @KeyurPATEL Sorry, but ite seems that my ModulesView can not call OrderBy method. Commented Jun 16, 2017 at 2:08

1 Answer 1

1

After seeing your ModulesView code, I suggest using an ObservableCollection to bind at this step ModulesView = new ListCollectionView(sdb.GetModules()) instead of sdb.GetModules().

public ICollectionView ModulesView
{
    get { return _ModulesView; }
    set
    {
        _ModulesView = value;
        NotifyPropertyChanged();
    }
}

private ObservableCollection<Module> myModulesList;

private void RefreshModule()
{
    myModulesList = new ObservableCollection<Module>(sdb.GetModules().OrderBy(mod => mod.Address));
    ModulesView = CollectionViewSource.GetDefaultView(myModulesList);
    ModulesView.Filter = obj =>
        {
            var Module = (Module)obj;
            return SelectedProduct != null && SelectedProduct.ModelNumber == Module.ModelNumber;
        };
}

Basically, if you ever want to update data and show the change in the datagrid, all you need to do is modify the ObservableCollection myModulesList.

And later, whenever you want to fill your registerArray, you can use:

registerArray = myModulesList.OrderBy(mod => mod.Address).Select(mod => mod.ParamValue).ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your kind reply again. But I am kind of confuse how to write TodayDispatchCollection
@LeeBarry Ah, apologies, I missed that from part of my code which I copied, I'll edit.
Glad I could help :)
Sorry to borther you! what if I change the ObservableCollection to DataSet? How should I get the ModulsView in order? For details: links

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.