Get the value of a cell from a DataGrid wpf

0

How can I get the value of a cell from a DataGrid (WPF)? I have this:

     DataRowView item = (DGContactos as DataGrid).SelectedItem as DataRowView;  
    _contacto.Nombre = item.Row.ItemArray[0].ToString();  
    _contacto.NumeroCelular = item.Row.ItemArray[1].ToString();  
    _contacto.NumeroTrabajo = item.Row.ItemArray[2].ToString();  
    _contacto.NumeroParticular = item.Row.ItemArray[3].ToString();  
    _contacto.Email = item.Row.ItemArray[4].ToString();  
    _contacto.Notas = item.Row.ItemArray[5].ToString();

That works well and gives me all the values.

Apart I want to know the specific value of the cell that receives the focus example: string valueCeld = DGContacts ....

I have tried several ways and I have several days searching the net and I only find for DataDridView.

    
asked by wjvelasquez 28.12.2016 в 12:06
source

3 answers

0

Try this:

DataRowView view = (DataRowView)my_datagrid.SelectedItem;
int index = my_datagrid.CurrentCell.Column.DisplayIndex;
string cellvalue = view.Row.ItemArray[index].ToString();
    
answered by 04.01.2017 / 23:31
source
0

For that you have to capture the DataGrid selection event and set the selection mode to be only per cell.

Here the grid in XALM:

<DataGrid x:Name="dg" SelectedCellsChanged="dg_SelectedCellsChanged" SelectionUnit="Cell">

And here the event:

 private void dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        IList<DataGridCellInfo> celdas = e.AddedCells;
        string valor = celdas[0].Item as string;            
    }
    
answered by 29.12.2016 в 14:25
0

I would recommend that you not directly capture the value of the cell unless you have a very special reason for it, but always work with the collection behind the DataGrid.

For example, if we have:

public class Coches {
    string marca;
    string modelo;
}

Coche coche = new Coche();
ObservableCollection<Coches> listaCoches = new ObservableCollection<Coche>();

And in your DataGrid you have:

<DataGrid ItemSource="{Binding listaCoches}" SelectedItem="{Binding coche}"/>

What I would do is look at the car. brand or car. model it, instead of looking at what is in the cell corresponding to the model or brand column.

It is usually better to look at the collection behind the DataGrid, than the cells themselves. However, if you want to see the cell itself, you can always look at the CellEditEnding event, where you have an object called "e.EditingElement" (e.EditingElement.DataContext if you want the whole row) that has everything you need, but I insist that I recommend the other approach much more.

Finally, and maybe it's what you're most interested in, there's the CurrentCellChanged event, from which you can capture what you're looking for with:

DataGrid miDataGrid = sender as DataGrid;
miDataGrid.CurrentColumn.DisplayIndex

And knowing the column in which you are positioned, you can go to the list of Cars and look at the value of that property.

    
answered by 29.12.2016 в 21:29