Fahad
Fahad

Reputation: 55

How to get the `DataGridTextColumn` name and header of a `datagrid `through `code-behind`?

I am working with datagrid and I want to get the name and header of the datagridtextcolumn in code behind.

I have tried getting header value as

`Datagrid.Columns[0].HeaderText` 

. But HeaderText is not working in my code.

Another way I tried is

var HeaderTexts = dataGridUserSalesRep.Columns.Select(e => e.Header.ToString()).ToList();.

This one also not working for me.

My Datagrid:

<DataGrid SelectionChanged="DatagridListviewSelectionChanged" Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type Grid}, Mode=FindAncestor}}" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorLevel=1, AncestorType={x:Type Grid}, Mode=FindAncestor}}"   x:Name="ListView"  Style="{StaticResource DataGridListing}"  >
                            <DataGrid.Columns>
                                <DataGridTemplateColumn  Width="0.5*"  >
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <RadioButton GroupName="RadioButtonGroup"   Style="{StaticResource DatagridRadioButton}"  IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn   Width="2*"  Header="{Binding SerialNo , Source={StaticResource Strings}}" x:Name="DataGridColumnSlNo"  Binding="{Binding SlNo}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding ItemCode , Source={StaticResource Strings}}" x:Name="DataGridColumnHsn"  Binding="{Binding ItemCode}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding itemName , Source={StaticResource Strings}}"   Binding="{Binding ItemName}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding barcode , Source={StaticResource Strings}}"   Binding="{Binding BarCode}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding hsnCode , Source={StaticResource Strings}}"   Binding="{Binding HsnCodes}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding CountryOfOrigin , Source={StaticResource Strings}}"   Binding="{Binding COO}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding Brand , Source={StaticResource Strings}}"   Binding="{Binding Brand}"   />
                                <DataGridTextColumn   Width="2*"  Header="{Binding shelf     , Source={StaticResource Strings}}"   Binding="{Binding Shelf}"   />
                            </DataGrid.Columns>
                        </DataGrid>

I want both Name and header of all columns of this datagrid

Upvotes: 0

Views: 92

Answers (0)

Related Questions