Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Retrieve index value for Listbox
 

Retrieve index value for Listbox

Hi everyone,

Just want to know, I have a bound listbox where the ValueMember is the PrimaryKey (Incremental Integer) of a database table, and the DisplayMember being bound to another string column.

I am adding new data into the database and displaying it successfully in the listbox. Also in my insert stored procedure I am returning the @@Identity value for the newly inserted row and assigning it to an integer variable in my form from the return parameter. What I want to do is highlight the selection in my listbox for the newly added entry. I know that I have to target the SelectedIndex but have no idea how to "find" the index value for the given return parameter value.

FYI: The listbox is on 1 form and when the Add button is hit, a dialog box pops up with a single textbox in the center prompting the user for a new entry.

I am coding in VB.

rgdz
Shalan
shalan99  Saturday, September 29, 2007 11:34 AM

Hi Shalan,

Based on your post, you want select the new added entry in your ListBox, don’t you? If so, I you use either SelectedIndex or SelectedValue of your ListBox, but your situation the SelectedValue is more suitable. Try something like the following:

Code Block

ListBoxP

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

DataTable dt = new DataTable();

int count = 50;

private void Form3_Load(object sender, EventArgs e)

{

dt.Columns.Add("id",typeof(int));

dt.Columns.Add("name");

DataColumn [] keycol = new DataColumn [] { dt.Columns[0]};

dt.PrimaryKey = keycol;

for (int i = 0; i < 50; i++)

{

dt.Rows.Add(i, "name" + i.ToString("00"));

}

this.listBox1.DisplayMember = "name";

this.listBox1.ValueMember = "id";

this.listBox1.DataSource = dt;

}

private void button1_Click(object sender, EventArgs e)

{

dt.Rows.Add(count, "name" + count.ToString("00"));

this.listBox1.SelectedValue = count;//change to your new added entry's identity.

count++;

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, October 04, 2007 3:17 AM

Hi Shalan,

Based on your post, you want select the new added entry in your ListBox, don’t you? If so, I you use either SelectedIndex or SelectedValue of your ListBox, but your situation the SelectedValue is more suitable. Try something like the following:

Code Block

ListBoxP

public partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

DataTable dt = new DataTable();

int count = 50;

private void Form3_Load(object sender, EventArgs e)

{

dt.Columns.Add("id",typeof(int));

dt.Columns.Add("name");

DataColumn [] keycol = new DataColumn [] { dt.Columns[0]};

dt.PrimaryKey = keycol;

for (int i = 0; i < 50; i++)

{

dt.Rows.Add(i, "name" + i.ToString("00"));

}

this.listBox1.DisplayMember = "name";

this.listBox1.ValueMember = "id";

this.listBox1.DataSource = dt;

}

private void button1_Click(object sender, EventArgs e)

{

dt.Rows.Add(count, "name" + count.ToString("00"));

this.listBox1.SelectedValue = count;//change to your new added entry's identity.

count++;

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, October 04, 2007 3:17 AM
Hi Rong, and thanx for the reply!

After watching some WinForm vids, I now have my listbox bound to a BindingSource. Still using the same approach of data access with dialog forms, I am able to have my listbox position itself on the newly entered item!


Thanx anyway for the suggestion and the code, and sorry if it put u out of your way!

cheers
-shalan
shalan99  Friday, October 05, 2007 11:17 PM

You can use google to search for other answers

Custom Search

More Threads

• changing editable DataGridView into not editable
• Cancel from an AddNew in DataGridView
• How Do I Add TextBox & ComboBox Text To A DataGrid Thats Been Binded To A Access Database?
• adding new rows to already bound datagrid in frameowrk 1.1
• [C#]datagridview copy/paste entire rows
• DatePicker Cell Disappearing
• DataGridView, Transparent Selected row color
• Problem Add Data Source
• timer saving in database MYSQL
• Host Controls in Windows Forms DataGridView Cells