Gridview compare to list and set value to gridview checkbox - vb.net

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

Related

i can't fill my gridview vb.net

In my project , i have 2 windows forms and 1 gridview contains 5 columns (name gridv)
I want to fill 4 columns from forms1 and the 5th columun from the forms2
but i can't do it (error in my appli execution )
my code (how to fill the 5th column)
Dim selectrow As Integer = Form1.gridv.CurrentRow.Index ' selectrow mean selected row indice
MessageBox.Show("ligne selectionnée : " & Convert.ToDouble(selectrow))
Form1.gridv.Rows.Add(corp_mail.Text = Form1.gridv.Item(4, selectrow).Value) ' fill the 5th column`
You haven't shown the error but from what information you have given this is what your code is doing:
evaluates if corp_mail.Text is equal to the value of the 5th column of the selected row. This is a true or false, not a string.
Adds (or tries to) a new row to the grid with true or false as the value of the first cell.
If I understand correctly, this is what you want to do:
Form1.gridv.Item(4, selectrow).Value = corp_mail.Text
This will set the value of the 5th column of the selected row.
Although you mention a second grid on another form, not a textbox, so you probably have more work to do. But the idea is the same, instead of the TextBox value get the value from the other grid.

VBA ComboBox Value by Index

I've got a form that contains a combobox which contains the values
With io6
.AddItem "90°"
.AddItem "180°"
.AddItem "270°"
.AddItem "360°"
End With
The member of this method I am actually using is .ListIndex. When a user selects 270 degrees, I am sending a 2 to a variable. When I read the variable, I want have the combobox correctly show the appropriate value. In other words, if the variable is 1, I want the combo box to show 180 degrees. Is that achievable without a select\if statement that writes into .Value?
Sub varChange(val as integer)
With comboBox
Select Case val
Case 1
.value = "90°"
Case 2
.value = "180°"
Case 3
.value = "270°"
Case 4
.value = "360°"
End Select
End With
End Sub
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
I'm not sure if I got it right, but I think you want to get the list index of the selected item.
You can get that by
x = io6.ListIndex + 1
The +1 is there since ListIndex is 0 based, but you wanted it 1 based.
And something completely different,
Remove the brackets. Method calls without return values does not use them in VBA
Edit after comment
To take this the other way, i.e getting a value from an index value, do like this:
y = io6.List(x - 1)
sValue = combobox.List(combobox.ListIndex,0)
The most direct I can ask this question is, can the method element be set based on the index? Can I set the combo box element based on the index rather than having to write in a new value?
Yes,
I used the following line:
io6.ListIndex = val - 1
So in your code it's:
Sub varChange(val as integer)
comboBox.ListIndex = val - 1
End Sub

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

For Loop to count checked items on datagrid not counting correctly

I worked out the following loop to count and display how many rows on my datagrid are checked. However, the loop is ignoring my first checked row. The count does not start at 1 until I have checked the second row. The same happens when I uncheck, the values are off by one.
Dim chkRowCount As Integer = 0
For Each row As DataGridViewRow In dgvAssignGridView.Rows
If row.Cells(6).Value = True Then
chkRowCount += 1
Else
chkRowCount += 0
End If
Next
lblChkCount.Text = chkRowCount.ToString
I have tried setting the variable to 1 instead of 0, but that had some unwanted results.
I am guessing you have this code in CellContentClick. The problem is that the code in that routine fires before the value of the checkbox is actually changed. However, you can basically force the DataGridView to verify itself first by putting the following line right before your code.
dgvAssignGridView.EndEdit()
That forces the cell click to register before you do your count.

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

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.