Listbox selected Item

Posted by Toashish80 under C# on 3/2/2011 | Points: 10 | Views : 11165 | Status : [Member] | Replies : 6
I have a listbox containing a list of items coming from the database. When I select an item from the listbox, i want to display some attributes of the selected item. I am not able to trap the event which is getting fired when I select an item in the listbox. I have written code on listbox selected index change event which is firing only for the 0th index and not firing when I am changing the selection from 0th item to any other item.




Responses

Posted by: Vuyiswamb on: 3/2/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Let me see your code in the Selected index change event

Thank you for posting at Dotnetfunda
[Administrator]

Toashish80, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Toashish80 on: 3/2/2011 [Member] Starter | Points: 25

Up
0
Down
Below is the code which I have written on selected Index change event:

int intSelectedFile = 0;
DataTable dtfileAttributes = new DataTable();
LegacyMaster objLgcy = new LegacyMaster();

DataRowView dRow = lstFiles.SelectedItem as DataRowView;

intSelectedFile = Convert.ToInt32(dRow[0]);

dtfileAttributes = objLgcy.GetFileAttributes(intSelectedFile);

DisplayFileAttributes(dtfileAttributes);

Toashish80, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naimishforu on: 3/2/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
The below code is working for me :


using System.Drawing;

using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(listBox1.SelectedItem.ToString());
}
}
}


Thanks,
Naimish
http://dotnetinterview.zxq.net/
http://naimishpandya.blogspot.in/

Toashish80, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabhakar on: 3/2/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
hi Toashish80

i think namish is right u write ur code in SelectedIndexChanged event . .

Best Regard's
Prabhakar

Toashish80, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SheoNarayan on: 3/2/2011 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Very frequently people forget to specify AutoPostback=true property for the ListBox, see if you have written that. If this is already there then make sure that you are populating the ListBox in !IstPostback in the Page_Load method (in case of asp.net).

Hope this will help.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Toashish80, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 3/2/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
:) i like the response, before i replied i see you all gave him a response

Thank you for posting at Dotnetfunda
[Administrator]

Toashish80, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response