0

I'm using this List:

List<AssetListData> assetList = new List<AssetListData>();

Filled with data, and bind it to my RadGridView using this code:

AssetList_GridView.ItemsSource = assetList;

And now I have a GridView with two columns (Name and Type). I created a ContextMenu in AssetList_GridView consists of Edit and Delete. I need to get the value after I click the ContextMenu but it failed. I tried this code inside the click event in ContextMenu :

private void GridContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
        MenuItem item = (e.OriginalSource as RadMenuItem).DataContext as MenuItem;
        switch (item.Text)
        {
            case "Edit Asset":
                var typeValue = ((assetListData)AssetList_GridView.SelectedItem).assetType;
                this.AssetList_GridView.BeginEdit();
                break;
            case "Delete Asset":
                this.AssetList_GridView.Items.Remove(this.AssetList_GridView.SelectedItem);
                break;
        }
}

The error from var typeValue = ((assetListData)AssetList_GridView.SelectedItem).assetType; said that:

assetList could not be found.

Why can't I access assetList here, but I can access it for GridView ItemsSource? Is there any simple way to get value from the clicked row?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Mirza
  • 199
  • 4
  • 17

1 Answers1

2

SelectedItem cant be retyped to LIST, if you want to retype it you should use

var typeValue = ((AssetListData)AssetList_GridView.SelectedItem).assetType;
Slasko
  • 407
  • 2
  • 8
  • the error is still the same, i'll edit my question to make it more detail – Mirza Nov 19 '15 at 07:01
  • ah nvm i need to write all the directory of the AssetListData eventho i made it before. thanks – Mirza Nov 19 '15 at 07:07
  • Sir, what if i want to get both assetName and assetType? and what if i have more and want to get all the value in that Row? – Mirza Nov 19 '15 at 08:16
  • 1
    you can do var row = (AssetListData)AssetList_GridView.SelectedItem and you get whole ROW but not sure if that will work. Otherwise look http://stackoverflow.com/questions/5121186/datagrid-get-selected-rows-column-values – Slasko Nov 19 '15 at 08:24