none
Access ListView SubItem Text by Column Header RRS feed

  • Question

  • Hi,

    I have a ListView that I would like to access SubItem text from the selected row.  The ListView is dynamically created by a sql query so I am not always sure what SubItem relates to the info I need.  When populating the listview I included column headers but i am not sure how I can reference a selected listview item subItem by column header.

    I can get the value with the following line, however i cant guarantee subitems[11] will be the data i need to get.

    listView1.Items[1].SubItems[11].Text

    If I cant do this direct by column header name, is there some way i can determine the SubItems index by the known column name?

     

    Thanks.

    Monday, March 5, 2007 3:12 PM

Answers

  •  Actually the code which is wrote by Galin Iliev should works if you have add the key value, after i had done some tests i discovered few things --- Let's Start from begining

    • ListView.Columns is a ColumnHeaderCollection which means a list of ColumnHeader and to access to items in this case we have to options
      • ListView.Columns[int index]      --- returns ColumnHeader
      • ListView.Columns[string Key]   --- returns ColumnHeader
        • Then the question is "What is the key?" The Key is the (ColumnHeader.Name)
        • So you going to tell me that i had add it manually and i had inserted in the Name Property (NameKey) and in the Text Property (ColumnName) and when i had tried to access it listView1.Columns["NameKey"].Index it's still give me the same error ---- because the Name property in the designer is the name of the variable and not the name of the ColumnHeader.Name
        • Lets Have an example thats work

    this.listView1.Columns.Clear();
    ColumnHeader colHdr = new ColumnHeader
    ();
    colHdr.Name =
    "KeyName"
    ;
    colHdr.Text =
    "TextName"
    ;
    this
    .listView1.Columns.Add(colHdr);
    int ix = this.listView1.Columns["KeyName"
    ].Index;
    this.Text = ix.ToString();

    There is no error  and ix = o;
    In Your Case add the Column for the listview inside the code

    foreach (DataColumn column in MyTable.Columns)
    {
        ColumnHeader header = new ColumnHeader
    ();
        header.Name = column.ColumnName;
        header.Text = column.Caption;
        listView1.Columns.Add(header);
    }

    Thursday, March 8, 2007 2:24 AM

All replies

  • Is this what you looking for:

     

    int colIndex = listView1.Columns["Column Name here"].Index;

    string colValue = listView1.Items[1].SubItems[colIndex].Text;

    Hope this helps

    Tuesday, March 6, 2007 7:45 AM
  • Thanks for you help, and i believe its something like this that im looking for, but i keep getting this error: NullReferenceException: Object reference not set to an instance of an object.

    I made a new listview, added a few columns manually but i still get this. Any ideas?

     

    EDIT:

    Forgot to say that it shows up the line:

    int colIndex = listView1.Columns["test"].Index;

     

    and that the column test actually exists

    Tuesday, March 6, 2007 4:57 PM
  •  Actually the code which is wrote by Galin Iliev should works if you have add the key value, after i had done some tests i discovered few things --- Let's Start from begining

    • ListView.Columns is a ColumnHeaderCollection which means a list of ColumnHeader and to access to items in this case we have to options
      • ListView.Columns[int index]      --- returns ColumnHeader
      • ListView.Columns[string Key]   --- returns ColumnHeader
        • Then the question is "What is the key?" The Key is the (ColumnHeader.Name)
        • So you going to tell me that i had add it manually and i had inserted in the Name Property (NameKey) and in the Text Property (ColumnName) and when i had tried to access it listView1.Columns["NameKey"].Index it's still give me the same error ---- because the Name property in the designer is the name of the variable and not the name of the ColumnHeader.Name
        • Lets Have an example thats work

    this.listView1.Columns.Clear();
    ColumnHeader colHdr = new ColumnHeader
    ();
    colHdr.Name =
    "KeyName"
    ;
    colHdr.Text =
    "TextName"
    ;
    this
    .listView1.Columns.Add(colHdr);
    int ix = this.listView1.Columns["KeyName"
    ].Index;
    this.Text = ix.ToString();

    There is no error  and ix = o;
    In Your Case add the Column for the listview inside the code

    foreach (DataColumn column in MyTable.Columns)
    {
        ColumnHeader header = new ColumnHeader
    ();
        header.Name = column.ColumnName;
        header.Text = column.Caption;
        listView1.Columns.Add(header);
    }

    Thursday, March 8, 2007 2:24 AM
  • Thank you so much. I finally got the code using this as the guide:

    first when creating the column header add the key like said above, and then when calling it in the subitems do below:

    foreach (ListViewItem item in lsvStampRolls.Items)
                {
                    MessageBox.Show(item.SubItems[lsvStampRolls.Columns["Stamp ID"].Index].Text);
                }


    Debra has a question


    Thursday, October 2, 2014 10:28 AM