Description:
My grid can be grouped. I need to write a routine to process all (group and data) rows in the order a user sees them in the grid. Can you help me with creating the relevant code?
Answer:
The following code iterates through all the visible grid rows.
If a grid is not grouped (the GroupedColumnCount is zero), rows with handles from 0 to DataRowCount - 1 are added to a list box.
If the grid is grouped, the loop uses group row handles, which are negative. The child data rows of a given group row are accessed via the GetChildRowHandle method.
C#private static void TraverseRows(GridView gridView)
{
if (gridView.GroupCount == 0)
for (int rowHandle = 0; rowHandle < gridView.RowCount; rowHandle++)
{
foreach (GridColumn gc in gridView.Columns)
WriteString(String.Format("{0} ", gridView.GetRowCellDisplayText(rowHandle,gc)));
WriteString("\r\n");
}
else
{
// Get the list of grouped columns
List<GridColumn> groupedColumnsList = new List<GridColumn>();
foreach (GridColumn groupedColumn in gridView.GroupedColumns)
groupedColumnsList.Add(groupedColumn);
for (int rowHandle = -1; gridView.IsValidRowHandle(rowHandle); rowHandle--)
{
WriteString(gridView.GetGroupRowDisplayText(rowHandle) + "\r\n");
if (gridView.GetChildRowHandle(rowHandle, 0) > -1)
for (int childRowHandle = 0; childRowHandle < gridView.GetChildRowCount(rowHandle); childRowHandle++)
{
WriteString("-- ");
foreach (GridColumn gc in gridView.Columns)
{
if (groupedColumnsList.Contains(gc)) continue;
WriteString(String.Format("{0} ", gridView.GetRowCellDisplayText(gridView.GetChildRowHandle(rowHandle, childRowHandle), gc)));
}
WriteString("\r\n");
}
}
}
}
See also:
Rows
Traversing Rows
Process Group Rows
Is this solutions still valid? I ask not only because it is so old, but because the first example seems to contain an error. Granted, I did convert to VB, but the following line compares incompatible data types:
for(int i = -1; view.IsValidRowHandle(i); i--) {
i is an integer and IsValidRowHandle returns a Boolean.
Hello,
This code seems correct.
You can create a VB analog of this code using the While loop as follows:
Dim i As Integer = -1 Do While view.IsValidRowHandle(i) i -= 1 '... Loop
Please note that the first part of the KB article is related to version 2 of our controls. If you're using a newer version, please use the second approach. I hope this helps.
Sorry for delayed response, I did not receive a notification of your response.I am sure that your VB code will work fine, however it is using a Do While loop, where the C# code is using a For loop and is comparing different data types. Would not the equivalent VB code be For i = -1 to view.IsValidRowHandle() step -1…NextOr is it just that my C# is too rusty?
Hello,
Unlike in C#, in VB the For loop does not have capability to operate a condition. So, in VB, the While loop is an analog of the following code:
for(int i = -1; view.IsValidRowHandle(i); i--)
I hope this helps.
Ok, thanks. So, it was my rust :).
You are welcome. Please contact us if you need further assistance.