none
How to select first and last row in ListView from Button RRS feed

  • Question

  • Hi Guys I have connected to Database and filled my ListView but and I have two buttons (Select First,Select Last) how can I select first row and last row in ListView without using SQL Connection.. I want that when I click Select Last that last rows to be selected in my ListView 
    • Moved by CoolDadTx Monday, April 14, 2014 2:24 PM Winforms related
    Monday, April 14, 2014 1:10 PM

Answers

  • Hi,

    Selecting an item programmatically does not automatically change the focus to the ListView control. For this reason, you will typically want to call the Focus method when selecting an item.

    (Ref:http://msdn.microsoft.com/en-us/library/y4x56c0b(v=vs.90).aspx)

      private void button1_Click(object sender, EventArgs e)
            {
                this.listView1.Focus();
                this.listView1.Items[0].Selected = true;
                this.listView1.Items[listView1.Items.Count -1].Selected = true;
            }

    Regards,

    Marvin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Shreeharsh Ambli Tuesday, April 15, 2014 6:35 AM
    • Marked as answer by Zerhan Tuesday, April 15, 2014 8:41 AM
    Tuesday, April 15, 2014 3:03 AM
    Moderator

All replies

  • you can use the Selected property, like so:

    // select the 1st item
    yourListView.Items[0].Selected = true;
    hope this helps!

    Monday, April 14, 2014 1:52 PM
  • Hi I did first one with this thanks how about the last for example 
    lvwTable.Items[lvwTable.Items.Count-1].Selected= true; but again it selects the first one I don't know why it happens like that

    Monday, April 14, 2014 4:25 PM
  • Hi,

    Selecting an item programmatically does not automatically change the focus to the ListView control. For this reason, you will typically want to call the Focus method when selecting an item.

    (Ref:http://msdn.microsoft.com/en-us/library/y4x56c0b(v=vs.90).aspx)

      private void button1_Click(object sender, EventArgs e)
            {
                this.listView1.Focus();
                this.listView1.Items[0].Selected = true;
                this.listView1.Items[listView1.Items.Count -1].Selected = true;
            }

    Regards,

    Marvin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Shreeharsh Ambli Tuesday, April 15, 2014 6:35 AM
    • Marked as answer by Zerhan Tuesday, April 15, 2014 8:41 AM
    Tuesday, April 15, 2014 3:03 AM
    Moderator
  • thanks bro it is working but here you selected 1111 first but later you selected 4444 and still 1111 is selected how can I get rid of that?
    Tuesday, April 15, 2014 8:59 AM
  • I did bro thanks a lot in this way

    int myFocus;
            private void button1_Click(object sender, EventArgs e)
            {
                listView1.Items[Convert.ToInt32(myFocus)].Selected = false;
                myFocus = Convert.ToInt32(txtFind.Text)-1;
                listView1.Focus();
                listView1.Items[Convert.ToInt32(txtFind.Text) - 1].Selected = true;
                
            }

    Tuesday, April 15, 2014 9:17 AM