Question How to iterate through WPF datagrid columns following mvvm pattern?

TB007

Member
Joined
Aug 13, 2022
Messages
24
Programming Experience
Beginner
I want to iterate through a datagrid header column and get its values to a variable, something like

C#:
            foreach (DataGridColumn column in gridView.Columns)
            {
                var cell = column.Header.ToString()
            }

How can I do this following mvvm?
 
They're already in a variable? Can you more accurately explain what the output of the exercise is to be? Why does the posted code not work (other than repeatedly declaring and losing a local variable)? What do you mean by "following mvvm"?
 
Last edited:
I don't understand; the values of the columns are already in the someGrid.Columns variable

What do you mean by "values"? Columns don't have values, rows have values
 
What I mean is that the header of the datagrid column has some text in it, say the datagrid has 3 columns "Name", "Address" & "Age", I want to get those text dynamicall and use it. As far as I know in mvvm I simply cannot use like
foreach (DataGridColumn column in gridView.Columns)

Are you still confused ?
 
Why would you need to get the column headers from the UI? If that's data you need to use then it should be coming from the view model in the first place. The whole point of MVVM is that the View simply displays what's in the View Model. If you need to get data from the UI, i.e. the View, then you're doing MVVM wrong to start with.
 
Why would you need to get the column headers from the UI? If that's data you need to use then it should be coming from the view model in the first place. The whole point of MVVM is that the View simply displays what's in the View Model. If you need to get data from the UI, i.e. the View, then you're doing MVVM wrong to start with.
I get your point.

Actually in my XAML I have the set datagrid AutoGenerateColumns="False" & then added the columns like

C#:
<DataGrid.Columns>
    <DataGridTextColumn
        Header="Name"
        Binding="{Binding Path=eName}" />
    <DataGridTextColumn
        Header="Full Address"
        Binding="{Binding Path=eAddress}" />
    <DataGridTextColumn
        Header="Age"
        Binding="{Binding Path=eAge}" />
</DataGrid.Columns>

and I was trying to get the Header values in a method in my VM.

So, If I change the Header's to Header="{Binding billHead1}", Header="{Binding billHead2}", Header="{Binding billHead3}" and in my VM add properties like (so that I can access them inside any method of my VM)

C#:
public string eHead1 = @"Name";
public string billHead2 = @"Full Address";
public string billHead3 = @"Age";

But then the UI is not showing the Header's.

What am I doing wrong ?
 
Does other binding within your XAML work, but only not work in the data grid? Does it work for the data within the data grid, but not the column headers? I have a sneaking suspicion that you have a binding path problem if the data within the data grid works but not the columns headers. Please show us the XAML for your data grid.
 
Does other binding within your XAML work, but only not work in the data grid? Does it work for the data within the data grid, but not the column headers? I have a sneaking suspicion that you have a binding path problem if the data within the data grid works but not the columns headers. Please show us the XAML for your data grid.
Here is my XAML for data grid

C#:
<DataGrid
    SelectedItem="{Binding SelectedEmp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    ItemsSource="{Binding FilteredEmps}"
    SelectionMode="Extended"
    AutoGenerateColumns="False"
    IsReadOnly="True"
    SelectionUnit="FullRow"
    HeadersVisibility="Column"
    GridLinesVisibility="None"
    CanUserAddRows="false">
    <DataGrid.Columns>
        <DataGridTextColumn
            Header="Name"
            Binding="{Binding Path=eName}" />
        <DataGridTextColumn
            Header="Full Address"
            Binding="{Binding Path=eAddress}" />
        <DataGridTextColumn
            Header="Age"
            Binding="{Binding Path=eAge}" />
    </DataGrid.Columns>
</DataGrid>

Using this I was able to see the column headers but when I change the headers as Header="{Binding billHead1}", Header="{Binding billHead2}", Header="{Binding billHead3}" while declaring properties in my VM like below, the header is not showing !

C#:
public string eHead1 = @"Name";
public string billHead2 = @"Full Address";
public string billHead3 = @"Age";
 
Did the binding on line 3 work correctly (e.g. ItemsSource="{Binding FilteredEmps}") ? Do you see data in your data grid?
 
Wow! Have to do some path gymnastics.

See StackOverflow:
 
Did the binding on line 3 work correctly (e.g. ItemsSource="{Binding FilteredEmps}") ? Do you see data in your data grid?
Yes, I do see data in my data grid.

BTW, I've wrongly typed public string eHead1 = @"Name"; instead of public string billHead1 = @"Name"; in my previous post.
 
Wow! Have to do some path gymnastics.

See StackOverflow:
It sure seems that way !!!

Lets see if I find solution from here ...
 
The answer solution I linked to above worked for me. (e.g. creating a header template). The other answers which tried to do the binding straight into the header property did not work for me.
 
Back
Top Bottom