I have a listbox whose list is being populated through a datasource ...That list contains names of some products.when the list is being populated control goes to the first name on the list ....and without even selecting the first name its popping up the details of that name. I used listbox.selectedindex=-1;i
t worked for a while...
But my scenario is I have a datagrid which contains so many rows if I select on one row then listbox populates with those values through datasource and here the lsitbox1.selectedindex=-1 is working but now if I select an other row after that ..the values are populated in the list box but the control goes to the first name and displays its details.....how do I fix this??
I feel the values in the listbox are not being cleared or refreshed....so that next time when I am selecting another row its showing some of the values which are already there in the first one.....not sure but my vague guess....
Help is appreciated!
Here goes my sample code
private List<UserData.ProductsRegistered> proreg;
private void ProductsRegistered(string UserName, string productID,string Name)
proreg =userdata.GetRegisteredProducts(UserName, productID,Name);
listBox1.DataSource = proreg;
listBox1.DisplayMember = Name;
listBox1.SelectedIndexChanged += new EventHandler(this.listBox1_SelectedIndexChanged);
//In a list box the control automatically goes to the first place but should be fired only
//if the first element is selected....to avoid the control being highlighted use selected index
listBox1.SelectedIndex = -1;
//selecting individual items in a list box
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
// Get the currently selected item in the ListBox.
string Name = "";
// Name= listBox1.SelectedItem.ToString();
// string Name = listBox1.SelectedItem.ToString();
string UserId = "";
string SerialNumber = "";
string UserName = "";
string ProductId = "";
//populate the product Name and load the listbox with all values
if (null != listBox1.SelectedItem)
Name = listBox1.SelectedItem.ToString();
UserData.Name info = userdata.GetInfoName(UserName, Name);
ProductId = info.ProductId;
UserData.UID ui = userdata.GetUserID(ProductId);
UserId = ui.UserId;
UserData.LicenseInfo LInfo = userdata.GetNameInfo(UserId, ProductId, Name);
SerialNumber = LInfo.SerialNumber;
//where i = to  for example username=chambo1
//i=systemmechanic 7 professional
//Loop through and check the count ....items are added through datasource property..
int _count = listBox1.Items.Count;
if (_count != 0)
for (int i = 0; i < _count; i++)
MessageBox.Show("Serial No:= " + SerialNumber.ToString() + "UserID:= " + UserId.ToString(), "LicenseInfo");
//If listbox values to be cleared.
// listBox1.SelectedIndex = -1;
if (listBox1.SelectedIndex > -1)