Hello Experts,
I am currently populating three Lisboxes from database fields like this:
//*********GETTING DATA FROM DB and FILLING LISTBOXES
(Connection string code not shown)
dataAdapter = new SqlDataAdapter();
dataSet = new DataSet();
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet, "dbFields");
dataTable = dataSet.Tables[0];
SqlDataReader reader = cmd.ExecuteReader();
//fill first listbox
lbOne.Items.Clear();
foreach ( DataRow dataRowOne in dataTable.Rows)
{
lbOne.Items.Add(dataRow["f
ldOne"]);
}
//fill second listbox
lbTwo.Items.Clear();
foreach (DataRow dataRowTwo in dataTable.Rows)
{
lbTwo.Items.Add(dataRowTwo
["fldTwo"]
);
}
//fill third listbox
lbThree.Items.Clear();
foreach (DataRow dataRowThree in dataTable.Rows)
{
lbThree.Items.Add(dataRowS
pecialArea
["fldThree
"]);
}
reader.Close();
/*********END GETTING DATA FROM DB and FILLING LISTBOXES
I then have this code to select each possible combination of fields represented in the three listboxes based on database records:
//Code is called when user clicks on an item in lbOne
DataRow selectedRow = dataTable.Rows[lbTwo.Selec
tedIndex];
lbOne.Text = selectedRow["fieldOne"].To
String();
lbThree.Text = selectedRow["fieldThree"].
ToString()
;
For Example: If there is even one record in the database that looks like this:
rec fieldOne fieldTwo fieldThree
1 A C V
2 A C B
3 A D B
The values of all three listboxes selected when the user clicks on the first item (A of Rec1) in lbOne would be:
lbOne = A
lbTwo = C
lbThree = V
When the second itemin lbOne is selected (A of Rec 2) in lbOne :
lbOne = A
lbTwo = C
lbThree = B
And So on...
Now I have the following code to select each item in lbOne automatically and populate a variable based on each value in lbOne like this:
double x = 0;
foreach (object obj in lbOne.Items)
{
x = Convert.ToDouble(obj);
}
This works fine to extract ech item in lbOne but...
Problem: Does anyone know how to select the other listBox values in the for loop to get each possible combination from the database without the user having to click on each item in lbOne (like I have shown above) to select the items in lbTwo and lbThree???