Suddenly getting INDEX OUT OF RANGE exception vb.net - 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

Related

Datagridview add row form behind don't increment CurrentRowIndex

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.

Get the actual value of a BoundField element in a gridview VB.NET

The following image depicts the full gridview I have:
I'm able to get the values of the second column (project_ID) in the back end as follows:
Dim val As String = grdProjects.SelectedRow.Cells(1).Text.ToString
For instance if I press the Select of the third row I'll get back the value 3.
My problem appears when the gridview is updated and has less records as the one in the following image.
If I press the second record I get the value 2 instead of the value 4 that I'd like to get.
Any suggestions please on how to get the actual value of the cell instead of the number of the selected row?
I might be wrong but in VB.net datagrids SelectedRow does not exist, SelectedRows do exist.
Anyway, try this way and let me know if it work:
On the RowEnter or CellClick event use the E.RowIndex to get what you want. Do as follow:
Private Sub DataGrid1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid1.RowEnter
Dim CellValue As String = DataGrid1.Rows(e.RowIndex).Cells("Project_ID").Value.ToString
End Sub
Notice that for the .cells() I use the name of the column, this is better because if in the future you add columns this code will not need to be changed. Easier code maintenance always pay! Also. Cell().Value instead Cell().Text
Nandostyle

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.

Selecting single cell data from selected row - DataGridView

I have a datagridview with data in it, I have set it so you can only select the row and one at a time.
However, I need to get the data from one of the cells in the selected row, I have tried this;
Dim data As String = DataGridView1.SelectedRows.Cells(5).toString
Something like that, but I can't get the value of it, does anyone know the way to do it?
Perhaps the right syntax is:
Dim data As String = DataGridView1.SelectedRows(0).Cells(5).Value.ToString
See MSDN docs
Dim data As String = YourDataGrid.Item("YourColumnHere", YourDataGrid.CurrentRow.Index).Value
hope this help!
SelectedRows requires an index parameter. If you've set it to only select one row, then it will always be 0. Try changing that line to:
Dim data As String = DataGridView1.SelectedRows(0).Cells(5).Value.toString()
'hope this code is also another way............
DataGridView1.SelectedRows(DataGridView1.CurrentRow.Index).Cells(5).Value.ToString()
Rather than use ordinal numbers to identify which row you have selected, if this call is being used inside an event that is related to the datagridview, there should be a value passed to the event called "e" which is a structure of event arguments.
if e.RowIndex is valid for that eventarg structure, you can use it as the value of the row selected:
Dim strCellValue as string = DataGridView1.Rows(e.RowIndex).Cells(6).Value
You can also get e.ColumnIndex from that argument.

SSRS - How to check all rows in a table?

I have a table in a report and a textbox that changes its background color based on the value(s) in the table. Right now I have the background color expression for the textbox set to:
=iif(Me.Value = ReportItems![NewValue].Value, "Yellow", "Transparent")
"NewValue" is the name of one of the columns in the table. The above works fine if the textbox value is in the very first row in the "NewValue" column, but not otherwise.
How do I fix this so it will work if the textbox value shows up in any row in the "NewValue" column?
Sorry, I'm a little new to Reporting Services and haven't seen any functions for table controls.
Instead of ReportItems![NewValue].Value, you can also use ReportItems("NewValue").Value, by the way
I finally got this working yesterday thanks to this blog post:
http://mpasharp.spaces.live.com/blog/cns!5BA71A558863C810!191.entry?sa=340192646
I basically did what he did in the post, modified slightly for my report. I also added a function for getting the row index of the value I was looking for (variables are a little different in mine).
Public Shared Function GetChangedValueIndex(txt As String) As Integer
Dim counter As Integer = 0
For Each s As String In ChangedValueList
If s = txt Then
Return counter
End If
counter += 1
Next
Return -1
End Function
The other caveat is that calling the function to add a value to the array has to occur earlier in the report than anywhere you want to check for it. The table I was working with is actually at the end of the report (and can't be moved for requirements reasons) so I made a copy of that table, moved it to the top of the report, and set it to be hidden.
Unbelieveable that there isn't an easier way to do this.