简体   繁体   中英

how to get selected item value of a DataGridHyperlinkColumn in wpf datagrid

I am using a normal data grid column like

I am getting the value using
var item = datagrid1.SelectedItem; var a= datagrid1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text

But instead Of normal datagrid text column if I am using DataGridTextColumn it is giving the value as ""; why? and How can i get the value;

What are you populating your datagrid with? I usually use this method I created that gets the value from the data context and not the grid itself.

private Tuple<string,int,string,int> SelectedCell(System.Windows.Controls.DataGrid dataGrid, int textIndex, int valueIndex)
    {            
        string cellText = null, cellValue = null;

        //if the text index is out of bounds, fix it
        if (dataGrid.SelectedCells.Count < textIndex)
        {
            textIndex = dataGrid.SelectedCells.Count - 1;
        }
        else if (textIndex < 0)
        {
            textIndex = 0;
        }

        //if the value index is out of bounds, fix it
        if (dataGrid.SelectedCells.Count < valueIndex)
        {
            valueIndex = dataGrid.SelectedCells.Count - 1;
        }
        else if (valueIndex < 0)
        {
            valueIndex = 0;
        }

        System.Data.DataRowView row = dataGrid.SelectedItem as System.Data.DataRowView;

        if(row != null)
        {
            cellText = row[textIndex].ToString();
            cellValue = row[valueIndex].ToString();
        }

        return new Tuple<string,int,string,int>(cellText,textIndex, cellValue, valueIndex);
    }

It gets the values of 2 indexes (column indexes) and then returns those values as well as the index of those values in case the original indexes were out of bounds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM