开发者

WPF Toolkit DataGrid SelectionChanged Getting Cell Value

Please help me, Im trying to get the value of Cell[0] from the selected row in a SelectionChangedEvent.

I am only managing to get lots of different Microsoft.Windows.Controls and am hoping im missing something daft.

Hoping I can get some help from here...

    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Microsoft.Windows.Controls.DataGrid _DataGrid = sender as Microsoft.Windows.Co开发者_Python百科ntrols.DataGrid;
    }

I was hoping it would be something like...

_DataGrid.SelectedCells[0].Value;

However .Value isn't an option....

Many many thanks this has been driving me mad! Dan


Less code, and it works.

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 is the index of the column you want to know.


pls, check if code below would work for you:

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

hope this helps, regards


This will give you the current selected row in the DataGrid in WPF:-

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

Now to get the cell value just write dtr[0], dtr["ID"], etc.


Since you are using the "SelectionChanged", you can use the sender as a Data Grid:

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

I tried the answers posted here and were good, but gave me problems when started to hide columns in the DataGrid. This one works for me even when hiding columns. Hope it works for you too.


private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid _DataGrid = sender as DataGrid;

    string strEID = _DataGrid.SelectedCells[0].Item.ToString(); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜