How to find the number of expanded/collapsed master rows and grouped rows in a DevExpress GridView? - vb.net

I am currently using DevExpress 10.2 within Visual Studio 2010. In a previous question I was trying to print the current user view of a DevExpress GridControl with the user's choice of expanded or collapsed master rows and/or group sections. I was told this was not possible at this time. I have now decided to use the following code:
gvPOinvoiceBanded.OptionsPrint.ExpandAllGroups = False
gvPOinvoiceBanded.OptionsPrint.ExpandAllDetails = False
when I want it to be completely collapsed while printing as by default these are set to true.
I was just wondering if there is someway to check either the total number of expanded master rows or the total number of collapsed master rows. I would also like to do the same thing for the groups as you can have the groups expanded while the master rows are not.

You can get the number of expanded group rows using a loop like this:
Dim ExpandedGroupCount As Integer = 0
Dim Handle As Integer = -1 'group rows have negative row handles
Do Until GridView1.GetRow(Handle) Is Nothing
If GridView1.GetRowExpanded(Handle) Then
ExpandedGroupCount += 1
End If
Handle -= 1
Loop
'Do whatever with expanded group count
MsgBox(String.Format("Number of Expanded Group Rows: {0}{2}Number of Group Rows: {1}",
ExpandedGroupCount, Math.Abs(Handle + 1), Environment.NewLine))
Similarly, you can do this to get the count of expanded master rows:
Handle = 0
Dim ExpandedMasterRowCount As Integer = 0
Do Until GridView1.GetRow(Handle) Is Nothing
If GridView1.IsMasterRow(Handle) Then
If GridView1.GetMasterRowExpanded(Handle) Then
ExpandedMasterRowCount += 1
End If
End If
Handle += 1
Loop
MsgBox(String.Format("Number of Expanded Master Rows: {0}", ExpandedMasterRowCount))
Of course, if you are only checking so that you can see if you need to set the collapse this probably isn't worth the effort. There is no direct property that provides the counts you are looking for.
You could also probably use the events that fire when rows are collapsed and expanded to track the count as it changes. You have to be careful with that though because the event only fires once when expand or collapse all happens. So if you go with that method be sure to check the rowHandle in the event arguments parameter for GridControl.InvalidRowHandle. That is the value used in the case of collapse or expand all.

Related

Using a loop to populate unbound Textbox on a report for all records

I recently came across a document which numbers all record lines from 1 to the end of the document. I then thought the document can be created in our database in form of a report. The numbering has been used in the document for years for easy visualisation and reference.
How do I add this numbering onto the report? I have attempted to use a loop but then I get a repeat of numbering associated with the last record only.
Private Sub Report_Activate()
Dim Counter As Integer
Dim Total As Integer
Total = 0
For Counter = 1 To 10
Total = Counter + 1
Me.txtLine.Value = Counter
'Debug.Print Counter
Next Counter
End Sub
Current Attempt:
Expected: NB Sorting on Description is not important
Use textbox RunningSum property. It is only available on report, not form. No VBA needed. Set ControlSource to: =1. Then RunningSum property to OverAll or OverGroup.
Calculating sequential identifier or any running sum in query or with code gets rather complicated in Access. It is a common topic in many forums.

Gridview compare to list and set value to gridview checkbox

I have a grid view which has 3000 rows and one checkbox column and 2 textbox column.
I have a parameter list around say around 1700. it varies sometimes 5 or 10.
So I need to compare the list to the grid view and set the 1st checkbox column to true.
I use the compare and select method like below code which works well for a small number of parameter like 50,70
If Paramlist.Count > 0 Then
Dim row As Integer = 0
For Each listItem In Paramlist
Do While (row < gridview1.RowCount)
If Trim(gridview1.Rows(row).Cells(1).Value) = Trim(listItem) Then
gridview1.Rows(row).Cells(0).Value = True
Exit Do
End If
row = row + 1
Loop
Next
End If
It takes a lot of time to compare and set gridCheckbox value to true when I do it with more parameter list and the application seems like it is hanging when the user uses it.
Could anyone suggest me a faster way with less time complexity / best practice to set the grid check box value to true in the grid view.
[UPDATE]
I have attached a image of the exception thrown.
System.Invocation.TargetInvocationException

If Then to Loop through Pivot Items

I'm stuck on this issue and cannot figure out what it is I am doing wrong. I'm in the process of writing a macro to loop through visible fields in a pivot table, find the items in those fields that are filtered, and then apply those filters to the source data table. The particular problem I'm having deals with a loop that is supposed to look in a field, find the visible pivot items, and then store those items as variables. I've been messing around with trying to terminate the loop if all pivot items are visible. Below is what I have so far:
Sub PivotTest2()
Dim pi As PivotItem
Dim count As Integer
Dim available As Integer
Dim row As Integer
available = ActiveSheet.PivotTables("Cities").PivotFields("City").PivotItems.count
row = 2 'This is your starting row for display
For Each pi In ActiveSheet.PivotTables("Cities").PivotFields("City").PivotItems
If pi.Visible Then
count = count + 1
End If
If count = available Then
MsgBox ("All Pivot Items Are Selected")
End If
If count <> available Then
row = row + 1
Range("'Sheet4'!G" & row) = pi.Name
End If
Next pi
End Sub
So whenever I have a filter on a city, or group of cities, the macro pastes them to a range in the spreadsheet. Eventually, I want to store them as variables, but for now I'm just pasting them for the purposes of working through the code. You can also see that when the number of visible items is equal to the total number of pivot items in that field, I'm returning a message box. Eventually, that message box will go away and be replaced by code that tells it to start looking through pivot items in the next visible field.
I realize that this might be unnecessary, as applying all visible items as filters to the source data field would result in all items being shown, which accomplishes the same thing. However, I'm more curious about what it is I'm doing wrong at this point. The code pastes all items to the range, regardless of whether there is a filter or not. However, the message box only returns when there is no filter applied, so it appears to be working correctly.
Any input/feedback would be appreciated. Thanks.
It seems that part of your checks should be outside your loop which determines the amount of visible PivotItems.
At first, I'd suggest to move the Next pi after the first If-End If block. Your count variable should then contain the number of visible items. If all items are visible, your message box should then appear. However, for the second part (copying visible items), you'd need to loop again.
To avoid the second loop, you may want to copy the pi.Name values to a temporary range, and copy this range to the final range only if there are filters applied.
Try this:
For Each pi In ActiveSheet.PivotTables("Cities").PivotFields("City").PivotItems
If pi.Visible Then
count = count + 1
Range("'Sheet4'!G" & row) = pi.Name
row = row + 1
End If
Next pi
If count = available Then
MsgBox ("All Pivot Items Are Selected")
End If
Your code doesn't check if the city is visible before pasting, which is why it always pastes every city name.

DevExpress XtraGrid GroupRow, CheckEdit Interactions Possibilities

Currently, I am displaying an XtraGrid that contains Group Rows. I have a "Select All" DevExpress.XtraEditors.CheckEdit control (which is different from this elusive "Select All" check box control I am reading about in the documentation). It is different for a reason: I want the check box to do something other than "Select All" (which comes in only three different varieties according to the DevExpress Docs.).
I want a user to be able to do one of two things with the CheckEdit control. [1] If no Group Rows are expanded, I want to select all Group Rows. [2] If one or more Group Rows are expanded, I only want to select the expanded rows.
Presently, I am able to manipulate the controls to do only one of the two things (see code). My question is twofold: is this possible; and, if so, how would I go about it?
Here is my code that does the second of the two 'things' described above:
'If the CheckEdit control is checked:
xtraGrid.SelectAll()
Dim rowHandles() As Int32 = xtraGrid.GetSelectedRows()
If rowHandles.Count > 0 Then
For Each RowHandle As Int32 In rowHandles
If xtraGrid.IsGroupRow(RowHandle) Then
xtraGrid.UnselectRow(RowHandle)
End If
Next
End If
As you can see, all this really is just a work around. There is also presumably more overhead than needed in my calling .GetSelectedRows(). I am just attempting to sort through the row types in order to keep the row selected or .UnselectRow()
You can use GridView.GetRowExpanded method to check whether a specific group row is expanded. For iterating through visible rows you can use GridView.RowCount property. To get row level just use GridView.GetRowLevel method. Also you need to check wether a row is new item row or is filter row by using GridView.IsNewItemRow method and GridView.IsFilterRow method. For all of this methods you need to get Row handle by using GridView.GetVisibleRowHandle method. When you are working with selection is better to use GridView.BeginSelection method and GridView.EndSelection method outside of your code.
UPDATE: If you want to select hidden rows within a collapsed group then you can use GridView.GetChildRowCount method and GridView.GetChildRowHandle method to get all hidden rows.
Here is some sample code that is performing your two things together:
Private Sub SomeSub
If xtraGrid.GroupCount = 0 Then
xtraGrid.SelectAll()
Exit Sub
End If
xtraGrid.BeginSelection()
xtraGrid.ClearSelection()
Dim isExpanded As Boolean = False
For rowVisibleIndex = 0 To xtraGrid.RowCount - 1
Dim rowHandle As Integer = xtraGrid.GetVisibleRowHandle(rowVisibleIndex)
If xtraGrid.IsNewItemRow(rowHandle) Then
Continue For
End If
Dim level As Integer = xtraGrid.GetRowLevel(rowHandle)
If level = 0 Then
If Not isExpanded Then
isExpanded = xtraGrid.GetRowExpanded(rowHandle)
If isExpanded Then
xtraGrid.ClearSelection()
Else
xtraGrid.SelectRow(rowHandle)
End If
End If
Else
xtraGrid.SelectRow(rowHandle)
'Update: select hidden rows
If xtraGrid.IsGroupRow(rowHandle) And Not xtraGrid.GetRowExpanded(rowHandle) Then
SelectRowHierarchy(rowHandle)
End If
End If
Next
xtraGrid.EndSelection()
End Sub
Private Sub SelectRowHierarchy(rowHandle As Integer)
Dim childCount As Integer = xtraGrid.GetChildRowCount(rowHandle)
For childIndex As Integer = 0 To childCount - 1
Dim childRowHandle As Integer = xtraGrid.GetChildRowHandle(rowHandle, childIndex)
xtraGrid.SelectRow(childRowHandle)
If xtraGrid.IsGroupRow(childRowHandle) Then
SelectRowHierarchy(childRowHandle)
End If
Next
End Sub

Get the total number of master rows and grouped rows in a DevExpress GridControl

In a previous question I posted I found out how to keep track of the currently expanded grouped rows by using 2 events:
- gridview.GroupRowExpanded
- gridview.GroupRowCollapsed
where I increase or decrement an integer that keeps track of how many group rows are currently expanded. I am now tackling the problem of what to do if the user expands or collapses all of the group rows. I currently know when this is done by checking the e.RowHandle.
I was wondering if there is a way to find the total number of groups rows currently in the GridView (something like rowcount for normal rows) so I know how many to set the tracking integer to.
For Example:
If my current count is 2 and the total number of groups are 15 then when the Expand All is fired the current count is set to 15 rather than 3.
Every detail when expanded is its own view. So to get the master row count, you can use a count of the rows in the MainView property like:
GridControl1.MainView.RowCount
To get the number of group rows:
Dim Handle As Integer = -1 'group rows have negative row handles
Do Until GridView1.GetRow(Handle) Is Nothing
Handle -= 1
Loop
Dim count As Integer = Math.Abs(Handle + 1) 'number of group rows
Alternately, you could use your datasource & linq something like:
Dim count As Integer = (From item As Class1 In List Group By item.Something Into AsEnumerable()).count
but as far as I am aware there is no direct property for that one.