Datagridview add row form behind don't increment CurrentRowIndex - vb.net

I have found a bug on datagridview (vb.net), if I add a row from code, the value of DataGridView1.CurrentRow.Index is always 0.
If I add row manualy (with mouse) the value of DataGridView1.CurrentRow.Index is right.
I have a function that add some rows from code and I have implement DataGridView1_CellValueChanged and I check if the first cell in selected row is empty or not, but this check always first row.
I've found this bug in this link, https://developercommunity.visualstudio.com/t/datagridview-event-rowsadded-doesnt-show-the-corre/2270 and microsoft don't want to fix it.
I can use a simple variable boolean for fix this bug, but maybe someone can help me for resolve definitly this error?
Thanks

That's not a bug. Microsoft even stated as much in the report you linked to. "By design" means not a bug.
The CurrentRow is the row that contains the CurrentCell and that is the cell that contains the caret. When the user adds a row in the UI, it is assumed that they will want to edit that row, so the grid moves the caret to the first cell in that row. When you add a row in code, there's no specific expectation that the user is going to want to edit that row because it wasn't the user who added it, so the caret stays where it is.
If you want a newly added row to be the CurrentRow then you need to assign a cell in that row to the CurrentCell property of the grid, e.g.
Dim newRowIndex = myDataGridView.Rows.Add()
Dim newRow = myDataGridView.Rows(newRowIndex)
Dim firstNewCell = newRow.Cells(0)
myDataGridView.CurrentCell = firstNewCell
You would probably put code like that into a method and then call that method whenever you wanted to add a row.

Related

Vb.net Move A Datagridview Row to the last row when being edited

I haven't found exactly what i need in the forums. I don't want to move selection or scroll index, all what I need to is to change the entire row with a specific index to be the last line in my datagridview.
I need this to happen automatically when i edit this row.
P.S. Datagrid is Unbound
for example:
dgv.rows(index).index=dgv.row.getlastrow
However, the debugger return an error that index is read only.
Please help guys!
This worked for me:
Dim curr_row As DataGridViewRow = idgv.Rows(Index)
Dim curr_index As Integer=idgv.Rows.GetLastRow(DataGridViewElementStates.Visible)
idgv.Rows.Remove(curr_row)
idgv.Rows.Insert(curr_index, curr_row)

Suddenly getting INDEX OUT OF RANGE exception vb.net

my form has a dgvw and a textbox.When i click on any of the rows in the dgvw, i've coded the textbox to show the 3rd cell's value in it .The code is :
txt1.text=dgvw1.currentrow.cells(2).value
this works fine...but when i use this code instead :
txt1.Text = dgvw1.SelectedRows(dgvw1.CurrentRow.Index).Cells(2).Value.ToString
it gives me the index out of range exception!! Why ?? I don't get it! If the first code works,the second one should work as well!Why is it throwing an exception?
SelectedRows and CurrentRow are independent. SelectedRows might not have any rows in it, it only contains the highlighted (blue) rows. CurrentRow is where the cursor is. This could be inside or outside the selection.
DataGridView.CurrentRow.Index is an index into the DataGridView.Rows collection and shouldn't be used with DataGridView.SelectedRows. DataGridView.SelectedRows is a subset of DataGridView.Rows
Try:
txt1.Text = dgvw1.Rows(dgvw1.CurrentRow.Index).Cells(2).Value.ToString

Checkbox in a DataGridView column is not getting checked sometimes?

I use this code to check all the check-boxes in a data grid view in VB.NET (2010 - .NET Framework 4.0),
Dim i As Integer
For i = 0 To (DataGridView.Rows.Count - 1)
DataGridView.Rows(i).Cells(0).Value = True
Next
Sometimes when one of the row is selected (not checked but just the entire row is selected) while this code is running, it checks all other check-boxes in the column except the selected one. What is wrong here?
I also try adding this code,
DataGridView.SelectedCells(0).Cells(0).Value = True
Did not work.
Like I said in the comments, the issue is that the box is checked in the datasource, but the GUI hasn't refreshed that cell to reflect it. If you click off of it, the cell should change/refresh to your new value. After you have made your data changes, at the very end you should deselect cells/rows. You can do it by:
Datagridview.ClearSelection()
or
Datagridview.CurrentCell = Nothing 'deselects
You get the idea. This is what has worked for me in the past.

Can't clear a DataGridView combobox without 100s of errors

I'm using VB.NET to make a stand-alone executable containing an unbounded DataGridView.
One of its columns is a combobox. I add rows to the combobox with some simple code
like this:
For r As Int16 = 0 To eachLine.GetUpperBound(0)
Dim dgvcbc As DataGridViewComboBoxColumn = grd.Columns(col)
' Errors: dgvcbc.Items.Clear()
dgvcbc.Items.Add(eachLine(r))
Next r
Works great the first time, but when I try to clear it, in order to add some different
items in the combobox, I get this error 100s of times:
> DataGridViewComboBoxCell value is not valid
Any ideas on how to fix this and why it occurs?
The issue is that, if you clear the Items in that column, every cell in that column becomes invalid. Every value in that column has to match one of the items in the drop-down list and, if there's no items, no value can be valid. It doesn't make sense to clear the drop-down list. You must leave at least the items that match the values in the cells of that column.

figure out rowindex on a row I just inserted?

I've got a vb project I am updating and it has a datagridview, a panel, and buttons along the bottom. The buttons control CRUD operations. The panel shows numerical up/downs and textboxes to represent the selected row.
I use some logic to keep track of current selected row and current row index for timer updates in the background. Upon delete I reset focus on the first row. Upon loading I set focus on first row. On update I keep current row focus.
Upon a good insert I would like to set the focus to the row I just inserted. But I do not know a way to determine what the rowindex value is for this freshly inserted row. My datatable which the datagridview uses is sorted on two id columns so it's not like the new entry will just jump to the bottom.
Any ideas on how I can implement this?
If you don't want to deal with the RowAdded event as Jay Riggs suggested, if you add it using a row object, as opposed to just Rows.Add, you should be able to pull that off of the row object after it's inserted.
Dim dgr As New DataGridViewRow()
DataGridView1.Rows.Add(dgr)
Me.DataGridView1.Rows.IndexOf(dgr)
This should also work for insert as well.
Check out the DataGridView RowAdded event; its DataGridViewRowsAddedEventArgs parameter includes the RowIndex property which gives you what you need.
The event fires everytime a row is added, so you'll have to either wire the event up when you want to check for added rows, or ignore the event when you don't care when a row is added (such as when your grid fills with data).
If you manually add rows to the DataGridView, then you can just use the return value from the Add method. Examples:
Dim newRowIndex As Integer = myDataGridView.Rows.Add()
myDataGridView.Rows(newRowIndex).Selected = True
No need to call IndexOf. If the DataGridView is bound to a DataSource then Jay Riggs' solution of using the RowsAdded event is the way to go.