简体   繁体   English

如何以编程方式获取WPF gridview行值?

[英]How to get WPF gridview row values programmatically?

We have lots of ListViews with a GridView view that are bound to a ViewModelCollection<T> . 我们有很多ListViewsGridView视图绑定到ViewModelCollection<T> Now I need to get the displayed values (as string) from the GridView . 现在,我需要从GridView获取显示的值 (作为字符串)。 It seems that I cannot access the rows from GridView class. 看来我无法从GridView类访问行。

I don't know what type the GridView is showing, so I cannot use ListView.ItemCollection either. 我不知道GridView显示什么类型,所以我也不能使用ListView.ItemCollection Also I don't know the name of the GridView because the code isn't in the code behind (trying to follow MVVM pattern). 另外我也不知道GridView的名称,因为该代码不在后面的代码中(试图遵循MVVM模式)。 I just need the values that are being displayed, so that I can export them to Excel sheet (the column order is also important). 我只需要显示的值,以便可以将它们导出到Excel工作表(列顺序也很重要)。

Any suggestions how to approach this? 任何建议如何解决这个问题?

I would use the ViewModelCollection to get the data. 我将使用ViewModelCollection来获取数据。 If you lose formatting, just apply same formatting rules on the fields. 如果丢失格式,只需在字段上应用相同的格式规则。 Better, I would expose string properties in the ViewModelCollection with respective formatting, and bind the grid view to them. 更好的是,我将使用各自的格式公开ViewModelCollection中的字符串属性,并将网格视图绑定到它们。

By using the DataRowView you can get the each and every cell value. 通过使用DataRowView,您可以获取每个单元格值。

DataRowView row = (DataRowView)grid_view.SelectedItems[0];
cmbx_name.Text = row.Row.ItemArray[0].ToString();
lbl_date.Text = row.Row.ItemArray[1].ToString();
txt_location.Text = row.Row.ItemArray[2].ToString();
txt_vehicleno.Text = row.Row.ItemArray[5].ToString();
cmbx_source.Text = row.Row.ItemArray[6].ToString();
orderid = row.Row.ItemArray[7].ToString();
custid = row.Row.ItemArray[8].ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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