WPF:从所选行获取列名称 [英] WPF: Get column name from selected row

查看:175
本文介绍了WPF:从所选行获取列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



DataGrid的名称是 Datagrid_Newsale



当我被选中时,我正在收到整行提醒,所以我尝试映射其列。



如果行是 -

  {ID = 3,CustomerName = xyz,SaleDate = 05.08.2013 00:00: 00,TotalAmount = 10} 

然后是列 CustomerName = xyz 将显示在文本框中。



获取行 -

  var copyitem = Datagrid_NewSale.SelectedItem; 

if(copyitem == null)
{
MessageBox.Show(请从列表中选择值);
}
if(copyitem!= null)
{
MessageBox.Show(copyitem.ToString());
}

为了让customerName进入文本框,我尝试创建一个新的model- / p>

  public class CustomerDetailes 
{
public string CustomerName {get;组; }
}

来自客户表的数据库中的值 -

  public void viewcustomername()
{
列表< CustomerDetailes> ilist = null;
ilist =(从db.Customer
中的顺序选择新的CustomerDetailes
{
CustomerName = order.CustomerName
})ToList();
txtCustumer.Text = ilist.ToString();

}

所以再试一次 -

  CustomerDetailes copyitem =(CustomerDetailes)Datagrid_NewSale.SelectedItem; 

if(copyitem == null)
{
MessageBox.Show(请从列表中选择值);
}
if(copyitem!= null)
{
MessageBox.Show(copyitem.ToString());
}

txtCustomer.text = copyitem.CustomerName; // CustomerName into a textbox

但是在copyitem中引用null。



如何从整行中获取特定列。

解决方案

我想出了这个简单解决方案。



在我的情况下,匿名的映射数据类型为 copyitem 在这种情况下,使用动态数据类型解决了我的问题。



由于我的数据动态进入,以绘制特定的列,所以不可能静态地执行,因为那里没有数据。



使用动态数据类型 -

  dynamic copyitem = dataGrid1.SelectedItem; 

访问属性 -

  int localId = copyitem.ID; 

此外,对于customerName,TotalAmount我也是这样做的。



Linq查询更改

  var query =(从db.Customer 
其中order.ID = localId
选择顺序).ToList();

DataGrid_OpenSale.ItemsSource = query //返回数据到另一个datagrid。


I am trying to get the Row's particular column from selected item in wpf from DataGrid.

Name of DataGrid is Datagrid_Newsale.

I am getting alert of whole row when it is selected, So i tried mapping its column.

Say if row is-

{ ID = 3, CustomerName = xyz, SaleDate = 05.08.2013 00:00:00, TotalAmount = 10 }

Then it's column CustomerName=xyz is to be shown in textbox.

Getting row-

var copyitem = Datagrid_NewSale.SelectedItem;

if (copyitem == null)
{
    MessageBox.Show("Please select values from list");
}
if (copyitem != null)
{                       
    MessageBox.Show(copyitem.ToString());
}

For getting customerName into text box i tried creating a new instance of model-

public class CustomerDetailes
{
    public string CustomerName { get; set; }
}

And values from database from Customer Table-

public void viewcustomername()
{
    List<CustomerDetailes> ilist = null;
    ilist = (from order in db.Customer
                select new CustomerDetailes
                {
                    CustomerName= order.CustomerName
                }).ToList();
    txtCustumer.Text = ilist.ToString();

}

So giving it one more try-

CustomerDetailes copyitem = (CustomerDetailes)Datagrid_NewSale.SelectedItem;

if (copyitem == null)
{
    MessageBox.Show("Please select values from list");
}
if (copyitem != null)
{                       
    MessageBox.Show(copyitem.ToString());
}

txtCustomer.text=copyitem.CustomerName;  //CustomerName into a textbox

But it is referencing null in copyitem.

How can I get particular column from the whole row.

解决方案

I came up with this easy solution.

Mapped datatype of copyitem that was Anonymous in my case. In this case using Dynamic datatype solved my problem.

Due to my data was coming dynamically and then i was trying to map out particular column, so it was not really possible to do it statically because there is not data then.

Using Dynamic Datatype-

 dynamic copyitem = dataGrid1.SelectedItem;

Accessing property-

int localId = copyitem.ID;

furthermore for customerName,TotalAmount i did the same.

Linq Query changes-

var query= (from order in db.Customer
where order.ID=localId
select order).ToList();

DataGrid_OpenSale.ItemsSource=query // returning data to another datagrid.

这篇关于WPF:从所选行获取列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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