Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Select a new row in a listview.

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
ASKER CERTIFIED SOLUTION
Avatar of Cong Minh Vo
Cong Minh Vo
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Kiers

ASKER

Hi,

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

Peter
       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  <===========================

            }
        }
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