Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...