473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to get Valuemember of listbox in C#

frnds i m not getting first item of listbox details but i m getting otherthan first item listbox details when i click..on item in listbox. for loading values i m using below code..
Expand|Select|Wrap|Line Numbers
  1. public void bindinglistbox()
  2.         {
  3.             DataTable dataoffices;
  4.             dataoffices = obj.getoffices();
  5.  
  6.  
  7.  
  8.  
  9.             LTB_Existingoffices.DataBindings.Clear();
  10.             LTB_Existingoffices.DataBindings.Add("Text", States3, "LongName3");
  11.             LTB_Existingoffices.DataSource = dataoffices.DefaultView;
  12.             //LTB_Existingoffices.SelectedIndex = 0;
  13.             LTB_Existingoffices.DisplayMember = dataoffices.Columns["Name"].ToString();
  14.             LTB_Existingoffices.ValueMember = dataoffices.Columns["id"].ToString();
  15.  
  16.  
  17.  
  18.         }
  19.  
  20. for getting valuemember i m using this code..........
  21.  
  22.  
  23. private void LTB_Existingoffices_SelectedValueChanged(object sender, EventArgs e)
  24.         {
  25.             //if ((LTB_Existingoffices.SelectedIndex)  >= (LTB_Existingoffices.Items.Count-1) )
  26.             //{
  27.             //    LTB_Existingoffices.SelectedIndex = 0;
  28.                 if ((LTB_Existingoffices.SelectedIndex) > 0)
  29.                 {
  30.                     string myid;
  31.                     myid = (LTB_Existingoffices.SelectedValue.ToString());
  32.                     officeid = (Convert.ToInt32(myid));
  33.                     BTN_SAVE.Text = "Update";
  34.                     BTN_Del.Enabled = true;
  35.  
  36.                     binding();
  37.                 //}
  38.  
  39.                 //else
  40.                 //{
  41.                 //    LTB_Existingoffices.SelectedIndex += 1;
  42.                 //}
  43.  
  44.  
  45.             }
  46.         }
  47.  
any one plz Help me ........
Jul 31 '07 #1
3 4990
Frinavale
9,735 Expert Mod 8TB
private void LTB_Existingoffices_SelectedValueChanged(object sender, EventArgs e)
{
//if ((LTB_Existingoffices.SelectedIndex) >= (LTB_Existingoffices.Items.Count-1) )
//{
// LTB_Existingoffices.SelectedIndex = 0;
if ((LTB_Existingoffices.SelectedIndex) > 0)
{
string myid;
myid = (LTB_Existingoffices.SelectedValue.ToString());
officeid = (Convert.ToInt32(myid));
BTN_SAVE.Text = "Update";
BTN_Del.Enabled = true;

binding();
//}

//else
//{
// LTB_Existingoffices.SelectedIndex += 1;
//}


}
}
[/code]
any one plz Help me ........
Hi!

The first item in a ListBox is at index 0.
Your logic ignores 0 when you check:

if ((LTB_Existingoffices.SelectedIndex) > 0)

Try changing this to be
if ((LTB_Existingoffices.SelectedIndex) >= 0)


Cheers!

-Frinny
Jul 31 '07 #2
thnx for reply.... my problem clearded.. i used datasource after valuemember of control...

LTB_Existingoffices.DisplayMember = dataoffices.Columns["Name"].ToString();
LTB_Existingoffices.ValueMember = dataoffices.Columns["id"].ToString();
LTB_Existingoffices.DataSource = dataoffices.DefaultView;
Aug 1 '07 #3
Frinavale
9,735 Expert Mod 8TB
thnx for reply.... my problem clearded.. i used datasource after valuemember of control...

LTB_Existingoffices.DisplayMember = dataoffices.Columns["Name"].ToString();
LTB_Existingoffices.ValueMember = dataoffices.Columns["id"].ToString();
LTB_Existingoffices.DataSource = dataoffices.DefaultView;
Good Work!
Thanks for sharing the solution :)

Happy Coding!
-Frinny
Aug 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Supa Hoopsa | last post by:
Working on a windows form, I am trying to bind a listbox to a dataset and want to set the displaymember & Valuemember properties of the listbox so that I can retrieve the selectedvalue from a...
0
by: JMe9ka | last post by:
I have a ListBox to which I am assigning a DataTable as a DataSource, as shown below. ======================================================= DataTable VendorData = new DataTable(); // Code...
0
by: Alpha | last post by:
I am getting the following Exception error message when I assign the listbox Value to a table column in dataset. The dataset gets the tables and value from SQL table. There is no error if I...
2
by: Pat | last post by:
I'm a newbie to c# and could use some help - been banging my head on the keyboard for 3 days now. I have an unbound listbox that I'm populating this way: 1. loop through a datatable and load...
3
by: Henry Jones | last post by:
I want to populate a listbox with States but use abbreviations when someone selects a state. For instance if I have California Arizona Nevada in the listbox,
1
by: perrycheung221 | last post by:
Hi all, I got a problem of getting a display text and value from a item of a listbox Below is how I databind the data into myListBox dt is a dataTable with 2 column, ID (int) and value...
5
by: Spam Catcher | last post by:
Hello all, I can't seem to set the value member properly in code... I'm using a ComboBox in DropDownList mode: Private Class ApplicationsComboBoxItem Private _Name As String Private...
5
by: =?Utf-8?B?QmVu?= | last post by:
How would you add an item with its DisplayMember and ValueMember without binding to a datatable or dataview like: listbox.DisplayMember = "name"; listbox.ValueMember = "recid"; listbox.DataSource...
2
by: =?Utf-8?B?UGFvbG8=?= | last post by:
From a combo box I use the SelectedValue property as a parameter to a query from which I populate a ListBox private void cmbxCategory_SelectedValueChanged(object sender, EventArgs e) { string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.