Get the actual value of a BoundField element in a gridview VB.NET - 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

Related

Selected ListView item column value

I have created a listview,
and id like to be able to change the selected item values. I wonder if is possible to somehow access the "ID" string of the selected item.
I created an event to detect when the user selects an item and tried colID.Index.ToString but it just returns the actual column index. Anyone able to provide a syntax that will return the selected item ID?
Private Sub lvwCars_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lvwCars.SelectedIndexChanged
currentCarIDString = colID.Index.ToString
end sub
It's either going to be the item text or subitem text.
currentCarIDString = lvwCars.Items(colID.Index).Text
or
currentCarIDString = lvwCars.Items(colID.Index).SubItems(1).Text
I found something that works for my specific case
ListView.FocusedItem.Index
since my ID is always going by up by one, this solution works perfect. However if anyone can offer a solution that can retrieve the actual value of a row of a column, that would be great.

VB Datagridview cellvalidating event to check if active cell is empty berore leaving problems

In Visual basic i'm trying to make a field required by preventing the user to leave the active cell if it's empty.
The grid consists out of two columns. The first one is the ID which is automaticly filled in on adding a new row, the second one is the name.
On form load there are 4 items loaded into the grid. When the form is loaded the first cell that is selected passes the check with no problem. It is the same for the three cells that are filled on form load.
Unforunately it does not seem to work when i try to add a new row to the grid, enter a value and try to leave the cell. I've set a break point and checked the values. The value is equal to nothing... This is not the case with any of the already loaded values.
How does it not get the value i enter?
Thanks in advance.
Ivan.f
Private Sub ValidateCellValue(sender As Object, e As
DataGridViewCellValidatingEventArgs)
Handles grdResult.CellValidating
Dim catNaam = grdResult.Rows(e.RowIndex).Cells(1).Value
If IsNothing(catNaam) Then
MessageBox.Show("Veld mag niet leeg zijn")
e.Cancel = True
Exit Sub
End If
End Sub

Binding combobox selected value to a specific column of current row (not a datagridview)

I have a database that I am using in a VB 2010 project. What I did was removed a textbox for a database field that I dragged onto the form and replaced it with a combobox. The field name was orderstatus.
The problem is this: since I removed the textbox field from the form, I can't seem to bind the combobox value to the field in the active record. In other words, orderstatus textbox doesn't exist anymore. I want to let the user pick a status from the combobox and store that value to orderstatus so that it's saved to the current record my database.
I want to do something like this:
Private Sub Button16_Click_1(sender As System.Object, e As System.EventArgs) Handles Button16.Click
orderstatus = ComboBox13.SelectedValue
Me.OrdersDataSet.orders(0).orderstatus = orderstatus
Me.Validate()
Me.OrdersBindingSource.EndEdit()
Me.TableAdapterManager12.UpdateAll(Me.OrdersDataSet)
End Sub
but it doesn't like my second line where I try to assign the value to the field, saying there is no row 0. All I want to do is put the selected value of the combobox into the orderstatus field of the record being created (or updated).
I've also tried using:
Me.OrdersDataSet.orders.orderstatusColumn = orderstatus
and I get a message saying that the property of the column is ReadOnly. I'm not sure how that's possible because I configured the dataset to update, etc.
I should probably mention that I'm not using a datagridview but a details view, if that makes a difference. I've seen some talk about how to do this using datagridview and don't know if that would work in my case.
What am I doing wrong? What should I use to update just the column I want in the current row?
Ugh, turns out I had forgot to set the databinding properties of the field in question to save directly to the binding source and also select the selecteditem to bind. It was right there and I didn't realize. Ya live and learn. :)

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.

Is there a way to allow only unique values in a column of an Infragistics UltraWinGrid?

I'm using an Infragistics UltraWinGrid with a column of drop-down boxes. I don't want the user to be able to select the same value for multiple rows in that column. Is there a simple (or heck, I'd settle for advanced) way to do this?
I'm using VB.NET
-EDIT-
I tried setting a filter on the data source for the drop-down box. But when I did that, the values in the other boxes in that column started disappearing (not the values themselves, but the descriptions that they were supposed to represent, so instead of reading "Information", it just said "1"). All of the cells in a column refer to the same combo box, so if you filter out a value from the data source for one of them, you filter it out for all of them.
I'm currently trying to catch a CellChange event and check against all other currently-used values. If it was already used, I would put up a message saying as much and revert back to the old value. However, the value comes back as the previously-saved one, so... not helpful. About to see if I can use the "text" property.
Since you're using Infragistics, you could use an UltraDropDown which is bound to a DataTable (or something similiar) which you can add a "Selected" column in addition to a column holding the values you want to show.
As each value is selected (via AfterCellUpdate or AfterCellListCloseUp for instance), you could update the "Selected" column in that data source and use a column filter to only show items which haven't been marked as selected. That way as items are selected or removed, the contents of the drop-down would be automatically updated.
To clear the selected flag from the old value, you can use the BeforeCellUpdate event to access the cell's current value then perform a lookup on the data source bound to the UltraDropDown using that value to clear the flag.
Solved it:
The trick was to use BeforeCellUpdate whose BeforeCellUpdateEventArgs has a "NewValue" and a "Cancel" member. I just look through all of the items in the column to see if any of them match the new value. If one does, I notify the user and cancel the operation.
And that's it. Here's the code:
Private Sub myUltraWinGrid_BeforeCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs) Handles myUltraWinGrid.BeforeCellUpdate
If e.Cell.Column.Key = "myColumn" Then
Dim newValue As Integer = CInt(e.NewValue)
For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In myUltraWinGrid.Rows
If row.Cells("myColumn") IsNot e.Cell _ 'So I'm not checking against the current cell
AndAlso CInt(row.Cells("myColumn").Value) = newValue Then
MsgBox("That value has already been used in this column")
e.Cancel = True
End If
Next
End If
End Sub