VB.net how to return cell data from column and row of datagrid - wpf

I have a WPF datagrid currently populated with data.
I would like to return the cell value of the cell which the user click on, when the datagrid is in full row select mode.
At the moment I am able to return the row and column of the cell which the user select but I am not sure how to return the actual cell value.
Here is my VB code.
Private Sub WaterfallDataGrid_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs) Handles WaterfallDataGrid.SelectedCellsChanged
'return the column of the cell of the datagrid
textBox1.Text = WaterfallDataGrid.CurrentCell.Column.DisplayIndex
'return the row of the cell of the datagrid
textBox2.Text = WaterfallDataGrid.Items.IndexOf(WaterfallDataGrid.CurrentItem)
'how do i return the value of the cell itself?
'textBox3.Text = ??
End Sub

When the SelectedCellsChangedEvent is fired, you get the SelectedCellsChangedEventArgs to work with. This object has a property called AddedCells. When the user clicks the cell, the whole row is selected, so the whole row is contained in AddedCells. You already know the column number from your code, so this should work:
Private Sub WaterfallDataGrid_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs) Handles WaterfallDataGrid.SelectedCellsChanged
textBox3.Text = e.AddedCells(WaterfallDataGrid.CurrentCell.Column.DisplayIndex).Item(WaterfallDataGrid.CurrentCell.Column.DisplayIndex)
End Sub
Source:
https://msdn.microsoft.com/en-us/library/system.windows.controls.selectedcellschangedeventargs(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.items(v=vs.110).aspx
Edit:
The event will only fire when a cell in a new row is selected. If you select a cell in the same row, nothing will happen. To always get the current cell, you should register the CurrentCellChanged event in your DataGrid.
Add the CurrentCellChanged event to your xaml:
<DataGrid x:Name="WaterfallDataGrid" CurrentCellChanged="WaterfallDataGrid_CurrentCellChanged" />
And this to your codebehind:
Private Sub WaterfallDataGrid_CurrentCellChanged(sender As Object, e As EventArgs)
textBox3.Text = WaterfallDataGrid.CurrentCell.Item(WaterfallDataGrid.CurrentCell.Column.DisplayIndex)
End Sub

Related

Display selected item from datagridcombobox in the next cell which is datagridtextcolumn using vb.net

I have created a datagrid, in which there are two columns. One is datagridCombobox and another datagridtextbox column. Datagridcombobox consists of list of items. When I select an item, I want it to be displayed in the next cell ie., datagridtextbox column.
Create a cell value changed event for the grid, in that you can get the value of combobox column value and then assign to 2nd column. hope it helps.
Private Sub DGV_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellValueChanged
Dim ComboText As String = DGV.CurrentRow.Cells(1).Value
DGV.CurrentRow.Cells(2).Value = ComboText
End Sub

Deleting from an ObservableCollection from a DataGrid

I'm making a WPF application, and currently I'm having trouble deleting items from an ObservableCollection using the selected items in a DataGrid. The DataGrid uses the ObservableCollection as its ItemsSource. I made something that seemed to work until I started sorting the columns:
Private Sub confirmDelete()
Dim userAnswer As MessageBoxResult
userAnswer = MessageBox.Show("Are you sure?", "Delete Selected Items?", MessageBoxButton.YesNo)
If userAnswer = MessageBoxResult.Yes Then
Dim index As Integer
For i As Integer = grdReadings.SelectedItems.Count - 1 To 0 Step -1 'step backwards through list
index = grdReadings.Items.IndexOf(grdReadings.SelectedItems(i))
testDataList.RemoveAt(index)
Next
End If
End Sub
So this works fine if I never sort by any of the columns in the DataGrid, but as soon as I sort the DataGrid in a different order than the items were added, the index of selected items in the DataGrid doesn't match those item's indices in the ObservableCollection. Is there a better/easier way to delete items from an ObservableCollection using the items a user selects in the DataGrid?
Instead of looking up the index, you can directly delete the items
Private Sub confirmDelete()
Dim userAnswer As MessageBoxResult
userAnswer = MessageBox.Show("Are you sure?", "Delete Selected Items?", MessageBoxButton.YesNo)
If userAnswer = MessageBoxResult.Yes Then
For each item in grdReadings.SelectedItems
testDataList.Remove(item)
Next
End If
End Sub
Here's what I did:
Dim itemsToRemove As New List(Of CCReading)
For Each item In grdReadings.SelectedItems
itemsToRemove.Add(item)
Next
For Each item In itemsToRemove
testDataList.Remove(item)
Next
itemsToRemove.Clear()

how to get the Hidden Columns Control Value in PreparingCellForEdit Silverlight Datagrid

how to get the Hidden Columns Control Value in PreparingCellForEdit Silverlight Datagrid
Code as Follows:
Private Sub TaskDataGrid_LoadingRow(ByVal sender As System.Object, ByVal e As
System.Windows.Controls.DataGridRowEventArgs)
Dim row As DataGridRow = e.Row
Dim cellContent As FrameworkElement = TaskDataGrid.Columns(8).GetCellContent(e.Row)
Dim cboLabValidated As ComboBox = CType(cellContent.FindName("cboLabValidated"), ComboBox)
Dim ViewModel As New NonFirmWareNewRequestViewModel()
If cboLabValidated IsNot Nothing Then
cboLabValidated.ItemsSource = ViewModel.YesNoValues
End If
TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub
in the above code I am hidden the column 1 in LoadingRow Event and in need to get the value of that Column in PreparingCellForEdit
Code for PreparingCellForEdit as Follows:
Dim fe As FrameworkElement = TaskDataGrid.Columns(5).GetCellContent(e.Row)
Dim fe1 As FrameworkElement = TaskDataGrid.Columns(1).GetCellContent(e.Row)
Dim gridCmbo As Grid = DirectCast(fe, Grid)
Dim gridCmbo1 As Grid = DirectCast(fe1, Grid)
Dim lbltaskId As Label = CType(gridCmbo1.FindName("lbltaskId"), Label)
Dim cboCompVerSel As ComboBox = CType(gridCmbo.FindName("cboCompVerSel"), ComboBox)
Dim lblCompVer As Label = CType(gridCmbo.FindName("lblCompVer"), Label)
I am using label control to show column 1 and I am identifying the label control object but the content becomes empty..
this is working fine
1) While in the Loading Row Event We should not hide the Column, if you hide the column you cannot get the value from the column let the data Load first in the LoadingRow Event.
2) Hide the column in the Selection changed event code as follows :
Private Sub TaskDataGrid_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles TaskDataGrid.SelectionChanged
TaskDataGrid.Columns(1).Visibility = Visibility.Collapsed
End Sub
this event will fire after LoadingRow Datagrid event or PreparingCellForEdit event once the Row is loaded we can hide the column and can get the value where ever you want.If you Hide in LoadingRow event without loading the Data you cannot get the value of that Control in the Datagrid template or Data Column.

SelectedIndexChanged event of dropdownlist in Listview not happening. in a usercontrol

I have a ListView and DropDownList(primary) and tow more and a text box, inside every Row of that ListView. All on a user control ,and using the this user control on the other usercontol(here the operations to be performed.)(we are using DNN) .
Now I would like to add event handler for dropdownlist which would react on SelectedIndexChanged. I wanted to show and hide the other dropdowns and textbox depending on a specific value of the DropDrownList(primary) selected.
I am nowhere to do the same..despite the several attempts.
Screening Outcome:&nbsp
Comments:
Subset Used:&nbsp
TAR Status:&nbsp
code behind like this
Public Sub ddlFindingsScreeningOutcome_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
Dim txtFindingsComments As TextBox = CType(item.FindControl("txtFindingsComments"), TextBox)
Dim ddlFindingsTarStatus As DropDownList = CType(item.FindControl("ddlFindingsTarStatus"), DropDownList)
If Not ddl Is Nothing Then
If Not ddl.SelectedItem.Text = "Not Screened" Then
txtFindingsComments.Visible = True
ddlFindingsTarStatus.Visible = True
End If
Else
txtFindingsComments.Visible = False
ddlFindingsTarStatus.Visible = False
End If
End Sub

How to fire an event when making a selection of an editable combobox?

I have a ComboBox that is databound to an ObservableCollection of strings. The ComboBox is also editable, so you can either enter in your own value or select one from the list. The issue I'm running into is the index of SelectedItem seems to be the index of the last item you selected when you've entered in your own value in the ComboBox, though it's -1 when you have IsTextSearchEnabled set to true.
The problem is, if someone entered their own value and then decide to instead select the item on the ComboBox that had been selected before, the index doesn't change so the SelectionChange event doesn't fire. How could I get an event to fire in this situation?
Test this...
I hope this helps:
Dim oldSEL As String = ""
'always checking while you move your mouse over the combobox (when altering selection) and using the keyboard to (alter selection)
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.MouseMove, ComboBox1.KeyPress
Dim currentSEL As String = ComboBox1.SelectedText
If Not (oldSEL = "" And currentSEL = oldSEL) Then
fire()
oldSEL = currentSEL
End If
End Sub
Private Sub fire()
Trace.Write("text selected changed")
End Sub
You should change all the Combobox1 to your liking.

Resources