I’ve made a previous post that tried to use a textbox. From this I found out you can simply add an sql query results (the excute reader) to the ComboBox and then display and use the other column value.
Problem I have is I’m using a task for my form that runs a different HUGE sql query so it does not lock up my controls in my form. The problem, in detail, is that I’m using an invoke method wrapped around that control that only gets the 1st column.
public void fillmycombo()
{
SqlConnection conn1 = new SqlConnection(myConn1);
conn1.Open();
if (string.Compare(_userName, admin) == 0)
{
SqlCommand accountFill = new SqlCommand("SELECT name, FROM dbo.Customer", conn1);
SqlDataReader readacc = accountFill.ExecuteReader();
while (readacc.Read())
{
AddItem(readacc.GetString(0).ToString());
//accCollection.DataSource = readacc;
//accCollection.DisplayMember = "name";
//accCollection.ValueMember = "keycode";
}
conn1.Close();
}
}
this method as you can see gets the name.
private void AddItem(string value)
{
if (accCollection.InvokeRequired)
{
accCollection.Invoke(new Action<string>(AddItem), new Object[] { value });
}
else
{
accCollection.Items.Add(value);
}
}
as you can see im using the invoke method to wrap the control for use in my method that is on the task.
private void button1_Click_1(object sender, EventArgs e)
{
checkBox1.Checked = true;
string acct = accCollection.Text;
Task t = new Task(() => GetsalesFigures(acct));
t.Start();
}
this runs the task that calls my giant query method.
private void getsalesfigures(string acct)
{
string acct;// test using 1560
SqlConnection conn = new SqlConnection(myConn);
SqlCommand Pareto = new SqlCommand();
BindingSource bindme = new BindingSource();
SqlDataAdapter adapt1 = new SqlDataAdapter(Pareto);
DataSet dataSet1 = new DataSet();
DataTable table1 = new DataTable();
acct = Acct;
string fromDate = this.dateTimePicker1.Value.ToString("MM/dd/yyyy");
string tooDate = this.dateTimePicker2.Value.ToString("MM/dd/yyyy");
Pareto.Connection = conn;
Pareto.CommandType = CommandType.StoredProcedure;
Pareto.CommandText = "dbo.GetSalesParetotemp";
Pareto.CommandTimeout = 120;
Pareto.Parameters.AddWithValue("@acct", acct);
Pareto.Parameters.AddWithValue("@from", fromDate);
Pareto.Parameters.AddWithValue("@too", tooDate);
SetCheckBoxValue(true);
SetPictureBoxVisibility(true);
adapt1.Fill(dataSet1, "Pareto");
SetCheckBoxValue(false);
SetPictureBoxVisibility(false);
SetDataGrid(true, dataSet1, "Pareto", DataGridViewAutoSizeColumnsMode.AllCells);
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (Exception execc)
{
MessageBox.Show("Whoops! Seems we couldnt connect to the server!"
+ " information:\n\n" + execc.Message + execc.StackTrace,
"Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
What I want to do is add another field to my query called “keycode”, store this in a 2nd column in my ComboBox and then display the name field for the user, but use the keycode field as the value to be used in my giant task query.
I’m having trouble figuring out how I to do this.
In the past, I’ve used an object that contains an override of ToString() and instead of adding plain strings to my combo boxes (or other lists), I add these objects. Then, when you need to get the value of a selected item, you can cast it and do GetValue(). Here’s a sample.
Then, change your AddItem to add items this way:
And to retrieve the value: