Datagridview.Rows(#).Selected not updating CurrentRow.Index - vb.net

VB.Net 4.6.1
Windows Forms application
I have a bound datagridview that I'm trying to sort and select. The data loads fine and I can click the headers (with some code) and the sorting works well. I'm working on reselecting a record after the sort is complete and I'm running into some trouble.
Assume that I have a datagridview with 20 records in it. On the form I also have a button that runs the following:
MessageBox.Show(MyDataGridView.CurrentRow.Index.ToString)
I also have a Sub that handles MyDataGridView.Sorted and in it I have one test line as follows:
MyDataGridView.Rows(2).Selected = True
After the data loads I click on the 11th record (selectionmode fullrowselect) and then I click the button. Box pops up and says "10" (0 based index). I click one of my column headers, the data sorts and the highlight bar jumps up to the 3rd record (index 2). However when I hit my currentrow.index button it still says 10. Additionally if I hit the down arrow key the selection bar jumps down to the 11th (index 10) record in the list.
It seems that using "Selected" doesn't update the currentrow.index value and since it is read-only I can't force it. Can anyone educate me on what's going on because, as it is, my users would be confused if the arrow up and down keys didn't work properly after a sort.
Thank you!

"Current" and "selected" do not mean the same thing. How could they, when multiple rows can be selected but there can only be one current row? The current row is the row that currently contains the caret and a selected row is one that is highlighted. If you want the row at index 2 to be the current row then you need to make a cell in that row the current cell, i.e. assign a cell in that row to the CurrentCell property.

Related

VBA MSHFLEXGRID wrong .ROW information in the CLICK event

I'm using the VBA MSHFLEXGRID, 2 grids in fact in the same Form, one below the other so when I choose a row in the first grid, the second one shows info regarding the first grid row selection.
Well, the problem is that the second grid, after choosing a row in the first grid which brings to no information in the second one (I put the data through the own grid recordset, and if i dont want it to show information, i do an exit in the load procedure, it gets crazy and the .ROW property in the Click event starts returning a fixed number, like for example 4, instead of the actual row being clicked. Always a number that represents a higher row than the last one (one that is even out of the grid). I tried also the .MOUSEROW and this one returns 0 all the time (when the grid becomes crazy).
This never happens with the first grid, only with the second one. There's nothing I can do without real information about the row being clicked. I've seen that with different resolutions the problem gets reset. Seems not to happen with low resolutions like 1360x768, but it does with 1920x1080 and 2560x1440.
SOLVED! I was trying cases and more cases to try to understand why it happens sometimes in random cases and finally found it. The problem appears when you set the recordset directly to the grid (Set Grid.Recordset = xxxx) and this xxx is empty. It is, EOF. So I was doing a .Clearstructure, and then the SET. After this, if i did another SET (another selection of the second grid) with a non empty recordset, then it would go crazy.
So what I did was simple assigning first a recordset object to check if the SQL query is empty or not, and if it's not empty and only then, assign the recordset to the grid. Oh my god, this was bringing me crazy after a long time.

Excel VBA mulitiple checkboxes

is it possible to create listbox with multiple checkboxes in one row (Excel VBA)?
Thanks
Kamil
I'm not sure I understood your question fully, but I'll elaborate on ListBoxes as much as I can.
First things first: Checkboxes and ListBoxes are different objects in Excel Userforms. The first is the little box that returns a "true/false". The second is a list of items which can be chosen. Clicking in a Checkbox will make the tick mark appear/disappear (or fade if tristate is enabled), while clicking a Listbox row will turn the listbox row "blue"/"white" (or whatever color is being used for the selected rows). In both elements, clicking is a way to toggle between True and False.
While a checkbox only allows for a single information to be marked as True or False, a Listbox allows you to select entries out of a list. That list may be inserted through code (.AddItem method) or passed from a range (.RowSource property)
ListBox objects allow for multiple columns of data to be attributed to one row element, but each row is an entire element (which means you cannot pick the element on row 3, column 2 - only all of row 3). The number of columns is established using the ColumnCount property.
By changing the value of the MultiSelect property, you'll allow the user to select multiple or single row elements simultaneously on your Listbox. Using the Selected( RowIndex ) property, you can check whether or not an item is currently selected (returns True/False). Remember that row indexes start at 0.
Finally, if you're using the MultiSelect property set to fmMultiSelectSingle and have a single column (as far as I know), the Text property can be used to return the selected item's value.
An easy example of a listbox is in Excel can be found at File > Options > Customize Ribbon (or something like that). There are two listboxes, one (on the left) with the visible items and another with the available items. A pair of command buttons is used to move items between boxes. That's a simple application you can likely find already setup online.
Am I on track to answer your question?

Delete function first or last record

My main menu is where all records are displayed. The user can navigate between records by a list table which has all the records.
When i use the this code to delete a displayed record, allways the first record gets deleted.
BindingContext(JobsDBDataSet, "T_Jobs")
.RemoveAt(BindingContext(JobsDBDataSet, "T_Jobs").Position)
Ive added an extra line in which the position should match the ID of the record.
BindingContext(JobsDBDataSet, "T_Jobs").Position = Job_IDTextBox
BindingContext(JobsDBDataSet, "T_Jobs")
.RemoveAt(BindingContext(JobsDBDataSet, "T_Jobs").Position)
NOTE: Job_IDTextBox is directly connected to the column in the database (I dragged it from the datasource menu into the form in details form)
My goal is so that i can delete the job that has been selected (and displayed) from the list box (with all records in it)
Right so ive changed my code to
BindingContext(JobsDBDataSet, "T_Jobs").Position = Convert.ToInt32(Job_IDTextBox.Text)
BindingContext(JobsDBDataSet, "T_Jobs").RemoveAt(BindingContext(JobsDBDataSet, "T_Jobs").Position)
And the same thing still happens: last job is deleted and not the selected one (from the listbox). I have a feeling its a problem regard the listbox, and maybe I'd firstly have to tell the program to change its pointer to the job ive chosen from the list box but i have no idea how to do this - and this is just an assumption.

Edit datagrid without clicking

I have a UltraGridWinGrid and I need to naviguate through it without using the mouse.
I can get to the grid with Tabs.
I can give focus to any rows with Tabs and Arrow Keys.
Once I highlight a row, how can I select a specific cell to edit it's content ? (All my cells are editable)
If you can modify the code that is executed when the TAB key is pressed, you can do manually select the next cell and start editing like this :
DataGridView.CurrentCell = theNextCell;
DataGridView.BeginEdit(true)
You still need to calculate which cell is next. This should be easy, increment the column index when you press tab, however if you reached the last column, just change row and set index to 0.
For the arrow keys, you could increment/decrement column or row index, depending of the arrow, and apply the same code as above.

Moving a row to the top of an unbound datagridview

I have a form created which an unbound datagridview that has three columns. "Position", "ID", "Name". The data is being added at runtime into a database upon the forms completion. (I don't want to add unnecessary rows to the database in the event a person clicks Cancel.)
What I need to do is on the deletion of a row in the datagridview, search through the rows "Position" for a "HJ" value. If it's not found, change the "Position" value of the first row to be "HJ".
Here's a photo of what I mean:
I've already taken care of the "Add Judge" button. The "Make Head Judge" button I guess will be on another question.
EDIT: Code removed, not needed since I found a function to search through a datagridview for me.
I found information about FirstDisplayedScrollingRowIndex but I can't seem to get it to work for some reason. Any assistance is greatly appreciated.
I would add a hidden column "rank" which could be used to sort the grid. Enumerate each of your ranks: HeadJudge = 0, AsstHeadJudge = 1, Judge = 2. If you will only have one head judge, then sorting by rank will always place the HeadJudge in the first row.