Selecting a value from a column in a list view

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • grahamb123
    New Member
    • Sep 2010
    • 2

    #1

    Selecting a value from a column in a list view

    I use the following method to display some items from a database into a list view:
    Code:
            private void  search (string query)
            {
                MySqlCommand command = connection.CreateCommand();
                MySqlDataReader Reader;
                command.CommandText = query;
                connection.Open();
                Reader = command.ExecuteReader();
                string results = null;
    
                while (Reader.Read())
                {
    
                    for (int i = 0; i < Reader.FieldCount; i++)
                    {
                        results = (Reader.GetValue(i).ToString());
                        
                        Debug.WriteLine(results + "" + i);
                        if (i == 0)
                        {
                            item_object = results_view.Items.Add(results);
                        }
                        else if (i == 1)
                        {
                            item_object.SubItems.Add(results);
                        }
                        else if (i == 2)
                        {
                            item_object.SubItems.Add(results);
                        }
                        else if (i == 3)
                        {
                            item_object.SubItems.Add(results);
                        }
    
                    }
    
                }
    
                DataTable dtresults = new DataTable();
            
                connection.Close();
    
               
            }
    When I run this method, the list view gets updated as follows


    I want to be able to select one of the id's and press another button which will run another query (delet for example)

    I have tried
    Code:
    if (results_view.Items[0].Selected == true)
    {
    // Do Something
    }
    but even if the 0th item is clicked in the list view nothing happens

    The following is defined elsewhere:
    Code:
    ListViewItem item_object = null;
    Any ideas?
    Last edited by Niheel; Sep 4 '10, 09:19 PM. Reason: more post details merged
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    What happens if you make use of one of the Methods, MysqlDataReader states to be using:
    25.2.3.5. MySqlDataReader Reference
    Try GetString for example

    Comment

    • grahamb123
      New Member
      • Sep 2010
      • 2

      #3
      I'm not sure exactly what you mean?
      I can collect the data from the database

      I just need to be able to get the id or value of the item in the list when I click it. So if I click 2 in the list. I can store 2 in memory for some other operation? Or another query like delete?

      Comment

      • mzmishra
        Recognized Expert Contributor
        • Aug 2007
        • 390

        #4
        Did u try SelectedIndexCh anged event of listview

        Comment

        • MartijnHoekstra
          New Member
          • Sep 2010
          • 39

          #5
          Another possibility is to make use of the, FocusedItem.
          But if you are using multiselect; you may get better results by iterating through all the rows yourself. And check which of the ListViewItems have 'selected == true'

          Also you can have a look at all ListView members and its possibilities. Check here:
          ListView Members (System.Windows .Forms)

          Comment

          Working...