pass data from the datagrid of one form to the textbox of another form wpf

0

I have the following code to load my data into a datagrid of a form

Private Sub cargarGrid()
    Dim conexion As New ConexionBD
    Dim data As DataTable
    dataGridVehiculos.ItemsSource = Nothing
    Try
        data = conexion.mostrarDatosenGrid("VEHICULOS_EN_TRANSITO")
        dataGridVehiculos.ItemsSource = data.DefaultView
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Public Function mostrarDatosenGrid(ByVal tabla As String) As DataTable
        Dim nuevaTabla As New DataTable
        Try
            conexion()
            _adaptador.SelectCommand = New SqlCommand("SELECT * FROM " & tabla, _conexion)
            _conexion.Open()
            _adaptador.SelectCommand.Connection = _conexion
            _adaptador.Fill(nuevaTabla)
            Return nuevaTabla
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        Finally
            _conexion.Close()
        End Try
    End Function

It works for me to load the data in the data grid, what did not work for me is that when selecting an item from the data grid it does not load me in the texbox of another form I have the following code.

Private Sub dataGridVehiculos_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles dataGridVehiculos.SelectionChanged
    Dim tabla As DataRowView = DirectCast(dataGridVehiculos.SelectedItem, DataRowView)
    registro.noTiquete = tabla.Item(1)
    registro.fechaPesoVacio = tabla.Item(2)
    registro.fechaPesoLleno = tabla.Item(3)
    registro.placa = tabla(4)
    registro.conductor.cedula = tabla.Item(5)
    registro.conductor.nombre = tabla.Item(6)
    registro.clienteProveedor.nombre = tabla.Item(7)
    registro.productoMateriaPrima.nombre = tabla.Item(8)
    registro.tara = tabla.Item(9)
    registro.bruto = tabla.Item(10)
    registro.neto = tabla.Item(11)
    registro.noOrden = tabla.Item(12)
    registro.origen.nombre = tabla.Item(13)
    registro.destino.nombre = tabla.Item(14)
    registro.observaciones = tabla.Item(15)
    registro.taraAutorizado = tabla.Item(16)
    registro.brutoAturorizado = tabla.Item(17)
    registro.usuarioTransito = tabla.Item(18)
    registro.usuarioProcesado = tabla.Item(19)
    registro.rutaFotos = tabla.Item(20)
    registro.horaPesoVacio = tabla.Item(21)
    registro.horaPesoVacio = tabla.Item(22)
    registro.noShipment = tabla.Item(23)
    registro.noSello = tabla.Item(24)
    registro.noR = tabla.Item(25)
    registro.noContenedor = tabla.Item(26)
    registro.planta.nombre = tabla.Item(28)
    registro.transportadora.nombre = tabla.Item(29)
    registro.pesoOrden = tabla.Item(30)
End Sub

I have not been able to know what I'm doing wrong, or if I have to do it in another way, the registration class is a class that contains the necessary fields to store the grid data and from there I can take that information to the texbox, the point is that the data is not moving from the grid to the fields of the class, could someone give me a hand to know exactly how to do it? this the xaml code.

<DataGrid Margin="-351.935,-15.119,-355.73,-61.323" ItemsSource="{Binding Items3}" CanUserSortColumns="True" CanUserAddRows="False" AutoGenerateColumns="False"  md:DataGridAssist.CellPadding="13 8 8 8" md:DataGridAssist.ColumnHeaderPadding="8"></DataGrid>

Thanks for the help.

    
asked by Benkos 08.02.2017 в 16:00
source

1 answer

0

A starting observation:

The DataGrid has the ItemSource defined by Binding but you are assigning it to the code as well, since your code is not defined as MVVM, you do not need the Binding.

The other thing that is about your question, use the FullRow selection mode and when you select something you get the SelectedItem and from that object it takes the ItemArray which is the object that contains all the columns of the row and that you could go through or use the index of the cell that you selected to obtain that specific data:

DataRowView view = (DataRowView)myDataGrid.SelectedItem;
int index = dg_detail.CurrentCell.Column.DisplayIndex;
string cellvalue = view.Row.ItemArray[index].ToString();

Sorry to write it in C #, I do not handle well Vb.

    
answered by 09.02.2017 в 03:36