Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Listbox with hidden id

Listbox with hidden id

Scheduled Pinned Locked Moved C#
database
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    viana
    wrote on last edited by
    #1

    Hi, i need to create a listbox from a database, which should display the name of a field and have the row id hidden, that i can "see" to manipulate the database table.

    J 1 Reply Last reply
    0
    • V viana

      Hi, i need to create a listbox from a database, which should display the name of a field and have the row id hidden, that i can "see" to manipulate the database table.

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      There are a few ways of going about this, one is with a DataSet/DataTable the other is using a custom class. With a DataSet:

      Set the listbox's DisplayMember and ValueMember properties to the name of the fields in the DataTable (if using a DataSet prefix them with the name of the table), then set the DataSource property to the instance of the DataSet or DataTable.

      With a custom class: First define your custom class

      public class MyData
      {
      private int id;
      public string theField;
      // More data members

      public MyData(int id, string theField)
      {
      this.id = id;
      this.theField = theField;
      }
      // More Constructors

      public int ID
      {
      get { return id; }
      }

      public string TheField
      {
      get { return theField; }
      set { theField = value; }
      }
      }

      Now on the ListBox, set the ValueMember to "ID" and the DisplayMember to "TheField". Once you have a collection of MyData objects (an Array or ArrayList or something that implements IList which returns instances of MyData) assign that collection to the DataSource property of the ListBox.

      MyData [] data = new MyData[3];
      data[0] = new MyData(0, "Huey");
      data[1] = new MyData(1, "Dewy");
      data[2] = new MyData(2, "Louie");

      myListBox.DisplayMember = "TheField";
      myListBox.ValueMember = "ID";
      myListBox.DataSource = data;

      HTH, James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

      V 2 Replies Last reply
      0
      • J James T Johnson

        There are a few ways of going about this, one is with a DataSet/DataTable the other is using a custom class. With a DataSet:

        Set the listbox's DisplayMember and ValueMember properties to the name of the fields in the DataTable (if using a DataSet prefix them with the name of the table), then set the DataSource property to the instance of the DataSet or DataTable.

        With a custom class: First define your custom class

        public class MyData
        {
        private int id;
        public string theField;
        // More data members

        public MyData(int id, string theField)
        {
        this.id = id;
        this.theField = theField;
        }
        // More Constructors

        public int ID
        {
        get { return id; }
        }

        public string TheField
        {
        get { return theField; }
        set { theField = value; }
        }
        }

        Now on the ListBox, set the ValueMember to "ID" and the DisplayMember to "TheField". Once you have a collection of MyData objects (an Array or ArrayList or something that implements IList which returns instances of MyData) assign that collection to the DataSource property of the ListBox.

        MyData [] data = new MyData[3];
        data[0] = new MyData(0, "Huey");
        data[1] = new MyData(1, "Dewy");
        data[2] = new MyData(2, "Louie");

        myListBox.DisplayMember = "TheField";
        myListBox.ValueMember = "ID";
        myListBox.DataSource = data;

        HTH, James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

        V Offline
        V Offline
        viana
        wrote on last edited by
        #3

        hi, if i understand this rigth in a listbox i can have a field to be displayed and a value "linked" to that field. what i did was: public class BaseDados { public DataTable doquery(string query) { OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); DataAdapter.Fill(dataSet, "Same"); DataTable dataTable = dataSet.Tables[0]; return dataTable; } } and in the class of the form where is my listbox i put: BaseDados db = new BaseDados(); DataTable table = db.doquery("Select * from autenticacao"); listBoxOrigem.DisplayMember = "email"; listBoxOrigem.ValueMember = "id"; listBoxOrigem.DataSource = table;

        J 1 Reply Last reply
        0
        • V viana

          hi, if i understand this rigth in a listbox i can have a field to be displayed and a value "linked" to that field. what i did was: public class BaseDados { public DataTable doquery(string query) { OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); DataAdapter.Fill(dataSet, "Same"); DataTable dataTable = dataSet.Tables[0]; return dataTable; } } and in the class of the form where is my listbox i put: BaseDados db = new BaseDados(); DataTable table = db.doquery("Select * from autenticacao"); listBoxOrigem.DisplayMember = "email"; listBoxOrigem.ValueMember = "id"; listBoxOrigem.DataSource = table;

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          viana wrote: if i understand this rigth in a listbox i can have a field to be displayed and a value "linked" to that field. I think you got it :) James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

          1 Reply Last reply
          0
          • J James T Johnson

            There are a few ways of going about this, one is with a DataSet/DataTable the other is using a custom class. With a DataSet:

            Set the listbox's DisplayMember and ValueMember properties to the name of the fields in the DataTable (if using a DataSet prefix them with the name of the table), then set the DataSource property to the instance of the DataSet or DataTable.

            With a custom class: First define your custom class

            public class MyData
            {
            private int id;
            public string theField;
            // More data members

            public MyData(int id, string theField)
            {
            this.id = id;
            this.theField = theField;
            }
            // More Constructors

            public int ID
            {
            get { return id; }
            }

            public string TheField
            {
            get { return theField; }
            set { theField = value; }
            }
            }

            Now on the ListBox, set the ValueMember to "ID" and the DisplayMember to "TheField". Once you have a collection of MyData objects (an Array or ArrayList or something that implements IList which returns instances of MyData) assign that collection to the DataSource property of the ListBox.

            MyData [] data = new MyData[3];
            data[0] = new MyData(0, "Huey");
            data[1] = new MyData(1, "Dewy");
            data[2] = new MyData(2, "Louie");

            myListBox.DisplayMember = "TheField";
            myListBox.ValueMember = "ID";
            myListBox.DataSource = data;

            HTH, James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

            V Offline
            V Offline
            viana
            wrote on last edited by
            #5

            hi, if i understand this rigth in a listbox i can have a field to be displayed and a value "linked" to that field. what i did was: public class BaseDados { public DataTable doquery(string query) { OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); DataAdapter.Fill(dataSet, "Same"); DataTable dataTable = dataSet.Tables[0]; return dataTable; } } and in the class of the form where is my listbox i put: BaseDados db = new BaseDados(); DataTable table = db.doquery("Select * from autenticacao"); listBoxOrigem.DisplayMember = "email"; listBoxOrigem.ValueMember = "id"; listBoxOrigem.DataSource = table; and it display the field email correctly. How can i read the selected item from my listbox, the id i tried: string b=listBoxOrigem.SelectedItem.ToString(); but it did't work

            J 1 Reply Last reply
            0
            • V viana

              hi, if i understand this rigth in a listbox i can have a field to be displayed and a value "linked" to that field. what i did was: public class BaseDados { public DataTable doquery(string query) { OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); DataAdapter.Fill(dataSet, "Same"); DataTable dataTable = dataSet.Tables[0]; return dataTable; } } and in the class of the form where is my listbox i put: BaseDados db = new BaseDados(); DataTable table = db.doquery("Select * from autenticacao"); listBoxOrigem.DisplayMember = "email"; listBoxOrigem.ValueMember = "id"; listBoxOrigem.DataSource = table; and it display the field email correctly. How can i read the selected item from my listbox, the id i tried: string b=listBoxOrigem.SelectedItem.ToString(); but it did't work

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              I thought it was odd you'd put all of that in a reply just to say you have it right :) viana wrote: How can i read the selected item from my listbox, the id string b = listBoxOrigem.Selected**Value**.ToString(); James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups