How to get Valuemember of listbox in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crsqn91l
    New Member
    • Jul 2007
    • 11

    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..
    [code=cpp]
    public void bindinglistbox( )
    {
    DataTable dataoffices;
    dataoffices = obj.getoffices( );




    LTB_Existingoff ices.DataBindin gs.Clear();
    LTB_Existingoff ices.DataBindin gs.Add("Text", States3, "LongName3" );
    LTB_Existingoff ices.DataSource = dataoffices.Def aultView;
    //LTB_Existingoff ices.SelectedIn dex = 0;
    LTB_Existingoff ices.DisplayMem ber = dataoffices.Col umns["Name"].ToString();
    LTB_Existingoff ices.ValueMembe r = dataoffices.Col umns["id"].ToString();



    }

    for getting valuemember i m using this code..........


    private void LTB_Existingoff ices_SelectedVa lueChanged(obje ct sender, EventArgs e)
    {
    //if ((LTB_Existingo ffices.Selected Index) >= (LTB_Existingof fices.Items.Cou nt-1) )
    //{
    // LTB_Existingoff ices.SelectedIn dex = 0;
    if ((LTB_Existingo ffices.Selected Index) > 0)
    {
    string myid;
    myid = (LTB_Existingof fices.SelectedV alue.ToString() );
    officeid = (Convert.ToInt3 2(myid));
    BTN_SAVE.Text = "Update";
    BTN_Del.Enabled = true;

    binding();
    //}

    //else
    //{
    // LTB_Existingoff ices.SelectedIn dex += 1;
    //}


    }
    }
    [/code]
    any one plz Help me ........
    Last edited by Frinavale; Jul 31 '07, 01:24 PM. Reason: Added [code] tags to make more legible
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by crsqn91l
    private void LTB_Existingoff ices_SelectedVa lueChanged(obje ct sender, EventArgs e)
    {
    //if ((LTB_Existingo ffices.Selected Index) >= (LTB_Existingof fices.Items.Cou nt-1) )
    //{
    // LTB_Existingoff ices.SelectedIn dex = 0;
    if ((LTB_Existingo ffices.Selected Index) > 0)
    {
    string myid;
    myid = (LTB_Existingof fices.SelectedV alue.ToString() );
    officeid = (Convert.ToInt3 2(myid));
    BTN_SAVE.Text = "Update";
    BTN_Del.Enabled = true;

    binding();
    //}

    //else
    //{
    // LTB_Existingoff ices.SelectedIn dex += 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_Existingo ffices.Selected Index) > 0)

    Try changing this to be
    if ((LTB_Existingo ffices.Selected Index) >= 0)


    Cheers!

    -Frinny

    Comment

    • crsqn91l
      New Member
      • Jul 2007
      • 11

      #3
      thnx for reply.... my problem clearded.. i used datasource after valuemember of control...

      LTB_Existingoff ices.DisplayMem ber = dataoffices.Col umns["Name"].ToString();
      LTB_Existingoff ices.ValueMembe r = dataoffices.Col umns["id"].ToString();
      LTB_Existingoff ices.DataSource = dataoffices.Def aultView;

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by crsqn91l
        thnx for reply.... my problem clearded.. i used datasource after valuemember of control...

        LTB_Existingoff ices.DisplayMem ber = dataoffices.Col umns["Name"].ToString();
        LTB_Existingoff ices.ValueMembe r = dataoffices.Col umns["id"].ToString();
        LTB_Existingoff ices.DataSource = dataoffices.Def aultView;
        Good Work!
        Thanks for sharing the solution :)

        Happy Coding!
        -Frinny

        Comment

        Working...