Gary Jones
asked on
Populate ComboBox in Custome Ribbon
How do I populate a combobox that is in a custom ribbon. When I try to use the .Items.Add() method (or whatever it's called) I get an error message that reads:
Cannot convert from 'object' to 'Microsoft.Office.Tools.Ri bon.Ribbon DropDown'.
I have included the code I have so far.
Any and all help is welcome!
Cannot convert from 'object' to 'Microsoft.Office.Tools.Ri
I have included the code I have so far.
Any and all help is welcome!
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
OracleConnection cs = new OracleConnection("User Id=MyUserID;Password=MyPassword;Data Source=OracleSID");
OracleDataAdapter da = new OracleDataAdapter("SELECT JOB_CODE, JOB_NAME FROM JCJOB", cs);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
//Error is generated at this line!
Globals.Ribbons.Ribbon1.cmbProject.Items.Add(dt.Rows[i]["JOB_CODE"]);
}
}
ASKER
I am very new to c# so i am not sure what you mean by create a RibbonDropDown and add instanced of RibbonDropDownItems. Can I have an example of what you men?
Thanks in advance.
Thanks in advance.
To create an instance of an object, you need to use the constructor (new keyword).
This form might be a little too much for you, so let's rewrite it:
1) Construct a RibbonDropDown:
2) Loop through each of your data rows, using the enumerator, instead of an index variable (i in your case):
3) Create RibbonDropDownItems for each row, and add to the RibbonDropDown. Set the Label fro the drop-down item to the field value.
4) Add the RibbonDropDown to the control:
This form might be a little too much for you, so let's rewrite it:
dropDown.Items.Add(new RibbonDropDownItem() { Label = jobCode });
1) Construct a RibbonDropDown:
RibbonDropDown dropDown = new RibbonDropDown();
2) Loop through each of your data rows, using the enumerator, instead of an index variable (i in your case):
foreach (DataRow dr in dt.Rows)
{
}
3) Create RibbonDropDownItems for each row, and add to the RibbonDropDown. Set the Label fro the drop-down item to the field value.
foreach (DataRow dr in dt.Rows)
{
RibbonDropDownItem dropDownItem = new RibbonDropDownItem();
dropDownItem.Label = dr["JOB_CODE].ToString();
}
4) Add the RibbonDropDown to the control:
Globals.Ribbons.Ribbon1.cmbProject.Items.Add(ribbonDropDown);
ASKER
I now get thefollowing errors:
Error 1 Cannot create an instance of the abstract class or interface 'Microsoft.Office.Tools.Ri bbon.Ribbo nDropDown'
Error 2 Cannot create an instance of the abstract class or interface 'Microsoft.Office.Tools.Ri bbon.Ribbo nDropDownI tem'
Error 1 Cannot create an instance of the abstract class or interface 'Microsoft.Office.Tools.Ri
Error 2 Cannot create an instance of the abstract class or interface 'Microsoft.Office.Tools.Ri
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
TheLearnedOne, I was able to compile the RibbonDropDownItem item = Globals.Factory.GetRibbonF actory().C reateRibbo nDropDownI tem(); statement, what is the next step?
Next step:
item.Label = dr["JOB_CODE].ToString();
ASKER
The code in the "Next step" appears to have worked but now I think I must associate the item.Label to the cmbProject object. How do I add the values to the cmbProject "object" (not sure if that is the correct verbiage). Sorry, but I am extremely new to c# programming (no duh, right ;)).
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The RibbonDropDown (dropDown) has been created. I keep trying to add item.Label to the dropDown but I know I am still missing something as nothing is working. So, after the RibbonDropDown and RibbonDropDownItems have been created successfully, what is the next step?
Next step is to add the dropdown items to the dropdown:
dropDown.Items.Add(item);
ASKER
Error: Index must be within the bounds of the list. Parameter name: index. Not sure what that means but that is the error I get now.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had a difficult time following the code, I did not know where the code needed to go and was confused (the reason for B grade).
foreach (DataRow dr in dt.Rows)
{
string jobCode = dr["JOB_CODE].ToString();
dropDown.Items.Add(new RibbonDropDownItem() { Label = jobCode });
}