Page 1 of 2
Dependent combobox?
Posted: Fri Nov 21, 2008 8:29 am
by Jennypi
Hi,
My report uses a user form with 2 comboboxes. I would like the 2nd one to be dependent on the 1st one. It means that when users will choose the 1st combo, the content of the 2nd one will be adapted with only relevant values.
My combo are based on 2 different queries. I tried to define the second one with including a parameter from the 1st one (the selected value) but it is not working, it leaves the 2nd one blank.
I also tried to set the 'event editor' of the 1st combo with 'Query2.Connect();' but still got nothing.
I hope this is understandable!
Could you please help me?
Thanks,
Jennifer.
Dependent combobox?
Posted: Fri Nov 21, 2008 9:35 pm
by Edward
Hi, Jennifer.
Please see the report from the attachment. The same task has been done in that report.
Run the Demo.exe from the standard installation, press 'Design' button and open the report template there.
Thank you.
Dependent combobox?
Posted: Mon Nov 24, 2008 5:07 am
by Jennypi
Hi Edward,
Thank you for your answer.
Unfortunately I was not able to understand the code of the report you gave me. I think it does not work exactly the way I want and I can not find out how to modify it correctly.
(Also, I think your report does not work properly because t1 and t2 are not filled with anything although I guess they shoud be)
Let's say that my first combobox is filled in with names of several teams (football teams for example).
Each team as an ID in the "teams" table, but this id is not visible in the combo. (or is it possible to display 2 columns in one combo?)
Each football game involving this team is recorded in the "games" table, with the date of the game and the ID of the 2 involved teams.
Depending on the chosen team, the 2nd combo would display the recorded game dates for this team.
Can you please help me again with this problem? I have to learn a lot !
Thank you!
Dependent combobox?
Posted: Tue Nov 25, 2008 5:23 am
by Edward
Hi, Jennifer.
Please follow the steps:
1. Run Demo.exe from the following folder:
"C:\Program Files\Stimulsoft Reports.Net 2008.2 Trial\Bin\Demo.exe"
2. Press 'Design' button.
3. Open the report I sent to you.
Here is a brief explanation how it works:
When the Form1 is being shown for a very first time Comboboxes are filled as follows:
ComboBox1 with items from Categories DataSource
ComboBox2 - with items from Products DataSource (and these DataSources has a relation between them and filling of the Combobox2 is being done according with Categories.CategoryID)
Here is all the code that the report contains:
Code: Select all
if (ComboBox1.SelectedIndex >= 0)
{
int index = (int)Categories.GetData("CategoriesID", ComboBox1.SelectedIndex);
ComboBox2.Control.Items.Clear();
Products.First();
while (!Products.IsEof)
{
if (Products.CategoryID == index)
{
ComboBox2.Control.Items.Add(Products.ProductName);
}
Products.Next();
}
ComboBox2.Control.SelectedIndex = 0;
}
Thank you.
Dependent combobox?
Posted: Tue Nov 25, 2008 8:52 am
by Jennypi
Thank you for your explanation Edward.
Now I have successed in filling my second combobox according to the selected value of the first one. The link between the 2 lists is an INT, like in your example.
But then, I have a third combobox (ComboBoxControl2) depending on the selected values of the 2 previous one.
One link is done with the ID of the team, the other link is done with the year.
I think I wrote it correctly:
Code: Select all
if (ComboBoxControl1.SelectedIndex >= 0)
{
if (ComboBoxControl3.SelectedIndex >= 0)
{
int index = (int)Team.GetData("maslul_id", ComboBoxControl1.SelectedIndex);
int indexyear = (int)Year.GetData("year", ComboBoxControl3.SelectedIndex);
ComboBoxControl2.Control.Items.Clear();
Request_list.First();
while (!Request_list.IsEof)
{
if (Request_list.year == indexyear)
{
if (Request_list.maslul_id == index)
{
ComboBoxControl2.Control.Items.Add(Request_list.delivery_number);
}
}
Request_list.Next();
}
ComboBoxControl2.Control.SelectedIndex = 0;
}
}
but I am getting an error when running it: I select the team in the list -> no problem -> I select the year in the list -> I get this message:
.
I think it is because of the field "year" which is defined as a smallint in the database (I can not change it). Could it be the cause of the problem? How to fix it?
Thank you.
Dependent combobox?
Posted: Tue Nov 25, 2008 12:17 pm
by Edward
Hi, Jennifer.
Could you please check which line led to the error. (You can comment lines one by one until the error disappears.)
For tracking of the error you also could save the report as cs file (it is available option in 'Save' and in 'Save as' menu command the Designer) . Then you can attach this class to your project and to debug the code. It usually helps to track down such errors.
Thank you.
Dependent combobox?
Posted: Tue Nov 25, 2008 2:40 pm
by Jennypi
My error is indeed where I thought.
By commenting the line ' int indexyear = (int)Year.GetData("year", ComboBoxControl3.SelectedIndex);' I have no error anymore.
I tried to set it as a string like this:
Code: Select all
string indexyear = (string)Year.GetData("year", ComboBoxControl3.SelectedIndex);
but I got this message:
Code: Select all
Unable to cast object of type 'System.Int32' to type 'System.String'.
I guess I am missing a cast to pass my year parameter to my variable indexyear.
How should I do that?
Thank you.
EDIT : here is the first error message with the non valid cast thing

Dependent combobox?
Posted: Wed Nov 26, 2008 5:08 am
by Edward
Hello, Jennifer.
Please modify your code as follows:
Code: Select all
string indexyear = Year.GetData("year", ComboBoxControl3.SelectedIndex).ToString();
Thank you.
Dependent combobox?
Posted: Wed Nov 26, 2008 6:40 am
by Jennypi
Thank you for your answer.
I had finally found a solution by modifying my code like this:
Code: Select all
string indexyear = (ComboBoxControl3.SelectedItem).ToString();
And it is working well too.
Another question: how can I get the content of a cell in the grid? I mean if the user clicks on a row, I want to display (in a text box) the text contained in a certain column for this row. I have already search in the whole forum, in the demos but also on the web, can't find out how to do this! Selectedrow? SelectedItem?
Thank you!

Dependent combobox?
Posted: Thu Nov 27, 2008 6:08 pm
by Edward
Hi, Jennifer.
Please use the following code:
Code: Select all
DataGridCell myCell;
myCell = GridControl1.Control.CurrentCell;
DataTable myTable;
// Assumes the DataGrid is bound to a DataView.
myTable = ((DataView) GridControl1.Control.DataSource).Table;
MessageBox.Show(myTable.Rows[myCell.RowNumber][myCell.ColumnNumber].ToString());
Thank you.