Enumerating Rows and Columns Using the FieldLayoutInitialized Event

Glossary Item Box

Infragistics(R) NetAdvantage(R) for WPF

Enumerating Rows and Columns Using the FieldLayoutInitialized Event

xamDataPresenter's™ Initialize events are critical for accessing cell values as the control is being initialized. This is where you have access to each cell in a DataRecord, and can test its value, or set its value. For example, if you want to display some where on your Page/Window the total number of records with a certain value in them, you would need to enumerate over all the Records, and test the value of the cell, and the best place to do this is in the FieldLayoutInitialized event.

Note: The xamNumericEditor mentioned in this topic is only available in the NetAdvantage for WPF Full version. The concepts shown here though can be applied to the editors available in the Express version.

The following procedure assumes you have a data bound xamDataPresenter, and you want to enumerate over all the rows, to check to see how many records have a value of two in a specific field.. For more information, see Creating xamDataPresenter in XAML.

  1. Create an instance of XamDataPresenter and name it. Set the FieldLayoutInitialized event to the XamDataPresenter1_FieldLayoutInitialized method you will create in step 6. Define a XamNumericEditor, name it, and set its ReadOnly property to True.

    In XAML:

    <igDP:XamDataPresenter x:Name="XamDataPresenter1" 
      ...
      FieldLayoutInitialized="XamDataPresenter1_FieldLayoutInitialized"/>
    	<igDP:XamDataPresenter.View>
    		<igDP:GridView/>
    	</igDP:XamDataPresenter.View>
    </igDP:XamDataPresenter>
    
    <igEditors:XamNumericEditor x:Name="XamNumericEditor1" ReadOnly="True" />
    

  2. Before you start writing any code, you should place using/Imports directives in your code-behind so you don't need to always type out a member's fully qualified name.

    In Visual Basic:

    Imports Infragistics.Windows.DataPresenter.Events
    Imports Infragistics.Windows.DataPresenter
    

    In C#:

    using Infragistics.Windows.DataPresenter.Events;
    using Infragistics.Windows.DataPresenter;
    

  3. The method defined for the FieldLayoutInitialized event loops over the xamDataPresenter's Records collection and checks if the current record is a DataRecord. If the record is a DataRecord, the method scans the DataRecord to determine if it should be included in the count.

    In Visual Basic:

    Sub XamDataPresenter1_FieldLayoutInitialized(ByVal sender As Object, _
      ByVal e As FieldLayoutInitializedEventArgs)
    	Dim counter As Integer = 0
    	Dim i As Integer
    	For i = 0 To Me.XamDataPresenter1.Records.Count - 1
    		' Check to see if the Record is a DataRecord
    		If TypeOf Me.XamDataPresenter1.Records(i) Is DataRecord Then
    			Dim myRecord As DataRecord = _
    			  CType(Me.XamDataPresenter1.Records(i), DataRecord)
    			' Test the 3rd Field's Cell Value to see if it is a 2
    			If myRecord.Cells(2).Value.ToString() = "2" Then
    				' If it is we want to include it in our count
    				counter += 1
    			End If
    		End If
    	Next i
    	' Display the count 
    	Me.XamNumericEditor1.Text = counter.ToString()
    End Sub
    

    In C#:

    void XamDataPresenter1_FieldLayoutInitialized(object sender, 
      FieldLayoutInitializedEventArgs e)
    {
    	int counter = 0;
    	for (int i = 0; i <= this.XamDataPresenter1.Records.Count-1; i++)
    	{
    		// Check to see if the Record is a DataRecord
    		if (this.XamDataPresenter1.Records[i] is DataRecord)
    		{
    			DataRecord myRecord = 
    			  (DataRecord)this.XamDataPresenter1.Records[i];
    			// Test the 3rd Field's Cell Value to see if it is a 2
    			if (myRecord.Cells[2].Value.ToString() == "2")
    			{
    				// If it is we want to include it in our count
    				counter++;
    			}
    		}
    	}
    	// Display the count 
    	this.XamNumericEditor1.text = counter.ToString();
    }
    

  4. Build and run the project. You should see the number two in the xamNumericEditor similar to the image below.

E-mail your feedback on this topic.

Opinion about our help? Take our survey.

Copyright © 2003-2007 Infragistics, Inc. All rights reserved.

Build Version: 7.1.20071.1320