Results 1 to 2 of 2

Thread: [RESOLVED] How get the value of a HIDDEN DataGrid cell?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Resolved [RESOLVED] How get the value of a HIDDEN DataGrid cell?

    I'm having trouble getting the value of a cell in a DataGrid if that column is hidden. The following C# code works fine if the column is visible, but fails if it is hidden.

    Code:
    int id = Convert.ToInt32(((TextBlock)grdData.Columns[0].GetCellContent(grdData.SelectedItem)).Text);
    If the column is hidden, I get a null value exception because the visual elements haven't been created... the TextBlock is null. So the question is, how do I get the value from a hidden cell?

    For the record, I use VS 2012, .NET 4.5, and program in C#.

    I believe the VB version of this code would look like this:

    Code:
    Dim id As Integer = Convert.ToInt32(DirectCast(grdData.Columns(0).GetCellContent(grdData.SelectedItem), TextBlock).Text)
    Thanks in advance for any help on this.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    Re: How get the value of a HIDDEN DataGrid cell?

    Okay, I got the solution, and it's incredibly simple. First, assume that the DataGrid is bound to a list of type Policy, which is a custom written class... List<Policy>. The policy class has a field called PolicyID, and that's the value I want. Given that as the ItemSource, getting the value of a hidden column, or ANY column, for that matter, becomes simple:

    Code:
    Policy pol = (Policy)grdData.SelectedItem;
    int policyID = pol.PolicyID;
    What I'm doing here is accessing the bound list instead of the DataGrid itself. But it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width