haroldjclements Posted January 12, 2005 Posted January 12, 2005 Hello This is a quite easy question I hope. I am programming in J# (apologise if this is not the correct forum, I am just seeing if anyone has an idea). I am trying to obtain to fields from a database and deposit them into a comboBox. I have my database setup and retrieving the data. My problem is how I get the data into the comboBox. An example of the data is the database is: NameID Name 1 Harold 2 Nick 3 Bob 4 James When a name is selected the NameID is passed to the next form to retrieve more into on the person. This is what I have so far. <CODE> sql = "SELECT * FROM tblPeople"; OleDbCommand oc = new OleDbCommand(sql, myConnection); myConnection.Open(); OleDbDataReader dr = oc.ExecuteReader(); while (dr.Read()) { comboBox1.get_Items().Add(dr.GetString(1) + " (" + dr.GetString(2) + ")"); } myConnection.Close(); </CODE> This adds the names to the comboBox but gives me a blank box until I pull the box down. Really I would like something like �Select Name� and give it the index of 0. I understand that I have not added any index�s in, this is because I am unsure how to. Any help anyone could give me with this would be very much appreciated. Quote
Ontani Posted January 12, 2005 Posted January 12, 2005 I don't know anything about J# but i can tell if you do this in VB.NET its the value you give to the combobox the value that shows until its pulled down ComboBox1.Text = "Select Name" Quote www.purevision.be :: www.devpoint.be
haroldjclements Posted January 12, 2005 Author Posted January 12, 2005 I don't know anything about J# but i can tell if you do this in VB.NET its the value you give to the combobox the value that shows until its pulled down ComboBox1.Text = "Select Name" Cheers, it's comboBox1.set_Test = "Select Name"; Now how do tell the comboBox the value that the name represents (the db's unique ID). Thanks again, Harold Quote
Ontani Posted January 12, 2005 Posted January 12, 2005 in VB.NET its something like this: Dim index As Integer index = ComboBox1.SelectedIndex Quote www.purevision.be :: www.devpoint.be
RobEmDee Posted January 12, 2005 Posted January 12, 2005 haroldjclements: Have you considered putting the data into a DataTable and then binding the ComboBox to it? In my opinion, it is a lot easier to work with underlying data through a bound control when you do so. I'll give you a quick example using C#: // DataTable tblData has been filled with data from db // Add generic row DataRow row = tblData.NewRow(); row["Name"] = "Select Name"; row["ID"] = -1; tblData.Rows.Add(row); // Sort so that generic row is always first in table tblData.DefaultView.Sort = "ID ASC"; // Bind ComboBox1 this.ComboBox1.DataSource = tblData; this.ComboBox1.DisplayMember = "Name"; this.ComboBox1.ValueMember = "ID"; // Select generic row in ComboBox1 this.ComboBox1.SelectedValue = -1; // You can now retrieve the bound data from the ComboBox a few different ways // Get selected value - what you set for ComboBox1.ValueMember int selID = (int) this.ComboBox1.SelectedValue; // Get selected text - what you set for ComboBox1.DisplayMember string selText = this.ComboBox1.SelectedText; // Get selected row - the whole corresponding DataRow in the DataTable for the selected item. DataRowView rowSel = (DataRowView) this.ComboBox1.SelectedItem; int selID = (int) rowSel["ID"]; string selName = rowSel["Name"].ToString(); Quote
donnacha Posted January 14, 2005 Posted January 14, 2005 You can add the data directly to the combobox using the ".add", it is basically a collection and then you can use it away. But as suggested above, putting it in a dataset may help in the long run as it may also be worth storing this info in you database to allow for updates without having to change your code... Quote Hamlet
haroldjclements Posted January 15, 2005 Author Posted January 15, 2005 Thanks Thanks guys for all you answers their very much appreciated. Quote
haroldjclements Posted January 15, 2005 Author Posted January 15, 2005 Ok, this piece of code works fine for me. What I want to be able to do is add a value (obtained from the database) to each line of the comboBox so that if the data is saved only the �ID� is saved and not the string description. I am struggling with the .NET syntax as much as anything and using J# is even harder as the volume of resource�s are very limited. Thanks in advance, Harold Clements sql = "SELECT * FROM tblComponentType"; OleDbCommand oc = new OleDbCommand(sql, myConnection); myConnection.Open(); OleDbDataReader dr = oc.ExecuteReader(); while (dr.Read()) { comboBox1.get_Items().Add(dr.GetString(1) + " (" + dr.GetString(2) + ")"); } myConnection.Close(); Quote
RobEmDee Posted January 15, 2005 Posted January 15, 2005 haroldjclements: I tried out J#...below is an excerpt of my code...hope it helps. private DataTable tblData; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // Bind combo box this.BindData(); } private void BindData() { // Initialize form level DataTable this.tblData = new DataTable(); this.tblData.get_Columns().AddRange(new DataColumn[]{new DataColumn("ID"), new DataColumn("Name")}); // Create some test data (you could replace this with DataReader or DataAdapter for(int count = 0; count <= 10; count++) { DataRow row = tblData.NewRow(); row.set_Item("ID", System.Convert.ToString(count)); row.set_Item("Name", "Item_" + System.Convert.ToString(count)); tblData.get_Rows().Add(row); } // Bind comboBox1 to data source this.comboBox1.set_DataSource(tblData); this.comboBox1.set_ValueMember("ID"); this.comboBox1.set_DisplayMember("Name"); // Register comboBox1 event this.comboBox1.add_SelectedIndexChanged( new System.EventHandler(this.comboBox1_SelectedIndexChanged) ); } private void comboBox1_SelectedIndexChanged (Object sender, System.EventArgs e) { // Retrieve selected row from comboBox1 DataRowView row; row = (DataRowView) this.comboBox1.get_SelectedItem(); // View the desired data - at this point you can do whatever you want with the data from the underlying DataRow MessageBox.Show("Selected ID: " + row.get_Item("ID").toString() + "\n Selected Text: " + row.get_Item("Name").toString()); row = null; } My code is probably not as optimal as possible.....I have never used J# and I don't fully understand the management of type conversions in the language. You are right about the online J# documentation...pretty thin. MS is in the process of adding J# examples to the QuickStarts . Quote
haroldjclements Posted January 17, 2005 Author Posted January 17, 2005 That is excellent, it works beautifully. Thanks everyone :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.