Huma Ali
Huma Ali

Reputation: 1809

Get value of a hidden column

I have a DataGrid with columns ID,Name and a HyperLinkButton on each row. My ID column is hidden by the property Visibility="Collapsed". On HyperLinkButton's Click event I am accessing my columns value but I am unable to access the column when it is hidden i.e it returns null. Its only accessible when I make it Visible. Is there any solution to this problem?

XAML for DataGrid:

<sdk:DataGrid AutoGenerateColumns="False"
              HorizontalAlignment="Left"
              Height="163"
              VerticalAlignment="Top"
              Name="ProductGrid">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn x:Name="ID"
                                Binding="{Binding Path=ProductID, Mode=OneWay}"
                                Header="ID"
                                IsReadOnly="True"
                                Width="50"
                                Visibility="Collapsed" />
        <sdk:DataGridTextColumn x:Name="Name"
                                Binding="{Binding Path=Name, Mode=OneWay}"
                                Header="Name"
                                IsReadOnly="True"
                                Width="340" />
        <sdk:DataGridTemplateColumn Header="">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate x:Name="gridTemplate">
                    <StackPanel Orientation="Vertical"
                                VerticalAlignment="Center">
                        <HyperlinkButton Content="Details"
                                         Tag="Hyperlinkbutton"
                                         HorizontalAlignment="Center"
                                         Click="OnGetDetailsClick"
                                         Width="100" />
                    </StackPanel>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
        </sdk:DataGridTemplateColumn>
    </sdk:DataGrid.Columns>

HyperLink's click event:

var selectedGridRow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
TextBlock txtblkID = (TextBlock)ProductGrid.Columns[0].GetCellContent(selectedGridRow);
p.ID = Int32.Parse(txtblkID.Text);

Upvotes: 0

Views: 213

Answers (1)

jvanrhyn
jvanrhyn

Reputation: 2824

You can get hold of the DataContext of the selected row and get the value from there. Replace the DataSourceType cast with your model.

var selectedGridRow = DataGridRow.GetRowContainingElement(sender as FrameworkElement);
p.ID = ((DataSourceType)selectedGridRow.DataContext).ProductID;

Upvotes: 1

Related Questions