0

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);
2
  • Can you the HyperLinkButton XAMl code too? Jan 21, 2016 at 6:24
  • @user2946329 Added XAMl code for the DataGrid
    – Huma Ali
    Jan 21, 2016 at 6:28

1 Answer 1

1

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;
7
  • I added this line p.ID = ((DataSourceType)selectedGridRow.DataContext).ProductID; and got error The type or namespace name DataSourceType could not be found
    – Huma Ali
    Jan 21, 2016 at 6:41
  • Yes, as I said in the comment, I don't know what your data model is called, you have to replace that with whatever type you are using for your grid data source.
    – jvanrhyn
    Jan 21, 2016 at 6:43
  • How? Currently I am binding the Grid with GetProductsCompletedEventArgs e.Result property PagedCollectionView pagingCollection = new PagedCollectionView(e.Result); ProductGrid.ItemsSource = pagingCollection;
    – Huma Ali
    Jan 21, 2016 at 6:45
  • You are binding your ID column to a field called ProductId. Where is this product Id defined?
    – jvanrhyn
    Jan 21, 2016 at 6:46
  • In a class Product
    – Huma Ali
    Jan 21, 2016 at 6:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.