3

私を助けてください、ImはSelectionChangedEventで選択された行からCell[0]の値を取得しようとしています。

私はたくさんの異なるMicrosoft.Windows.Controlsを手に入れることができているだけで、何かが足りないことを望んでいます。

ここから助けが得られることを願っています...

    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Controls.DataGrid;
    }

私はそれが次のようなものになることを望んでいました...

_DataGrid.SelectedCells[0].Value;

ただし、.Valueはオプションではありません...。

これが私を怒らせてくれたことに感謝します!ダン

4

5 に答える 5

16

コードが少なくて済み、機能します。

private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dataGrid = sender as DataGrid;
        DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
        DataGridCell RowColumn = dataGrid.Columns[ColumnIndex].GetCellContent(row).Parent as DataGridCell;
        string CellValue = ((TextBlock)RowColumn.Content).Text;
    }

ColumnIndexは、知りたい列のインデックスです。

于 2013-09-06T13:41:21.610 に答える
9

pls、以下のコードがあなたのために働くかどうかを確認してください:

private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid dataGrid = sender as DataGrid;
    if (e.AddedItems!=null && e.AddedItems.Count>0)
    {
        // find row for the first selected item
        DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
            // find grid cell object for the cell with index 0
            DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
            if (cell != null)
            {
                Console.WriteLine(((TextBlock)cell.Content).Text);
            }
        }
    }
}

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

これがお役に立てば幸いです

于 2010-01-28T01:12:53.270 に答える
3

これにより、WPFのDataGridで現在選択されている行が表示されます。-

DataRow dtr = ((System.Data.DataRowView)(DataGrid1.SelectedValue)).Row;

セルの値を取得するには、、などを記述しdtr[0]ますdtr["ID"]

于 2013-07-21T10:27:12.187 に答える
3

「SelectionChanged」を使用しているため、送信者をデータグリッドとして使用できます。

DataGrid dataGrid = sender as DataGrid;
DataRowView rowView = dataGrid.SelectedItem as DataRowView;
string myCellValue = rowView.Row[0].ToString(); /* 1st Column on selected Row */

ここに投稿された回答を試してみましたが、DataGridで列を非表示にし始めたときに問題が発生しました。これは、列を非表示にしている場合でも機能します。それがあなたにもうまくいくことを願っています。

于 2014-04-12T20:18:23.157 に答える
1
private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid _DataGrid = sender as DataGrid;

    string strEID = _DataGrid.SelectedCells[0].Item.ToString(); 
}
于 2011-07-25T11:33:29.157 に答える