Devexpress LookupEdit is not displaying the selected row for the last row - vb.net

I am using DevExpress.XtraEditors.LookUpEdit to display the information about the classes available. Currently it has 3 columns. The lookupedit is working perfectly except when I set the editValue to the last row. When the editvalue is set to any row other than the last one it shows the selected row when the lookupedit isn't opened yet when the lookupedit is set to the last row nothing is displayed. Currently I am :
lookupedit.Properties.ForceInitialize() ' Force it to initialize
lookupedit.Properties.PopulateColumns() ' Force the lookupedit to populate
For i As Integer = 0 To tableData.Rows.Count - 1 ' Go through the information in it
If lblClassVal.Text = tableData.Rows(i).Item(1).ToString() Then ' if the current row is equal to the value I want to select
lookupedit.EditValue = i + 1 ' then I set the lookupedit value
End If
Next i
lookupedit.Properties.Columns("class_id").Visible = False ' set two columns to invisible
lookupedit.Properties.Columns("active").Visible = False
lookupedit.Properties.Columns("class_name").Caption = "Class Name" ' set the 3rd column to a better title
Right now the lookupedit displays the selected text unless I select the last row, row number tableData.Rows.Count which then displays nothing. Yet when I print the values they are correct and when I remove the +1 when setting the lookupedit it sets it to the previous line I want an the first row can't be displayed.

Ok I solved this. Just in case someone else if having this problem I will leave the question up.
So the DevExpress LookUpEdit doesn't use the row number, it uses the ID column in your LookUpEdit table. So instead of selecting this by the row number set the EditValue to the ID number of the row you want to select. So instead of:
If lblClassVal.Text = tableData.Rows(i).Item(1).ToString() Then ' if the current row is equal to the value I want to select
lookupedit.EditValue = i + 1 ' then I set the lookupedit value
End If
Use :
If lblClassVal.Text = tableData.Rows(i).Item(1).ToString() Then ' if the current row is equal to the value I want to select
lookupedit.EditValue = tableData.Rows(i).Item(0).ToString() ' Where Item(0) is my ID number column
End If

Related

Compare DataTable and DataGridView and hightlight matching rows in DataGridView

I have a database table which contains a list of usernames and their current active location, an integer called SEQUENCE.
I get this list of sequence numbers into a datatable with a single column (e.g. "SELECT SEQUENCE FROM TABLE"):
dtUsers = CLS_USERS.GetUsers(User)
It excludes the current user, which is why I parse in User.
What I then do is loop through the datatable and for each number I want to set the matching row in a datagridview (which also has a SEQUENCE column) to a different colour. This is my current code:
For Each row As DataRow In dtUsers.Rows
intSeq = row("SEQUENCE")
For Each dgv_row As DataGridViewRow In dgvCandList.Rows
If dgv_row.Cells("CURRENT_SQ").Value = intSeq Then
dgv_row.DefaultCellStyle.BackColor = Color.Cyan
Else
dgv_row.DefaultCellStyle.BackColor = Color.Grey
End If
Next
Next
However, all rows are highlighted rather than just those where dgv_row.Cells("SV_CURRENT_CAND_SQ").Value = intSeq is True... I've verified that this loop is working correctly, so it must be dgv_row.DefaultCellStyle.BackColor = Color.Cyan which is incorrect?
If so, how should I correctly be assigning the row back colour of specific DataGridRow?
EDIT: This code works correctly, my issue was related to bug outside this loop causing the BackColor to be sent for everything, and then never setback to default if where BackColor wasn't being set back to the default if it wasn't in dtUsers.

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.

"Range" object must move along when adding a new row

I'm trying to color different rows depending on an if-statement. Example:
' # 1
If (test = True) Then
Worksheets("sheet1").Range("A1:J1").Interior.Color = varColor1
Else
Worksheets("sheet1").Range("A1:J1").Interior.Color = varColor2
End If
' # 2
If (test2 = True) Then
Worksheets("sheet1").Range("A2:J2").Interior.Color = varColor1
Else
Worksheets("sheet1").Range("A2:J2").Interior.Color = varColor2
End If
' # 3
'...etc
My question is, if I add a new row in the excel sheet, the value of the "Range" becomes incorrect. For instance, if I add a row between A and B the program will color the new inserted row (as this new inserted row becomes B) and the actual B row that I want to color is now C. How can I ensure that when a row is added, the correct row is still colored. Ofcourse I can change the "Range" values manually but I have a lot of them so it will take very long to change all, there must be another way to do it...
If you define a named range (Ctrl-F3) XL will expand the definition if you insert a column within that range.
Worksheets("sheet1").Range("test1_range").Interior.Color = varColor1
Be careful if you insert a column at left edge of the named range - it will not be included into the named range.

Move Index to last row in Datagridview

I want to programmatically delete the last row in my Datagridview when a user clicks a button. As of now, the program will scroll down and highlight the last row, but only deletes the row where the index is at. So, I need to move the indexer (triangle indicator) to the last row. How do I do this??
If LftMtr_Data_Grid.RowCount >= 2 Then
LftMtr_Data_Grid.FirstDisplayedScrollingRowIndex = LftMtr_Data_Grid.RowCount - 2 'scroll to the last row
LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 2).Selected = True 'select the last row
LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.CurrentRow.Selected) 'delete selected row
End If
Thank you
The CurrentRow is the row containing the active cell. By setting the Selected property to true, the row is only highlighted but not active.
To make the last row as active i.e. to make the row as CurrentRow use the CurrentCell property. Set any of the cell in the last row as CurrentCell before removing the row.
LftMtr_Data_Grid.CurrentCell = LftMtr_Data_Grid.Rows(LftMtr_Data_Grid.RowCount - 1).Cells(0);
LftMtr_Data_Grid.Rows.RemoveAt(LftMtr_Data_Grid.CurrentRow.Index);

Get value of one columns in DataGridView with multi select vb.net

I want get value of one columns in DataGridView with multi select in vb.net
when I use
For Each cell In DGNo.SelectedCells
If cell Is Nothing Then
Exit Sub
End If
If Not FirstValue Then
str += ","
End If
str += cell.Value.ToString
FirstValue = False
Next
it take all values DataGridView. How get value of column(0)?
You can get the individual cell values using the DataGridView.Item property which provides an indexer to get or set cell value in the given location.
DataGridView(ColumnNumber,RowNumber)
In your case, use it like below to get cell value from first column in first row.
DGNo(0,0).Value
To get the value of only column 0 in the selected cells,
For Each cell In DGNo.SelectedCells
If cell.ColumnIndex =0 Then
' do work
End If
Next