Listbox with hidden id
-
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.
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
andValueMember
properties to the name of the fields in theDataTable
(if using aDataSet
prefix them with the name of the table), then set theDataSource
property to the instance of theDataSet
orDataTable
.With a custom class: First define your custom class
public class MyData
{
private int id;
public string theField;
// More data memberspublic MyData(int id, string theField)
{
this.id = id;
this.theField = theField;
}
// More Constructorspublic int ID
{
get { return id; }
}public string TheField
{
get { return theField; }
set { theField = value; }
}
}Now on the
ListBox
, set theValueMember
to "ID" and theDisplayMember
to "TheField". Once you have a collection ofMyData
objects (anArray
orArrayList
or something that implementsIList
which returns instances ofMyData
) assign that collection to theDataSource
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
-
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
andValueMember
properties to the name of the fields in theDataTable
(if using aDataSet
prefix them with the name of the table), then set theDataSource
property to the instance of theDataSet
orDataTable
.With a custom class: First define your custom class
public class MyData
{
private int id;
public string theField;
// More data memberspublic MyData(int id, string theField)
{
this.id = id;
this.theField = theField;
}
// More Constructorspublic int ID
{
get { return id; }
}public string TheField
{
get { return theField; }
set { theField = value; }
}
}Now on the
ListBox
, set theValueMember
to "ID" and theDisplayMember
to "TheField". Once you have a collection ofMyData
objects (anArray
orArrayList
or something that implementsIList
which returns instances ofMyData
) assign that collection to theDataSource
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
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;
-
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;
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
-
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
andValueMember
properties to the name of the fields in theDataTable
(if using aDataSet
prefix them with the name of the table), then set theDataSource
property to the instance of theDataSet
orDataTable
.With a custom class: First define your custom class
public class MyData
{
private int id;
public string theField;
// More data memberspublic MyData(int id, string theField)
{
this.id = id;
this.theField = theField;
}
// More Constructorspublic int ID
{
get { return id; }
}public string TheField
{
get { return theField; }
set { theField = value; }
}
}Now on the
ListBox
, set theValueMember
to "ID" and theDisplayMember
to "TheField". Once you have a collection ofMyData
objects (anArray
orArrayList
or something that implementsIList
which returns instances ofMyData
) assign that collection to theDataSource
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
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
-
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
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