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 |