|
-
Jun 13th, 2013, 09:50 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Jun 13th, 2013, 10:39 AM
#2
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|