WPF DataGrid Gets the data for the selected row

Source: Internet
Author: User
Tags wpf datagrid

In WPF, click the DataGrid, how do I get the row currently clicked?

For example, in the MouseDoubleClick event, in fact the selected row is a DataRowView, you can use the following methods to get the data of the selected row, you need to refer to system. IO and System.Data;

var a =this.exdatagrid.selectitem;

var b= a as DataRowView;

or Var b= (DataRowView) Exdatagrid.selectitem

b["Filedname"]. ToString (); Where the data you can convert directly

Note, however, that if the ItemsSource of your DataGrid is a collection of objects rather than a DataTable, then using Datagrid.selecteditem as Youclass can be obtained. For example, in LINQ to SQL your Itemsource is a collection of objects, which is treated as a class object, except that you should convert the type of the table (the product table corresponds to the product class in DBML)

var myselectedelement = (Product) Exdatagrid.selecteditem;

int Selectedid=myselectedelement.productid;

WPF DataGrid Gets the data for the selected row

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.