简体   繁体   中英

How to get WPF gridview row values programmatically?

We have lots of ListViews with a GridView view that are bound to a ViewModelCollection<T> . Now I need to get the displayed values (as string) from the GridView . It seems that I cannot access the rows from GridView class.

I don't know what type the GridView is showing, so I cannot use ListView.ItemCollection either. Also I don't know the name of the GridView because the code isn't in the code behind (trying to follow MVVM pattern). I just need the values that are being displayed, so that I can export them to Excel sheet (the column order is also important).

Any suggestions how to approach this?

I would use the ViewModelCollection to get the data. 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.

By using the DataRowView you can get the each and every cell value.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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