Accessing data: examples

You can use the PrimaryData, FilteredData, and DeletedData objects to access and set data values for a single column, an entire row, or a range of rows. Most of the examples in this section use the primary buffer, but could also be used for the filtered and delete buffers. If an invalid datatype is submitted to the DataWindow by an indexer an exception is thrown.

Single column

To set a value for a single column, the value must be cast to Object. A returned value can be cast to its actual datatype. You must know the column datatype in advance.

// get a column value
Object O = dw_1.PrimaryData [ 1, (short) 3 ];

// get original data value with third argument "true"
Object O = dw_1.PrimaryData [ 1, (short) 3, true ];

// use a column name 
Object O = dw_1.PrimaryData [ 1, "emp_lname" ];

// get original data value
Object O = dw_1.PrimaryData [ 1, "emp_lname", true];

// set a column value
dw_1.PrimaryData [ 1, (short) 3 ] = (Object) O;

// use the column name
dw_1.PrimaryData [ 1, "emp_lname" ] = (Object) O; 

Entire row

To set data values for an entire row, you need to provide an array of objects and each object in the array must be of the correct datatype. Objects in a returned array can be cast to their real datatype. Again, you must know the column datatypes in advance.

// return an array of objects for row 5
Object [ ] O = dw_1.PrimaryData[ 5 ]; 

// get original data values in row 5
Object [ ] O = dw_1.PrimaryData[ 5, true ];

// set data values in row 5 from an array of objects
dw_1.PrimaryData [ 5 ] = O; 

Range of rows

When you access data values for a range of rows, an array of object arrays is returned. Each object array is the same as the single row described in “Entire row,” and accessing and setting values follow the same rules for datatypes.

// get data values in rows 1 to 5
Object [ ] O = dw_1.PrimaryData [ 1, 5 ];

// get original data in rows 1 to 5
Object [ ] O = dw_1.PrimaryData [ 1, 5, true ];

// set data values in rows 1 to 5
dw_1.PrimaryData [ 1, 5 ] = O;

Subset of columns in a range of rows

You can access or set a subset of column values in a range of rows using startrow, startcol, endrow, and endcol arguments:

// get data values in columns 3 to 6 in rows 1 to 5
Object [ ] O = dw_1.PrimaryData [ 1, 3, 5, 6 ]; 

// get original data values
Object [ ] O = dw_1.PrimaryData [ 1, 3, 5, 6, true ]; 

// set data values in columns 3 to 6 in rows 1 to 5
dw_1.PrimaryData [ 1, 3, 5, 6 ] = O;

Entire data buffer

You can access an entire data buffer using the Rows and OriginalValues properties:

Object [ ] O = dw_1.PrimaryData.Rows;
Object [ ] O = dw_1.PrimaryData.OriginalValues;
dw_1.PrimaryData.Rows = O;

C# example

This C# example is for a DataWindow based on the department table in the EAS Demo database. The DataWindow contains three columns (dept_id, dept_name, and dept_head_id).

// Get a single row 
Object[] obj = dw_1.PrimaryData[1];
MessageBox.Show("dept id :"+obj[0].ToString());
MessageBox.Show("dept name :"+obj[1].ToString());
MessageBox.Show("dept head id :"+obj[2].ToString());

// Get a range of rows from row 1 to 
// row 3 for all columns
Object[] obj = dw_1.PrimaryData[1,3];
For (int i=0;i<3;i++){
   Object[] rowobj = (Object[])obj[i];
   MessageBox.Show("dept id:"+rowobj[0].ToString());
   MessageBox.Show("dept name:"+rowobj[1].ToString());
   MessageBox.Show("dept head id:"
      +rowobj[2].ToString());
}

// Set data values in a single row
int deptid = 600;
String deptname = "Human Resources";
int managerid = 102;
int li_row;
Object[] obj = new Object[3];
obj[0] = deptid;
obj[1] = deptname;
obj[2] = managerid;
li_row = dw_1.InsertRow(0);
dw_1.PrimaryData[li_row] = obj;

Visual Basic example

The following Visual Basic examples use the department and employee tables in the EAS Demo database and are included in the samples available in the Code Examples directory. These statements set the data value of the start_date column in row 1 to December 25, 2005:

Dim D As System.DateTime
D = New DateTime(2005, 12, 25, 1, 25, 0, 0)
dwPrimary.PrimaryData(1, "start_date") = D

These statements filter data from a primary DataWindow and display it in a secondary DataWindow:

'Clear any data from the secondary DataWindow
dwSecondary.Reset()
'Apply a filter to the primary DataWindow
dwPrimary.SetFilter("dept_id = 100")
dwPrimary.Filter()

'Take the filtered rows and set them into 
'the Secondary DataWindow
Dim dwData As Object
dwData = dwPrimary.FilteredData.Rows
dwSecondary.PrimaryData.Rows = dwData