Click here to Skip to main content
15,909,827 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a DataGrid which is bind to a Generic list.I want to get values from datagrid by a datarowView but Show an error invalid cast exception was caught

What I have tried:

My Code is

C#
private void dgSearch_SelectionChanged_1(object sender,SelectionChangedEventArgs e)
     {
         try
         {
            DataRowView drv=(DataRowView) dgSearch.SelectedItem;
            txtMRName.Text = drv["Id"].ToString();
            txtEditMrSe.Text = drv["Name"].ToString();
            dgSearch.Visibility = Visibility.Collapsed;
         }
         catch (Exception ex)
         {

         }
     }
Posted
Updated 20-Oct-16 0:19am
Comments
Richard Deeming 18-Oct-16 9:29am    
So debug your code and find out what type the SelectedItem actually is.

Also, don't silently swallow all exceptions. That makes it much harder to diagnose any problems. Only catch the exceptions you can actually deal with.

1 solution

public void filldatagrid()
{
long idclient =0;
MRinfoHelper _MRinfoHelper = new MRinfoHelper();
MRlistLogic objlist = new MRlistLogic();
List<MRInfoModel> lstMRInfoModel = new List<MRInfoModel>();
lstMRInfoModel =JsonConvert.DeserializeObject<List<MRInfoModel>>(_MRinfoHelper.MRList(true, idclient));
dgShowData.ItemsSource = lstMRInfoModel;


}


XML
<DataGrid HorizontalAlignment="Left" Name="dgSearch" Cursor="Hand" HeadersVisibility="None" Visibility="Collapsed" RowHeight="25" CanUserAddRows="False" AutoGenerateColumns="False" HorizontalGridLinesBrush="#FF808080" VerticalGridLinesBrush="#FF808080" SelectionChanged="dgSearch_SelectionChanged_1" Margin="55,205,0,0" VerticalAlignment="Top" Width="222" Height="Auto">
           <DataGrid.Columns>
               <DataGridTextColumn Header="ID" Binding="{Binding Path=Id}" Visibility="Collapsed" ></DataGridTextColumn>
           <DataGridTextColumn Header="Term" Width="*" Binding="{Binding Path=Name}"></DataGridTextColumn>
           </DataGrid.Columns>





C#
private void dgSearch_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        try
        {
            var myListView = sender as DataGrid;
            if (myListView != null)
            {
                var selectedItem = myListView.SelectedItem.ToString();
                Type t = dgSearch.SelectedItem.GetType();
                System.Reflection.PropertyInfo[] props = t.GetProperties();
                string propertyValue = props[1].GetValue(dgSearch.SelectedItem, null).ToString();
              // txtMRName.Text = propertyValue;
                getDataByIdFun(Convert.ToInt64(propertyValue));
            }
        }
        catch (Exception ex) { }


    }

    public void getDataByIdFun(long id)
    {
        MRlistLogic objlist = new MRlistLogic();
        MRInfoModel _mrInfoModel = new MRInfoModel();
        _mrInfoModel = objlist.GetMRByID(id);
        txtMRName.Text = _mrInfoModel.Mrname;
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900