如何使用C#在WPF中通过datarowview从数据网格获取数据 [英] How to get data from a datagrid by a datarowview in WPF with C#

查看:270
本文介绍了如何使用C#在WPF中通过datarowview从数据网格获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到Generic list的DataGrid。我想通过datarowView从datagrid获取值但是显示错误无效的强制转换异常被捕获



我尝试过:



我的代码是



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

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)
         {

         }
     }

推荐答案

public void filldatagrid()

{

long idclient = 0;

MRinfoHelper _MRinfoHelper =新的MRinfoHelper();

MRlistLogic objlist = new MRlistLogic();

List< MRInfoModel> lstMRInfoModel = new List< MRInfoModel>();

lstMRInfoModel = JsonConvert.DeserializeObject< List< MRInfoModel>>(_ MRinfoHelper.MRList(true,idclient));

dgShowData .ItemsSource = lstMRInfoModel;





}





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;


}


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
















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


这篇关于如何使用C#在WPF中通过datarowview从数据网格获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆