Bogdan Marius
Bogdan Marius

Reputation: 45

Get Text from Datagrid cell or Row

Hello i am using the data grid to show an observable collection in WPF. Now how can i get the selected row text from the DataGrid so i can call a function. Here is my XAML:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="0,77,0,0" VerticalAlignment="Top" Height="720" Width="664" ItemsSource="{Binding Items}" AutoGenerateColumns="False" Grid.ColumnSpan="2" SelectedCellsChanged="dataGrid_SelectedCellsChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ProjectName" MinWidth="100" Binding="{Binding Path=ProjectName,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Header="Name" MinWidth="100" Binding="{Binding Path=Name,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Header="Path" MinWidth="460" Binding="{Binding Path=Path,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

How can i get the text from the selected cell or row?

Upvotes: 2

Views: 7590

Answers (3)

mm8
mm8

Reputation: 169200

If the Items collection you bind to is an IEnumerable<T>, you could cast the SelectedItem property of the DataGrid to T:

YourItemType obj = dataGrid.SelectedItem as YourItemType;
string name = obj.Name;

Or you could bind the SelectedItem property to a property of type T, just as you bind the ItemsSource property:

<DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ...>

Upvotes: 1

boop_the_snoot
boop_the_snoot

Reputation: 3247

As WPF DataGrid is more flexible than a WinForms DataGridView, gettings values seems difficult. So I made the below static extension methods/funcitons to get SelectedRow, SelectedCell & SelectedCellValue. Credits to some post I saw a while back in SO for GetVisualChild and some of the functions given below.

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
     T child = default(T);
     int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
     for (int i = 0; i < numVisuals; i++)
     {
         Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
         child = v as T;
         if (child == null)
         {
            child = GetVisualChild<T>(v);
         }
         if (child != null)
         {
            break;
         }
    }
        return child;
}

To get SelectedRow:

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

Gets the DataGridRow when you know the RowIndex:

public static DataGridRow GetRow(this DataGrid grid, int index)
{
     DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
     if (row == null)
     {
        grid.UpdateLayout();
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
     }
     return row;
}

Gets the DataGridCell when you have a DataGridRow and a columnIndex:

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int columnIndex)
{
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[columnIndex]);
            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        }

        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        return cell;
    }
    return null;
}

Gets the DataGridCell when you have a DataGridRow and a columnName:

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, string columnName)
{
    int index = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
    return GetCell(grid, row, index);
}

Gets the DataGridCell when you have a rowIndex and a columnIndex:

public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
     DataGridRow rowContainer = grid.GetRow(row);
     return grid.GetCell(rowContainer, column);
}

Gets the DataGridCellValue when you have a DataGridRow and a ColumnName:

public static string GetCellValue(this DataGrid grid, DataGridRow row, string columnName)
{
    int index = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
    DataGridCell dgc = GetCell(grid, row, index);
    string str = Convert.ToString(((TextBlock)dgc.Content).Text);
    return str;
}

Gets the DataGridCellValue when you have a rowIndex and a ColumnName:

public static string GetCellValue(this DataGrid grid, int rowIndex, string columnName)
{
     DataGridRow row = grid.GetRow(rowIndex);
     int columnIndex = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;

     DataGridCell dgc = GetCell(grid, row, columnIndex);
     string str = Convert.ToString((TextBlock)dgc.Content);
     return str;
}

Upvotes: 11

Nick
Nick

Reputation: 5042

You should use the default CollectionView for your data source. Assumed it is called Items, you can use:

var cv = (CollectionView)CollectionViewSource.GetDefaultView(Items);
var currentItem = (YourItemType)cv.CurrentItem;

Upvotes: 0

Related Questions