Avatar of Peter Kiers
Peter Kiers
Flag 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
.NET ProgrammingC#

Avatar of undefined
Last Comment
Shahan Ayyub

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Minh Võ Công

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Peter Kiers

ASKER
Hi,

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

Peter
Peter Kiers

ASKER
       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 Ayyub

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy