locked
how to get value of selected item in listview RRS feed

  • Question

  • 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.

     

    Saturday, June 26, 2010 7:26 AM

Answers

  • 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"]);

    Saturday, June 26, 2010 9:50 AM

All replies

  • 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.


    Syed Mustehsan Ikram
    Saturday, June 26, 2010 8:58 AM
  • What is Company in

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

    Saturday, June 26, 2010 9:27 AM
  • 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"]);

    Saturday, June 26, 2010 9:50 AM
  • Amit am wondering how you got this to work. It isnt working here with me.It gives error "Object reference not set to an instance of an object" at the line

    CompID = Convert.ToInt16(CompRow["CompanyID"]);
    

    Friday, August 17, 2012 8:19 AM
  • First check if DataRowView CompRow has data or not, it shouldn't be null.

    Maybe, there is no value at the passed index value.


    Amit Govil | Email

    "Weeks of coding can save you hours of planning"

    Friday, August 17, 2012 8:45 AM