Home » .Net FrameworkRSS

how to get value of selected item in listview

I have a listview which has three columns which i added like below:

 

 <ListView.View>
        <GridView>
          <GridViewColumn Width="360" Header="Company" DisplayMemberBinding="{Binding Path=CompanyName}"/>
          <GridViewColumn Width="100" Header="Company Code" DisplayMemberBinding="{Binding Path=CompanyCode}"/>
          <GridViewColumn Width="0" Header="Company ID" DisplayMemberBinding="{Binding Path=CompanyID}"/>
        </GridView>
 </ListView.View>

After binding the lisview,it has multiple rows.Now i want the value of 3rd column i.e. from CompanyID after when user selects a row in listview.

It seems to be simple but i am not able to do this.

Please help me.

Thanks in advance.

 

 

3 Answers Found

 

Answer 1

You can get the ID like this.

Company company = urListView.SelectedItem[0] as Company;

if(company!=null)

ID = company.CompanyID;

Note: I am thinking that you are giving list of company to Listview as itemssource.

 

Answer 2

What is Company in

Company company = urListView.SelectedItem[0] as Company;

 

Answer 3

I got it solved:

DataRowView CompRow;
int SComp;
long CompID;

SComp = listCompany.SelectedIndex;
CompRow = listCompany.Items.GetItemAt(SComp) as DataRowView;
CompID = Convert.ToInt16(CompRow["CompanyID"]);

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter