Search code examples
c#arrayscomboboxvstoribbon

Dynamically populate a ribbon design combobox with a string array?


I'm wondering how to dynamically populate a combobox in the Ribbon Designer for an excel 2010 Add-in. I have a button that when pressed calls a method that finds a list of all available SQL Servers on the network. I then want to show all the SQL servers that the method found and populate the Combo-box. I have been able to populate the combo-box with the datasource property in a Windows Forms Application, but in the ribbon designer it is missing some of those properties.

I was wondering if there was a way to do it with comboBox1.Items.Add(string[] array here) But it appears that the comboBox1.Items.Add takes object, so I was wondering if there was a way to make each string in the array into an object with a for loop.

I also heard that another possible solution could be to use the XML Ribbon Designer, but I'm not to comfortable with that just yet. I've done a ton of research on it, and haven't been able to find anything that fit my exact situation. Thank-you in advance, here is some of my code.

public static string[] GetSQLServerList()
{
SqlDataSourceEnumerator dse = SqlDataSourceEnumerator.Instance;
DataTable dt = dse.GetDataSources();
if (dt.Rows.Count == 0)
{
      return null;
}

string[] SQLServers = new string[dt.Rows.Count];
int f = -1;
foreach (DataRow r in dt.Rows)
    {
       string SQLServer = r["ServerName"].ToString();
       string Instance = r["InstanceName"].ToString();
       if (Instance != null && !string.IsNullOrEmpty(Instance))
          {
             int i = 0;SQLServer += "\\" + Instance;
          }
       SQLServers[System.Math.Max(System.Threading.Interlocked.Increment(ref f), f - 1)] = SQLServer;
    }
 Array.Sort(SQLServers);

 //not sure how to fill combobox from here
 comboBox1.Items.Add(RibbonDropDownItem here);
 return SQLServers;

 }

In my Windows Forms Application I have the exact same method, and this is how I populate the combo-box.

    private void button1_Click(object sender, EventArgs e)
    {
        Cursor = Cursors.WaitCursor;
        comboBox1.DataSource = GetSQLServerList();
        Cursor = Cursors.Default;
    }

But like I said before, there is no Datasource property in the Ribbon Visual Designer.

  ![ribbon designer combobox properties][1]
  [1]: https://i.sstatic.net/xzNUO.png

So, I think Basically, in the excel 2010 add-in, there will be a button that populates the combobox with the SQL servers on the network. Then the user can choose from the combo-box which server they want to connect to. All I need to be able to do from there is get the selected option from the combobox and I'm set. Thanks so much in advance!


Solution

  • I found what worked, using mostly what The Dutch Man said above. I made a for loop that would iterate through each string in the array, and got the value from it. I then used the code he gave me and added the values to the combobox.

    private void button2_Click_2(object sender, RibbonControlEventArgs e)
        {
    
            SqlDataSourceEnumerator dse = SqlDataSourceEnumerator.Instance;
            DataTable dt = dse.GetDataSources();
            if (dt.Rows.Count == 0)
            {
               // return null;
            }
    
            string[] SQLServers = new string[dt.Rows.Count];
            int f = -1;
            foreach (DataRow r in dt.Rows)
            {
                string SQLServer = r["ServerName"].ToString();
                string Instance = r["InstanceName"].ToString();
                if (Instance != null && !string.IsNullOrEmpty(Instance))
                {
                    int i = 0; SQLServer += "\\" + Instance;
                }
                SQLServers[System.Math.Max(System.Threading.Interlocked.Increment(ref f), f - 1)] = SQLServer;
            }
            Array.Sort(SQLServers);
    
            for(int i = 0; i < SQLServers.Length; i++)
            {
    
    
                RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
                item.Label = SQLServers.GetValue(i).ToString();
                //item.Label = "yourtext";
    
                comboBox1.Items.Add(item);
            }
        }