/// <summary> /// Answers whether the data in the column named columnName in the child table all have the same value. /// </summary> /// <param name="ds">The Dataset</param> /// <param name="relationName">The index of the parent row which is being considered for this check.</param> /// <param name="parentRowIndex">The name of the relation between the parent and child table. </param> /// <param name="columnName">The DataColumn to look for</param> /// <param name="ignoreNullCells">Should we ignore NULL cells</param> /// <returns></returns> public static bool columnInChildRowsHaveSameValue(DataSet ds, string relationName, int parentRowIndex, string columnName, bool ignoreNullCells) { bool haveSameValue = true; DataRelation dataRelation = ds.Relations[relationName]; // get the child rows which correspond to the parent row DataRow[] childRowsArray = dataRelation.ParentTable.Rows[parentRowIndex].GetChildRows(relationName); IEnumerator childRowsCollection = childRowsArray.GetEnumerator(); if (childRowsArray.Length < 1) { // special case // (from bug 267) // it appears that if one tries to group by one or more columns and all child rows happen to have DBNull in the corresponding // columns that GetChildRows will not find any matching child rows (when it seems that it should have returned all rows) // Under these circumstances we'll try to do a select distinct on the grouping columns and, if we get exactly one data item // back, we'll set the childRowsArray equal to all of the rows in the child table. // a better solution would be to somehow involve the GetChildRows call directly but I didn't find anything in my // web searching which talked about this situation. DataSetHelper dsh = new DataSetHelper(); DataTable distinctValuesTable = dsh.SelectDistinct("distinctChildValues", dataRelation.ChildTable, columnName); if (distinctValuesTable.Rows.Count > 0) { childRowsCollection = dataRelation.ChildTable.Rows.GetEnumerator(); // select everything } else { throw new ApplicationException("No child rows for relation name [" + relationName + "] parent row index [" + parentRowIndex + "]"); } } // finally check for matching value of the column within all the child rows. object firstCellValue = null; bool firstCellValueWasSet = false; DataRow childRow; while (childRowsCollection.MoveNext()) { childRow = (DataRow)childRowsCollection.Current; if (!firstCellValueWasSet) { if (((isNull(childRow[columnName])) && (!ignoreNullCells)) || (!isNull(childRow[columnName]))) { firstCellValue = childRow[columnName]; firstCellValueWasSet = true; } } else { if (isNull(childRow[columnName])) { if (!ignoreNullCells) { if (!isNull(firstCellValue)) { haveSameValue = false; break; } } } else { if (!firstCellValue.Equals(childRow[columnName])) { haveSameValue = false; break; } } } } return(haveSameValue); }
/// <summary> /// Answers whether the data in the column named columnName in the child table all have the same value. /// </summary> /// <param name="ds">The Dataset</param> /// <param name="relationName">The index of the parent row which is being considered for this check.</param> /// <param name="parentRowIndex">The name of the relation between the parent and child table. </param> /// <param name="columnName">The DataColumn to look for</param> /// <param name="ignoreNullCells">Should we ignore NULL cells</param> /// <returns></returns> public static bool columnInChildRowsHaveSameValue(DataSet ds, string relationName, int parentRowIndex, string columnName, bool ignoreNullCells) { bool haveSameValue = true; DataRelation dataRelation = ds.Relations[relationName]; // get the child rows which correspond to the parent row DataRow[] childRowsArray = dataRelation.ParentTable.Rows[parentRowIndex].GetChildRows(relationName); IEnumerator childRowsCollection = childRowsArray.GetEnumerator(); if (childRowsArray.Length < 1) { // special case // (from bug 267) // it appears that if one tries to group by one or more columns and all child rows happen to have DBNull in the corresponding // columns that GetChildRows will not find any matching child rows (when it seems that it should have returned all rows) // Under these circumstances we'll try to do a select distinct on the grouping columns and, if we get exactly one data item // back, we'll set the childRowsArray equal to all of the rows in the child table. // a better solution would be to somehow involve the GetChildRows call directly but I didn't find anything in my // web searching which talked about this situation. DataSetHelper dsh = new DataSetHelper(); DataTable distinctValuesTable = dsh.SelectDistinct("distinctChildValues", dataRelation.ChildTable, columnName); if (distinctValuesTable.Rows.Count > 0) { childRowsCollection = dataRelation.ChildTable.Rows.GetEnumerator(); // select everything } else { throw new ApplicationException("No child rows for relation name [" + relationName + "] parent row index [" + parentRowIndex + "]"); } } // finally check for matching value of the column within all the child rows. object firstCellValue = null; bool firstCellValueWasSet = false; DataRow childRow; while (childRowsCollection.MoveNext()) { childRow = (DataRow) childRowsCollection.Current; if (!firstCellValueWasSet) { if ( ( (isNull(childRow[columnName])) && (!ignoreNullCells) ) || (!isNull(childRow[columnName]))) { firstCellValue = childRow[columnName]; firstCellValueWasSet = true; } } else { if (isNull(childRow[columnName])) { if (!ignoreNullCells) { if (!isNull(firstCellValue)) { haveSameValue = false; break; } } } else { if (!firstCellValue.Equals(childRow[columnName])) { haveSameValue = false; break; } } } } return haveSameValue; }