I am working on a sof开发者_JAVA技巧tware in which i need to display the search query results in a listbox. mine is a medical software and im trying to display the list of medicines in the listbox. im using c# for coding under Windows Visual studio 10 platform. please help me wid this. regards
Im using Sql server management studio as backend. im using a button which when clicked will fire an event performing a query. the query is select * from Table_name i want to show the query list in the listbox..
Within the button_click event:
- Configure a SqlCommand with CommandType=Text and CommandText="your query".
- Execute the SqlCommand with ExecuteReader.
- Convert the resulting SqlDataReader in a DataTable with dataTable.Load(dataReader);
- Make that datatable your ListBox's Data Source (libYourListBox.DataSource = dataTable).
- Define your list box's DisplayMember to the name of the column that holds the medicine's name.
- Define your list box's ValueMember to the id or code of the medicin.
That should work.
try something like this:
SqlDataAdapater adp=new SqlDataAdapter(querystr,"set connection string");
DataTable dt=new DataTable();
adp.Fill(dt);
List.DataSource=dt;
List.DisplayMember=dt.Columns[0].ColumnName;
精彩评论