Select a new row in a listview.

Peter Kiers
Peter Kiers used Ask the Experts™
on
Hi,

I have a listview with 1 button on my form, and the listview is connected to a database.
When I press on the button a childform appears with a textbox on it where the user
can enter text. After pressing the x-button of the childform the text of the textbox will be
written on the listview on the mainform, so a new row will be inserted into the listview.
But how can I automaticly select the new inserted row in the listview?


        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
          {
            if (frmNote.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
               listView1.Items.Add(frmNote.textBox1.Text, 0);
               //SELECT THE NEW INSERTED ROW
            }
        }

Greetings, Peter Kiers
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ListViewItem lv =  listView1.Items.Add(frmNote.textBox1.Text, 0);
lv.Selected = true;
Peter KiersOperator

Author

Commented:
Hi,

I have tested the code but it does not select the node.

Peter
Peter KiersOperator

Author

Commented:
       private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
          {
            if (frmNote.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
              listView1.Items.Add(frmNote.textBox1.Text, 0);
            //select the new item  <===========================

            }
        }
Shahan AyyubSenior Software Engineer

Commented:
You can this piece of code:

ListViewItem lv = new ListViewItem(frmNote.textBox1.Text, 0);
ListView1.Items.Add(lv);
ListView1.Focus();
ListView1.SelectedItems.Clear();
ListView1.Items(ListView1.Items.Count - 1).Selected = true;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial