Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to insert directly listbox valuemember and displaymember

Status
Not open for further replies.

AnNguyen

Programmer
Jul 18, 2002
33
US
Hello
I try to bind both displaymember and value member of a listbox to a dtataset data, but this time the displaymember witth multiple field value as follow.
....
string SSQL= "SELECT ID, Fname,LName,Init From tblEmp";
sqlDA = new SqlDataAdapter(sSQL,sqlconn);
DS = sqlDA.Fill("General");l
listbox.DisplayMember= DS.Tables[0].Columns[1].ToString() + DS.Tables[0].Columns[2].ToString();
listbox.ValueMember= DS.Tables[0].Columns[0].ToString();
listbox.datasource =DS.Table[0];
....

but the listbox display all entries with value 'System.Data.DataRowView'.

is it posible to do that, and if so How?
ANY HELP WOULD BE VERY APPRECIATED.




 
You could change your SQL to generate a single field with all the values that you want to display and then use this field in displaymember.
You can use a SELECT CASE to do this.

And then silence smacks right in there...
... And it is loud!
 
The DisplayMember property expects a string that represent the name of a an existing property or an empty string.
If that property doesn't exist then the code calls ToString() method of the object and in this case it is System.Data.DataRowView.
One way is to provide a column in the DataTable with the right value to display.
Another method is to use Column expression e.g. after Fill() is done, add a column expression in the table[0] to express what you want.
Example:
Code:
myDataColumn.Expression = "SUBSTRING(FirstName,0,25)+SUBSTRING(LastName,0,15)";
where 'FirstName' and "LastName" are column in that table.
Another method is to implement a class to embeed a DataRow object and there add properties to be used by DisplayMember.
Example:
Code:
class CMyDataRow 
{
     DataRow m_DataRow;
     public CMyDataRow( DataRow dr)
     {
        m_DataRow = dr;
     }
     public object ValColumn
     {
          get
	    {
	      return dr["FirstName];
            }
         
     }
     public string CalcName
        {
        
            get
            {
                return dr["FirstName].ToString() + dr["LastName"].ToString();
            }
        }
     
}

ArrayList MyRows = new ArrayList();
foreach (DataRow dr in DS.Tables[0].Rows)
{
      MyRows.Add(dr);
}
ListBox1.DataSource = MyRows ;
ListBox1.DisplayMember = "CalcName";
ListBox1.ValueMember = "ValColumn" ;
obislavu
 
Thankyou all

I found a way to solve my problem thanks to obislavu from this link
and here my code if any body need it

string SSQL= "SELECT ID, Fname,LName,Init From tblEmp";
sqlDA = new SqlDataAdapter(sSQL,sqlconn);
DS = sqlDA.Fill("General");l
DataColumn dc = new DataColumn("Disp");

DS.Tables["General"].Columns.Add(dc);
foreach ( DataRow dr in DS.Tables["Details"].Rows )
{
dr["Disp"] = dr["FName"].ToString() +" "+ dr["Init"].ToString().Substring(0,20)+", "+dr["Lname"].ToString();
};
listbox.DisplayMember= DS.Tables["Details"].Columns["Disp"].ToString();
listbox.ValueMember= DS.Tables["Details"].Columns["ID"].ToString();
listbox.datasource =DS.Table["General"];

That is all folks and cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top