listview question

This is a discussion on listview question within the C# Programming forums, part of the General Programming Boards category; okay i have a listView with the detail option on and it has columns ...Actually the question is how can ...

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    listview question

    okay i have a listView with the detail option on and it has columns ...Actually the question is how can i get the selected index of the row clicked in the listView ???\

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    ListViews have ColumnClick Events. The second parameter of this event has a member of type ColumnHeader. This is the ColumnHeader that was clicked.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Okay so i found out how to get the selected item...But now my code is giving me alil problem ..which i have no idea about..

    Code:
    private void bookingListView_SelectedIndexChanged(object sender, System.EventArgs e)
    	{
    		clearFields();
    		OleDbDataReader dbReader = null;
    		string[] columns = new string[15];
    
    		try 
    		{
    			conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;
    										User Id =; Password=; 
    										Data Source=C:\professional\database"  + @"\" + companys[companyComboBox.SelectedIndex] + @"\" + companys[companyComboBox.SelectedIndex] + @"_data.mdb" );
    			conn.Open();
    			OleDbCommand cmd =  conn.CreateCommand();
    			cmd.CommandText = "SELECT * FROM "+ client.GetKey(clientComboBox.SelectedIndex).ToString().Replace(' ','_');
    			
    				
    			dbReader = cmd.ExecuteReader();
    		
    
    			//while( dbReader.Read() )
    			//{
    			MessageBox.Show(bookingListView.SelectedItems[0].Text/*bookingListView.Items[0].SubItems[0].Text*/,"" ,MessageBoxButtons.OK );
    			
    			//}
    						
    					
    		//	conn.Close();
    		}
    
    		catch( Exception error )
    		{
    			MessageBox.Show("Exception: " + error.Message, "Error", MessageBoxButtons.OK );
    		}
    		finally
    		{
    			if ( conn != null )
    			{
    				conn.Close();
    			}
    		}
    	
    	}
    The exception thrown was "specific argument was out of the range of valid values.Parameter name : index"

    It still works fine after throwing the exception ....When i try doing this bookingListView1.SelectedItems[1].Text it throws the same exception ..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    bookingListView.SelectedItems[0].Text

    There is not necessarily any SelectedItems. If you deselected the item, you will also get an SelectedIndexChanged event, but you will not have any SelectedItems.

    Try

    foreach( ListViewItem item in x.SelectedItems )
    {
    // action
    }

    OR

    make sure you have selected items by querying SelectedItems.Count
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    thanks ...Actually i used another method and it worked ..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubles with ListView and Toolbar
    By cornholio in forum Windows Programming
    Replies: 8
    Last Post: 11-14-2005, 12:26 AM
  2. Listview with c/c++
    By X PaYnE X in forum Windows Programming
    Replies: 8
    Last Post: 03-20-2005, 10:29 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. another listview question
    By Smoose777 in forum C# Programming
    Replies: 1
    Last Post: 06-22-2003, 11:24 PM
  5. Listview??
    By SuperNewbie in forum C# Programming
    Replies: 4
    Last Post: 02-13-2003, 02:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21